Passed
Push — master ( c48582...d94b56 )
by Roeland
10:10
created

BackgroundRepair::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
namespace OC\Migration;
24
25
use OC\BackgroundJob\JobList;
26
use OC\BackgroundJob\TimedJob;
27
use OC\NeedsUpdateException;
28
use OC\Repair;
29
use OC_App;
30
use OCP\BackgroundJob\IJobList;
31
use OCP\ILogger;
32
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
33
34
/**
35
 * Class BackgroundRepair
36
 *
37
 * @package OC\Migration
38
 */
39
class BackgroundRepair extends TimedJob {
40
41
	/** @var IJobList */
42
	private $jobList;
43
44
	/** @var ILogger */
45
	private $logger;
46
47
	/** @var EventDispatcherInterface */
48
	private $dispatcher;
49
50
	public function __construct(EventDispatcherInterface $dispatcher) {
51
		$this->dispatcher = $dispatcher;
52
	}
53
54
	/**
55
	 * run the job, then remove it from the job list
56
	 *
57
	 * @param JobList $jobList
58
	 * @param ILogger|null $logger
59
	 */
60
	public function execute($jobList, ILogger $logger = null) {
61
		// add an interval of 15 mins
62
		$this->setInterval(15*60);
63
64
		$this->jobList = $jobList;
65
		$this->logger = $logger;
66
		parent::execute($jobList, $logger);
67
	}
68
69
	/**
70
	 * @param array $argument
71
	 * @throws \Exception
72
	 * @throws \OC\NeedsUpdateException
73
	 */
74
	protected function run($argument) {
75
		if (!isset($argument['app']) || !isset($argument['step'])) {
76
			// remove the job - we can never execute it
77
			$this->jobList->remove($this, $this->argument);
78
			return;
79
		}
80
		$app = $argument['app'];
81
82
		try {
83
			$this->loadApp($app);
84
		} catch (NeedsUpdateException $ex) {
85
			// as long as the app is not yet done with it's offline migration
86
			// we better not start with the live migration
87
			return;
88
		}
89
90
		$step = $argument['step'];
91
		$repair = new Repair([], $this->dispatcher);
92
		try {
93
			$repair->addStep($step);
94
		} catch (\Exception $ex) {
95
			$this->logger->logException($ex,[
96
				'app' => 'migration'
97
			]);
98
99
			// remove the job - we can never execute it
100
			$this->jobList->remove($this, $this->argument);
101
			return;
102
		}
103
104
		// execute the repair step
105
		$repair->run();
106
107
		// remove the job once executed successfully
108
		$this->jobList->remove($this, $this->argument);
109
	}
110
111
	/**
112
	 * @codeCoverageIgnore
113
	 * @param $app
114
	 * @throws NeedsUpdateException
115
	 */
116
	protected function loadApp($app) {
117
		OC_App::loadApp($app);
118
	}
119
}
120