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

CronController::run()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 0
dl 0
loc 26
rs 9.1928
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\Controller;
23
24
use OCP\AppFramework\Controller;
25
use OCP\AppFramework\Http\JSONResponse;
26
use OCP\BackgroundJob\IJobList;
27
use OCP\IConfig;
28
use OCP\IRequest;
29
use OCP\ILogger;
30
31
class CronController extends Controller {
32
	
33
	/** @var IConfig */
34
	private $config;
35
	/** @var ILogger */
36
	private $logger;
37
	/** @var IJobList */
38
	private $jobList;
39
40
	/**
41
	 * CronController constructor.
42
	 *
43
	 * @param string $appName
44
	 * @param IRequest $request
45
	 * @param IConfig $config
46
	 * @param ILogger $logger
47
	 * @param IJobList $jobList
48
	 */
49 View Code Duplication
	public function __construct($appName, IRequest $request,
50
								IConfig $config,
51
								ILogger $logger,
52
								IJobList $jobList) {
53
		parent::__construct($appName, $request);
54
		$this->config = $config;
55
		$this->logger = $logger;
56
		$this->jobList = $jobList;
57
	}
58
59
	/**
60
	 * @PublicPage
61
	 * @NoCSRFRequired
62
	 *
63
	 * @return JSONResponse
64
	 * @throws \Exception
65
	 */
66
	public function run(): JSONResponse {
67
		// Exit if background jobs are disabled!
68
		$appMode = $this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax');
69
		if ($appMode === 'none') {
70
			return new JSONResponse(['data' => ['message' => 'Background jobs disabled!']]);
71
		}
72
73
		// We call cron.php from some website
74
		if ($appMode === 'cron') {
75
			return new JSONResponse(['data' => ['message' => 'Background jobs are using system cron!']]);
76
		}
77
78
		// Work and success :-)
79
		$job = $this->jobList->getNext();
80
		if ($job !== null) {
81
			$job->execute($this->jobList, $this->logger);
82
			$this->jobList->setLastJob($job);
83
		}
84
85
		// Log the successful cron execution
86
		if ($this->config->getSystemValue('cron_log', true)) {
87
			$this->config->setAppValue('core', 'lastcron', \time());
88
		}
89
90
		return new JSONResponse();
91
	}
92
}
93