1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]> |
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
|
|
|
|
22
|
|
|
namespace OCA\LookupServerConnector\BackgroundJobs; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use OC\BackgroundJob\Job; |
26
|
|
|
use OC\BackgroundJob\JobList; |
27
|
|
|
use OCP\BackgroundJob\IJobList; |
28
|
|
|
use OCP\Http\Client\IClientService; |
29
|
|
|
use OCP\IConfig; |
30
|
|
|
use OCP\ILogger; |
31
|
|
|
|
32
|
|
|
class RetryJob extends Job { |
33
|
|
|
/** @var IClientService */ |
34
|
|
|
private $clientService; |
35
|
|
|
/** @var IJobList */ |
36
|
|
|
private $jobList; |
37
|
|
|
/** @var string */ |
38
|
|
|
private $lookupServer; |
39
|
|
|
/** @var int how much time should be between two, will be increased for each retry */ |
40
|
|
|
private $interval = 100; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param IClientService $clientService |
44
|
|
|
* @param IJobList $jobList |
45
|
|
|
* @param IConfig $config |
46
|
|
|
*/ |
47
|
|
|
public function __construct(IClientService $clientService, |
48
|
|
|
IJobList $jobList, |
49
|
|
|
IConfig $config) { |
50
|
|
|
$this->clientService = $clientService; |
51
|
|
|
$this->jobList = $jobList; |
52
|
|
|
|
53
|
|
|
if ($config->getSystemValue('has_internet_connection', true) === false) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); |
58
|
|
|
if (!empty($this->lookupServer)) { |
59
|
|
|
$this->lookupServer = rtrim($this->lookupServer, '/'); |
60
|
|
|
$this->lookupServer .= '/users'; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* run the job, then remove it from the jobList |
66
|
|
|
* |
67
|
|
|
* @param JobList $jobList |
68
|
|
|
* @param ILogger|null $logger |
69
|
|
|
*/ |
70
|
|
|
public function execute($jobList, ILogger $logger = null) { |
71
|
|
|
if ($this->shouldRun($this->argument)) { |
72
|
|
|
parent::execute($jobList, $logger); |
73
|
|
|
$jobList->remove($this, $this->argument); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function run($argument) { |
78
|
|
|
if ($this->killBackgroundJob((int)$argument['retryNo'])) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$client = $this->clientService->newClient(); |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$client->post($this->lookupServer, |
86
|
|
|
[ |
87
|
|
|
'body' => json_encode($argument['dataArray']), |
88
|
|
|
'timeout' => 10, |
89
|
|
|
'connect_timeout' => 3, |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
} catch (\Exception $e) { |
93
|
|
|
$this->jobList->add(RetryJob::class, |
94
|
|
|
[ |
95
|
|
|
'dataArray' => $argument['dataArray'], |
96
|
|
|
'retryNo' => $argument['retryNo'] + 1, |
97
|
|
|
'lastRun' => time(), |
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* test if it is time for the next run |
106
|
|
|
* |
107
|
|
|
* @param array $argument |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
protected function shouldRun($argument) { |
111
|
|
|
$retryNo = (int)$argument['retryNo']; |
112
|
|
|
$delay = $this->interval * 6 ** $retryNo; |
113
|
|
|
return !isset($argument['lastRun']) || ((time() - $argument['lastRun']) > $delay); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* check if we should kill the background job |
118
|
|
|
* |
119
|
|
|
* The lookup server should no longer be contacted if: |
120
|
|
|
* |
121
|
|
|
* - max retries are reached (set to 5) |
122
|
|
|
* - lookup server was disabled by the admin |
123
|
|
|
* - no valid lookup server URL given |
124
|
|
|
* |
125
|
|
|
* @param int $retryCount |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
protected function killBackgroundJob($retryCount) { |
129
|
|
|
$maxTriesReached = $retryCount >= 5; |
130
|
|
|
$lookupServerDisabled = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes'; |
|
|
|
|
131
|
|
|
|
132
|
|
|
return $maxTriesReached || $lookupServerDisabled || empty($this->lookupServer); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|