Completed
Push — master ( 1c2ccd...167013 )
by Morris
14:20
created

RetryJob::run()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 26
rs 8.8571
c 1
b 0
f 0
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 tries (10 minutes) */
40
	private $interval = 600;
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
		$this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
54
		$this->lookupServer = rtrim($this->lookupServer, '/');
55
		$this->lookupServer .= '/users';
56
	}
57
58
	/**
59
	 * run the job, then remove it from the jobList
60
	 *
61
	 * @param JobList $jobList
62
	 * @param ILogger $logger
63
	 */
64
	public function execute($jobList, ILogger $logger = null) {
65
		if ($this->shouldRun($this->argument)) {
66
			parent::execute($jobList, $logger);
67
			$jobList->remove($this, $this->argument);
68
		}
69
	}
70
71
	protected function run($argument) {
72
		if($argument['retryNo'] === 5) {
73
			return;
74
		}
75
76
		$client = $this->clientService->newClient();
77
78
		try {
79
			$client->post($this->lookupServer,
80
				[
81
					'body' => json_encode($argument['dataArray']),
82
					'timeout' => 10,
83
					'connect_timeout' => 3,
84
				]
85
			);
86
		} catch (\Exception $e) {
87
			$this->jobList->add(RetryJob::class,
88
				[
89
					'dataArray' => $argument['dataArray'],
90
					'retryNo' => $argument['retryNo'] + 1,
91
					'lastRun' => time(),
92
				]
93
			);
94
95
		}
96
	}
97
98
	/**
99
	 * test if it is time for the next run
100
	 *
101
	 * @param array $argument
102
	 * @return bool
103
	 */
104
	protected function shouldRun($argument) {
105
		return !isset($argument['lastRun']) || ((time() - $argument['lastRun']) > $this->interval);
106
	}
107
}
108