RetryJob.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\LookupServerConnector\BackgroundJobs;
  22. use OC\BackgroundJob\Job;
  23. use OC\BackgroundJob\JobList;
  24. use OCP\BackgroundJob\IJobList;
  25. use OCP\Http\Client\IClientService;
  26. use OCP\IConfig;
  27. use OCP\ILogger;
  28. class RetryJob extends Job {
  29. /** @var IClientService */
  30. private $clientService;
  31. /** @var IJobList */
  32. private $jobList;
  33. /** @var string */
  34. private $lookupServer;
  35. /** @var int how much time should be between two, will be increased for each retry */
  36. private $interval = 100;
  37. /** @var IConfig */
  38. private $config;
  39. /**
  40. * @param IClientService $clientService
  41. * @param IJobList $jobList
  42. * @param IConfig $config
  43. */
  44. public function __construct(IClientService $clientService,
  45. IJobList $jobList,
  46. IConfig $config) {
  47. $this->clientService = $clientService;
  48. $this->jobList = $jobList;
  49. $this->config = $config;
  50. if ($config->getSystemValue('has_internet_connection', true) === false) {
  51. return;
  52. }
  53. $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
  54. if (!empty($this->lookupServer)) {
  55. $this->lookupServer = rtrim($this->lookupServer, '/');
  56. $this->lookupServer .= '/users';
  57. }
  58. }
  59. /**
  60. * run the job, then remove it from the jobList
  61. *
  62. * @param JobList $jobList
  63. * @param ILogger|null $logger
  64. */
  65. public function execute($jobList, ILogger $logger = null) {
  66. if ($this->shouldRun($this->argument)) {
  67. parent::execute($jobList, $logger);
  68. $jobList->remove($this, $this->argument);
  69. }
  70. }
  71. protected function run($argument) {
  72. if ($this->killBackgroundJob((int)$argument['retryNo'])) {
  73. return;
  74. }
  75. $client = $this->clientService->newClient();
  76. try {
  77. $client->post($this->lookupServer,
  78. [
  79. 'body' => json_encode($argument['dataArray']),
  80. 'timeout' => 10,
  81. 'connect_timeout' => 3,
  82. ]
  83. );
  84. } catch (\Exception $e) {
  85. $this->jobList->add(RetryJob::class,
  86. [
  87. 'dataArray' => $argument['dataArray'],
  88. 'retryNo' => $argument['retryNo'] + 1,
  89. 'lastRun' => time(),
  90. ]
  91. );
  92. }
  93. }
  94. /**
  95. * test if it is time for the next run
  96. *
  97. * @param array $argument
  98. * @return bool
  99. */
  100. protected function shouldRun($argument) {
  101. $retryNo = (int)$argument['retryNo'];
  102. $delay = $this->interval * 6 ** $retryNo;
  103. return !isset($argument['lastRun']) || ((time() - $argument['lastRun']) > $delay);
  104. }
  105. /**
  106. * check if we should kill the background job
  107. *
  108. * The lookup server should no longer be contacted if:
  109. *
  110. * - max retries are reached (set to 5)
  111. * - lookup server was disabled by the admin
  112. * - no valid lookup server URL given
  113. *
  114. * @param int $retryCount
  115. * @return bool
  116. */
  117. protected function killBackgroundJob($retryCount) {
  118. $maxTriesReached = $retryCount >= 5;
  119. $lookupServerDisabled = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes';
  120. return $maxTriesReached || $lookupServerDisabled || empty($this->lookupServer);
  121. }
  122. }