86c3df38d02211a2bf3791a5dfecbd93e290ea32
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Controller / SchedulerCliController.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * CLI controller for the 'scheduler' extension.
21 */
22 class SchedulerCliController {
23
24 /**
25 * @var \TYPO3\CMS\Core\Controller\CommandLineController
26 */
27 protected $cli;
28
29 /**
30 * @var bool
31 */
32 protected $hasTask = TRUE;
33
34 /**
35 * @var \TYPO3\CMS\Scheduler\Scheduler
36 */
37 protected $scheduler;
38
39 /**
40 * Constructor
41 */
42 public function __construct() {
43 $this->cli = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Controller\CommandLineController::class);
44 $this->scheduler = GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\Scheduler::class);
45 }
46
47 /**
48 * Execute scheduler tasks
49 */
50 public function run() {
51 if ($this->cli->cli_isArg('-i') && $this->cli->cli_isArg('-i') > 0) {
52 /** @var $task \TYPO3\CMS\Scheduler\Task\AbstractTask */
53 $task = $this->getTask();
54 if ($this->scheduler->isValidTaskObject($task)) {
55 if ($this->cli->cli_isArg('-s')) {
56 $this->stopTask($task);
57 } else {
58 $this->scheduler->executeTask($task);
59 }
60
61 // Record the run in the system registry
62 $this->scheduler->recordLastRun('cli-by-id');
63 }
64 return;
65 }
66 $this->loopTasks();
67 }
68
69 /**
70 * Stop task
71 *
72 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task
73 */
74 protected function stopTask($task) {
75 if ($this->scheduler->isValidTaskObject($task)) {
76 $result = $task->unmarkAllExecutions();
77 }
78 }
79
80 /**
81 * Return task
82 *
83 * @return \TYPO3\CMS\Scheduler\Task\AbstractTask
84 */
85 protected function getTask() {
86 $taskId = (int)$this->cli->cli_argValue('-i');
87
88 if ($this->cli->cli_isArg('-f') || $this->cli->cli_isArg('-s')) {
89 $task = $this->scheduler->fetchTask($taskId);
90 } else {
91 $whereClause = 'uid = ' . $taskId . ' AND nextexecution != 0 AND nextexecution <= ' . $GLOBALS['EXEC_TIME'];
92 list($task) = $this->scheduler->fetchTasksWithCondition($whereClause);
93 }
94
95 return $task;
96 }
97
98 /**
99 * Execute tasks in loop
100 */
101 protected function loopTasks() {
102 do {
103 // Try getting the next task and execute it
104 // If there are no more tasks to execute, an exception is thrown by \TYPO3\CMS\Scheduler\Scheduler::fetchTask()
105 try {
106 /** @var $task \TYPO3\CMS\Scheduler\Task\AbstractTask */
107 $task = $this->scheduler->fetchTask();
108 try {
109 $this->scheduler->executeTask($task);
110 } catch (\Exception $e) {
111 // We ignore any exception that may have been thrown during execution,
112 // as this is a background process.
113 // The exception message has been recorded to the database anyway
114 continue;
115 }
116 } catch (\OutOfBoundsException $e) {
117 $this->hasTask = FALSE;
118 } catch (\UnexpectedValueException $e) {
119 continue;
120 }
121 } while ($this->hasTask);
122 // Record the run in the system registry
123 $this->scheduler->recordLastRun();
124 }
125
126 }