TYPO3 CMS  TYPO3_7-6
SchedulerCliController.php
Go to the documentation of this file.
1 <?php
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 = [];
49  $this->cli->cli_options[] = ['-h', 'Show this output'];
50  $this->cli->cli_options[] = ['--help', 'Same as -h'];
51  $this->cli->cli_options[] = ['-s', 'Stop the task which is passed with -i option'];
52  $this->cli->cli_options[] = ['-i', 'UID of an task'];
53  $this->cli->cli_options[] = ['-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 
77  public function run()
78  {
79  if ($this->isHelp()) {
80  $this->cli->cli_help();
81  return;
82  }
83 
84  if ($this->cli->cli_isArg('-i') && $this->cli->cli_isArg('-i') > 0) {
86  $task = $this->getTask();
87  if ($this->scheduler->isValidTaskObject($task)) {
88  if ($this->cli->cli_isArg('-s')) {
89  $this->stopTask($task);
90  } else {
91  $this->scheduler->executeTask($task);
92  }
93 
94  // Record the run in the system registry
95  $this->scheduler->recordLastRun('cli-by-id');
96  }
97  return;
98  }
99  $this->loopTasks();
100  }
101 
107  protected function stopTask($task)
108  {
109  if ($this->scheduler->isValidTaskObject($task)) {
110  $result = $task->unmarkAllExecutions();
111  }
112  }
113 
119  protected function getTask()
120  {
121  $taskId = (int)$this->cli->cli_argValue('-i');
122 
123  if ($this->cli->cli_isArg('-f') || $this->cli->cli_isArg('-s')) {
124  $task = $this->scheduler->fetchTask($taskId);
125  } else {
126  $whereClause = 'uid = ' . $taskId . ' AND nextexecution != 0 AND nextexecution <= ' . $GLOBALS['EXEC_TIME'];
127  list($task) = $this->scheduler->fetchTasksWithCondition($whereClause);
128  }
129 
130  return $task;
131  }
132 
136  protected function loopTasks()
137  {
138  do {
139  // Try getting the next task and execute it
140  // If there are no more tasks to execute, an exception is thrown by \TYPO3\CMS\Scheduler\Scheduler::fetchTask()
141  try {
143  $task = $this->scheduler->fetchTask();
144  try {
145  $this->scheduler->executeTask($task);
146  } catch (\Exception $e) {
147  // We ignore any exception that may have been thrown during execution,
148  // as this is a background process.
149  // The exception message has been recorded to the database anyway
150  continue;
151  }
152  } catch (\OutOfBoundsException $e) {
153  $this->hasTask = false;
154  } catch (\UnexpectedValueException $e) {
155  continue;
156  }
157  } while ($this->hasTask);
158  // Record the run in the system registry
159  $this->scheduler->recordLastRun();
160  }
161 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']