RetryJob.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  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 OC\BackgroundJob\Job;
  29. use OC\BackgroundJob\JobList;
  30. use OCA\FederatedFileSharing\AddressHandler;
  31. use OCA\FederatedFileSharing\Notifications;
  32. use OCP\BackgroundJob\IJobList;
  33. use OCP\ILogger;
  34. /**
  35. * Class RetryJob
  36. *
  37. * Background job to re-send update of federated re-shares to the remote server in
  38. * case the server was not available on the first try
  39. *
  40. * @package OCA\FederatedFileSharing\BackgroundJob
  41. */
  42. class RetryJob extends Job {
  43. /** @var bool */
  44. private $retainJob = true;
  45. /** @var Notifications */
  46. private $notifications;
  47. /** @var int max number of attempts to send the request */
  48. private $maxTry = 20;
  49. /** @var int how much time should be between two tries (10 minutes) */
  50. private $interval = 600;
  51. /**
  52. * UnShare constructor.
  53. *
  54. * @param Notifications $notifications
  55. */
  56. public function __construct(Notifications $notifications = null) {
  57. if ($notifications) {
  58. $this->notifications = $notifications;
  59. } else {
  60. $addressHandler = new AddressHandler(
  61. \OC::$server->getURLGenerator(),
  62. \OC::$server->getL10N('federatedfilesharing'),
  63. \OC::$server->getCloudIdManager()
  64. );
  65. $this->notifications = new Notifications(
  66. $addressHandler,
  67. \OC::$server->getHTTPClientService(),
  68. \OC::$server->query(\OCP\OCS\IDiscoveryService::class),
  69. \OC::$server->getJobList(),
  70. \OC::$server->getCloudFederationProviderManager(),
  71. \OC::$server->getCloudFederationFactory()
  72. );
  73. }
  74. }
  75. /**
  76. * run the job, then remove it from the jobList
  77. *
  78. * @param JobList $jobList
  79. * @param ILogger|null $logger
  80. */
  81. public function execute($jobList, ILogger $logger = null) {
  82. if ($this->shouldRun($this->argument)) {
  83. parent::execute($jobList, $logger);
  84. $jobList->remove($this, $this->argument);
  85. if ($this->retainJob) {
  86. $this->reAddJob($jobList, $this->argument);
  87. }
  88. }
  89. }
  90. protected function run($argument) {
  91. $remote = $argument['remote'];
  92. $remoteId = $argument['remoteId'];
  93. $token = $argument['token'];
  94. $action = $argument['action'];
  95. $data = json_decode($argument['data'], true);
  96. $try = (int)$argument['try'] + 1;
  97. $result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try);
  98. if ($result === true || $try > $this->maxTry) {
  99. $this->retainJob = false;
  100. }
  101. }
  102. /**
  103. * re-add background job with new arguments
  104. *
  105. * @param IJobList $jobList
  106. * @param array $argument
  107. */
  108. protected function reAddJob(IJobList $jobList, array $argument) {
  109. $jobList->add(RetryJob::class,
  110. [
  111. 'remote' => $argument['remote'],
  112. 'remoteId' => $argument['remoteId'],
  113. 'token' => $argument['token'],
  114. 'data' => $argument['data'],
  115. 'action' => $argument['action'],
  116. 'try' => (int)$argument['try'] + 1,
  117. 'lastRun' => time()
  118. ]
  119. );
  120. }
  121. /**
  122. * test if it is time for the next run
  123. *
  124. * @param array $argument
  125. * @return bool
  126. */
  127. protected function shouldRun(array $argument) {
  128. $lastRun = (int)$argument['lastRun'];
  129. return ((time() - $lastRun) > $this->interval);
  130. }
  131. }