Completed
Push — master ( ab614a...e3991a )
by Thomas
50:58 queued 38:53
created

Cron::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Core\Command\System;
23
24
use OCP\BackgroundJob\IJobList;
25
use OCP\IConfig;
26
use OCP\ILogger;
27
use OCP\ITempManager;
28
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Helper\ProgressBar;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\OutputInterface;
32
33
class Cron extends Command {
34
35
	/** @var \OCP\BackgroundJob\IJobList */
36
	private $jobList;
37
	/** @var IConfig */
38
	private $config;
39
	/** @var ILogger */
40
	private $logger;
41
	/** @var ITempManager */
42
	private $tempManager;
43
44
	/**
45
	 * Cron constructor.
46
	 *
47
	 * @param IJobList $jobList
48
	 * @param IConfig $config
49
	 * @param ILogger $logger
50
	 * @param ITempManager $tempManager
51
	 */
52 View Code Duplication
	public function __construct(IJobList $jobList,
53
								IConfig $config,
54
								ILogger $logger,
55
								ITempManager $tempManager) {
56
		$this->jobList = $jobList;
57
		$this->config = $config;
58
		$this->logger = $logger;
59
		$this->tempManager = $tempManager;
60
		parent::__construct();
61
	}
62
63
	protected function configure() {
64
		$this
65
			->setName('system:cron')
66
			->setDescription('Execute background jobs as cron');
67
	}
68
69
	/**
70
	 * @param InputInterface $input
71
	 * @param OutputInterface $output
72
	 * @return int
73
	 */
74
	protected function execute(InputInterface $input, OutputInterface $output) {
75
		if (\OCP\Util::needUpgrade()) {
76
			$output->writeln('Update required, skipping cron');
77
			return 1;
78
		}
79
		if ($this->config->getSystemValue('maintenance', false)) {
80
			$output->writeln('We are in maintenance mode, skipping cron');
81
			return 1;
82
		}
83
		if ($this->config->getSystemValue('singleuser', false)) {
84
			$output->writeln('We are in admin only mode, skipping cron');
85
			return 1;
86
		}
87
88
		// clean the temp folder
89
		$this->tempManager->cleanOld();
90
91
		// Exit if background jobs are disabled!
92
		$appMode = $this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax');
93
		if ($appMode === 'none') {
94
			$output->writeln('Background Jobs are disabled!');
95
			return 1;
96
		}
97
98
		// We call ownCloud from the CLI (aka cron)
99
		if ($appMode !== 'cron') {
100
			$this->config->setAppValue('core', 'backgroundjobs_mode', 'cron');
101
		}
102
103
		$progress = new ProgressBar($output);
104
105
		// We only ask for jobs for 14 minutes, because after 15 minutes the next
106
		// system cron task should spawn.
107
		$endTime = \time() + 14 * 60;
108
109
		$executedJobs = [];
110
		while ($job = $this->jobList->getNext()) {
111
			if (isset($executedJobs[$job->getId()])) {
112
				$this->jobList->unlockJob($job);
113
				break;
114
			}
115
			$progress->advance();
116
			$jobName = \get_class($job);
117
			$progress->setMessage("Executing: {$job->getId()} - {$jobName}");
118
119
			$job->execute($this->jobList, $this->logger);
120
121
			// clean up after unclean jobs
122
			\OC_Util::tearDownFS();
123
124
			$this->jobList->setLastJob($job);
125
			$executedJobs[$job->getId()] = true;
126
			unset($job);
127
128
			if (\time() > $endTime) {
129
				break;
130
			}
131
		}
132
133
		// Log the successful cron execution
134
		if ($this->config->getSystemValue('cron_log', true)) {
135
			$this->config->setAppValue('core', 'lastcron', \time());
136
		}
137
		$output->writeln('');
138
139
		return 0;
140
	}
141
}
142