RetryJob.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\FederatedFileSharing\BackgroundJob;
  23. use OC\BackgroundJob\Job;
  24. use OC\BackgroundJob\JobList;
  25. use OCA\FederatedFileSharing\AddressHandler;
  26. use OCA\FederatedFileSharing\DiscoveryManager;
  27. use OCA\FederatedFileSharing\Notifications;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\ILogger;
  30. /**
  31. * Class RetryJob
  32. *
  33. * Background job to re-send update of federated re-shares to the remote server in
  34. * case the server was not available on the first try
  35. *
  36. * @package OCA\FederatedFileSharing\BackgroundJob
  37. */
  38. class RetryJob extends Job {
  39. /** @var bool */
  40. private $retainJob = true;
  41. /** @var Notifications */
  42. private $notifications;
  43. /** @var int max number of attempts to send the request */
  44. private $maxTry = 20;
  45. /** @var int how much time should be between two tries (10 minutes) */
  46. private $interval = 600;
  47. /**
  48. * UnShare constructor.
  49. *
  50. * @param Notifications $notifications
  51. */
  52. public function __construct(Notifications $notifications = null) {
  53. if ($notifications) {
  54. $this->notifications = $notifications;
  55. } else {
  56. $addressHandler = new AddressHandler(
  57. \OC::$server->getURLGenerator(),
  58. \OC::$server->getL10N('federatedfilesharing')
  59. );
  60. $discoveryManager = new DiscoveryManager(
  61. \OC::$server->getMemCacheFactory(),
  62. \OC::$server->getHTTPClientService()
  63. );
  64. $this->notifications = new Notifications(
  65. $addressHandler,
  66. \OC::$server->getHTTPClientService(),
  67. $discoveryManager,
  68. \OC::$server->getJobList()
  69. );
  70. }
  71. }
  72. /**
  73. * run the job, then remove it from the jobList
  74. *
  75. * @param JobList $jobList
  76. * @param ILogger $logger
  77. */
  78. public function execute($jobList, ILogger $logger = null) {
  79. if ($this->shouldRun($this->argument)) {
  80. parent::execute($jobList, $logger);
  81. $jobList->remove($this, $this->argument);
  82. if ($this->retainJob) {
  83. $this->reAddJob($jobList, $this->argument);
  84. }
  85. }
  86. }
  87. protected function run($argument) {
  88. $remote = $argument['remote'];
  89. $remoteId = $argument['remoteId'];
  90. $token = $argument['token'];
  91. $action = $argument['action'];
  92. $data = json_decode($argument['data'], true);
  93. $try = (int)$argument['try'] + 1;
  94. $result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try);
  95. if ($result === true || $try > $this->maxTry) {
  96. $this->retainJob = false;
  97. }
  98. }
  99. /**
  100. * re-add background job with new arguments
  101. *
  102. * @param IJobList $jobList
  103. * @param array $argument
  104. */
  105. protected function reAddJob(IJobList $jobList, array $argument) {
  106. $jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob',
  107. [
  108. 'remote' => $argument['remote'],
  109. 'remoteId' => $argument['remoteId'],
  110. 'token' => $argument['token'],
  111. 'data' => $argument['data'],
  112. 'action' => $argument['action'],
  113. 'try' => (int)$argument['try'] + 1,
  114. 'lastRun' => time()
  115. ]
  116. );
  117. }
  118. /**
  119. * test if it is time for the next run
  120. *
  121. * @param array $argument
  122. * @return bool
  123. */
  124. protected function shouldRun(array $argument) {
  125. $lastRun = (int)$argument['lastRun'];
  126. return ((time() - $lastRun) > $this->interval);
  127. }
  128. }