RetryJob.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. use OCP\ILogger;
  33. /**
  34. * Class RetryJob
  35. *
  36. * Background job to re-send update of federated re-shares to the remote server in
  37. * case the server was not available on the first try
  38. *
  39. * @package OCA\FederatedFileSharing\BackgroundJob
  40. */
  41. class RetryJob extends Job {
  42. private bool $retainJob = true;
  43. private Notifications $notifications;
  44. /** @var int max number of attempts to send the request */
  45. private int $maxTry = 20;
  46. /** @var int how much time should be between two tries (10 minutes) */
  47. private int $interval = 600;
  48. public function __construct(Notifications $notifications,
  49. ITimeFactory $time) {
  50. parent::__construct($time);
  51. $this->notifications = $notifications;
  52. }
  53. /**
  54. * Run the job, then remove it from the jobList
  55. */
  56. public function start(IJobList $jobList): void {
  57. if ($this->shouldRun($this->argument)) {
  58. parent::start($jobList);
  59. $jobList->remove($this, $this->argument);
  60. if ($this->retainJob) {
  61. $this->reAddJob($jobList, $this->argument);
  62. }
  63. }
  64. }
  65. protected function run($argument) {
  66. $remote = $argument['remote'];
  67. $remoteId = $argument['remoteId'];
  68. $token = $argument['token'];
  69. $action = $argument['action'];
  70. $data = json_decode($argument['data'], true);
  71. $try = (int)$argument['try'] + 1;
  72. $result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try);
  73. if ($result === true || $try > $this->maxTry) {
  74. $this->retainJob = false;
  75. }
  76. }
  77. /**
  78. * Re-add background job with new arguments
  79. */
  80. protected function reAddJob(IJobList $jobList, array $argument): void {
  81. $jobList->add(RetryJob::class,
  82. [
  83. 'remote' => $argument['remote'],
  84. 'remoteId' => $argument['remoteId'],
  85. 'token' => $argument['token'],
  86. 'data' => $argument['data'],
  87. 'action' => $argument['action'],
  88. 'try' => (int)$argument['try'] + 1,
  89. 'lastRun' => $this->time->getTime()
  90. ]
  91. );
  92. }
  93. /**
  94. * Test if it is time for the next run
  95. */
  96. protected function shouldRun(array $argument): bool {
  97. $lastRun = (int)$argument['lastRun'];
  98. return (($this->time->getTime() - $lastRun) > $this->interval);
  99. }
  100. }