RetryJob.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /**
  38. * @param IClientService $clientService
  39. * @param IJobList $jobList
  40. * @param IConfig $config
  41. */
  42. public function __construct(IClientService $clientService,
  43. IJobList $jobList,
  44. IConfig $config) {
  45. $this->clientService = $clientService;
  46. $this->jobList = $jobList;
  47. if ($config->getSystemValue('has_internet_connection', true) === false) {
  48. return;
  49. }
  50. $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
  51. if (!empty($this->lookupServer)) {
  52. $this->lookupServer = rtrim($this->lookupServer, '/');
  53. $this->lookupServer .= '/users';
  54. }
  55. }
  56. /**
  57. * run the job, then remove it from the jobList
  58. *
  59. * @param JobList $jobList
  60. * @param ILogger|null $logger
  61. */
  62. public function execute($jobList, ILogger $logger = null) {
  63. if ($this->shouldRun($this->argument)) {
  64. parent::execute($jobList, $logger);
  65. $jobList->remove($this, $this->argument);
  66. }
  67. }
  68. protected function run($argument) {
  69. if ($this->killBackgroundJob((int)$argument['retryNo'])) {
  70. return;
  71. }
  72. $client = $this->clientService->newClient();
  73. try {
  74. $client->post($this->lookupServer,
  75. [
  76. 'body' => json_encode($argument['dataArray']),
  77. 'timeout' => 10,
  78. 'connect_timeout' => 3,
  79. ]
  80. );
  81. } catch (\Exception $e) {
  82. $this->jobList->add(RetryJob::class,
  83. [
  84. 'dataArray' => $argument['dataArray'],
  85. 'retryNo' => $argument['retryNo'] + 1,
  86. 'lastRun' => time(),
  87. ]
  88. );
  89. }
  90. }
  91. /**
  92. * test if it is time for the next run
  93. *
  94. * @param array $argument
  95. * @return bool
  96. */
  97. protected function shouldRun($argument) {
  98. $retryNo = (int)$argument['retryNo'];
  99. $delay = $this->interval * 6 ** $retryNo;
  100. return !isset($argument['lastRun']) || ((time() - $argument['lastRun']) > $delay);
  101. }
  102. /**
  103. * check if we should kill the background job
  104. *
  105. * The lookup server should no longer be contacted if:
  106. *
  107. * - max retries are reached (set to 5)
  108. * - lookup server was disabled by the admin
  109. * - no valid lookup server URL given
  110. *
  111. * @param int $retryCount
  112. * @return bool
  113. */
  114. protected function killBackgroundJob($retryCount) {
  115. $maxTriesReached = $retryCount >= 5;
  116. $lookupServerDisabled = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes';
  117. return $maxTriesReached || $lookupServerDisabled || empty($this->lookupServer);
  118. }
  119. }