RetryJob.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\FederatedFileSharing\BackgroundJob;
  28. use OCA\FederatedFileSharing\Notifications;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\BackgroundJob\IJobList;
  31. use OCP\BackgroundJob\Job;
  32. /**
  33. * Class RetryJob
  34. *
  35. * Background job to re-send update of federated re-shares to the remote server in
  36. * case the server was not available on the first try
  37. *
  38. * @package OCA\FederatedFileSharing\BackgroundJob
  39. */
  40. class RetryJob extends Job {
  41. private bool $retainJob = true;
  42. private Notifications $notifications;
  43. /** @var int max number of attempts to send the request */
  44. private int $maxTry = 20;
  45. /** @var int how much time should be between two tries (10 minutes) */
  46. private int $interval = 600;
  47. public function __construct(Notifications $notifications,
  48. ITimeFactory $time) {
  49. parent::__construct($time);
  50. $this->notifications = $notifications;
  51. }
  52. /**
  53. * Run the job, then remove it from the jobList
  54. */
  55. public function start(IJobList $jobList): void {
  56. if ($this->shouldRun($this->argument)) {
  57. parent::start($jobList);
  58. $jobList->remove($this, $this->argument);
  59. if ($this->retainJob) {
  60. $this->reAddJob($jobList, $this->argument);
  61. }
  62. }
  63. }
  64. protected function run($argument) {
  65. $remote = $argument['remote'];
  66. $remoteId = $argument['remoteId'];
  67. $token = $argument['token'];
  68. $action = $argument['action'];
  69. $data = json_decode($argument['data'], true);
  70. $try = (int)$argument['try'] + 1;
  71. $result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try);
  72. if ($result === true || $try > $this->maxTry) {
  73. $this->retainJob = false;
  74. }
  75. }
  76. /**
  77. * Re-add background job with new arguments
  78. */
  79. protected function reAddJob(IJobList $jobList, array $argument): void {
  80. $jobList->add(RetryJob::class,
  81. [
  82. 'remote' => $argument['remote'],
  83. 'remoteId' => $argument['remoteId'],
  84. 'token' => $argument['token'],
  85. 'data' => $argument['data'],
  86. 'action' => $argument['action'],
  87. 'try' => (int)$argument['try'] + 1,
  88. 'lastRun' => $this->time->getTime()
  89. ]
  90. );
  91. }
  92. /**
  93. * Test if it is time for the next run
  94. */
  95. protected function shouldRun(array $argument): bool {
  96. $lastRun = (int)$argument['lastRun'];
  97. return (($this->time->getTime() - $lastRun) > $this->interval);
  98. }
  99. }