TYPO3  7.6
SchedulerCliController.php
Go to the documentation of this file.
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 
18 
23 {
27  protected $cli;
28 
32  protected $hasTask = true;
33 
37  protected $scheduler;
38 
42  public function __construct()
43  {
44  $this->cli = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Controller\CommandLineController::class);
45  $this->scheduler = GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\Scheduler::class);
46 
47  // Empty options array here because Scheduler uses "-s" as argument too
48  $this->cli->cli_options = array();
49  $this->cli->cli_options[] = array('-h', 'Show this output');
50  $this->cli->cli_options[] = array('--help', 'Same as -h');
51  $this->cli->cli_options[] = array('-s', 'Stop the task which is passed with -i option');
52  $this->cli->cli_options[] = array('-i', 'UID of an task');
53  $this->cli->cli_options[] = array('-f', 'Force execution of the task which is passed with -i option');
54 
55  // Setting help texts:
56  $this->cli->cli_help['name'] = 'scheduler -- Start the TYPO3 Scheduler from the command line';
57  $this->cli->cli_help['synopsis'] = '###OPTIONS###';
58  $this->cli->cli_help['description'] = 'This command line starts any task';
59  $this->cli->cli_help['examples'] = 'typo3/cli_dispatch.phpsh scheduler';
60  unset($this->cli->cli_help['author']);
61  }
62 
68  protected function isHelp()
69  {
70  return ($this->cli->cli_isArg('--help') && $this->cli->cli_isArg('--help') > 0)
71  || ($this->cli->cli_isArg('-h') && $this->cli->cli_isArg('-h') > 0);
72  }
73 
74 
78  public function run()
79  {
80  if ($this->isHelp()) {
81  $this->cli->cli_help();
82  return;
83  }
84 
85  if ($this->cli->cli_isArg('-i') && $this->cli->cli_isArg('-i') > 0) {
87  $task = $this->getTask();
88  if ($this->scheduler->isValidTaskObject($task)) {
89  if ($this->cli->cli_isArg('-s')) {
90  $this->stopTask($task);
91  } else {
92  $this->scheduler->executeTask($task);
93  }
94 
95  // Record the run in the system registry
96  $this->scheduler->recordLastRun('cli-by-id');
97  }
98  return;
99  }
100  $this->loopTasks();
101  }
102 
108  protected function stopTask($task)
109  {
110  if ($this->scheduler->isValidTaskObject($task)) {
111  $result = $task->unmarkAllExecutions();
112  }
113  }
114 
120  protected function getTask()
121  {
122  $taskId = (int)$this->cli->cli_argValue('-i');
123 
124  if ($this->cli->cli_isArg('-f') || $this->cli->cli_isArg('-s')) {
125  $task = $this->scheduler->fetchTask($taskId);
126  } else {
127  $whereClause = 'uid = ' . $taskId . ' AND nextexecution != 0 AND nextexecution <= ' . $GLOBALS['EXEC_TIME'];
128  list($task) = $this->scheduler->fetchTasksWithCondition($whereClause);
129  }
130 
131  return $task;
132  }
133 
137  protected function loopTasks()
138  {
139  do {
140  // Try getting the next task and execute it
141  // If there are no more tasks to execute, an exception is thrown by \TYPO3\CMS\Scheduler\Scheduler::fetchTask()
142  try {
144  $task = $this->scheduler->fetchTask();
145  try {
146  $this->scheduler->executeTask($task);
147  } catch (\Exception $e) {
148  // We ignore any exception that may have been thrown during execution,
149  // as this is a background process.
150  // The exception message has been recorded to the database anyway
151  continue;
152  }
153  } catch (\OutOfBoundsException $e) {
154  $this->hasTask = false;
155  } catch (\UnexpectedValueException $e) {
156  continue;
157  }
158  } while ($this->hasTask);
159  // Record the run in the system registry
160  $this->scheduler->recordLastRun();
161  }
162 }