TYPO3 CMS  TYPO3_8-7
SchedulerCommand.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 
25 
29 class SchedulerCommand extends Command
30 {
34  protected $hasTask = true;
35 
39  protected $scheduler;
40 
44  public function configure()
45  {
46  $this
47  ->setDescription('Start the TYPO3 Scheduler from the command line.')
48  ->setHelp('If no parameter is given, the scheduler executes any tasks that are overdue to run.
49 Call it like this: typo3/sysext/core/bin/typo3 scheduler:run --task=13 -f')
50  ->addOption(
51  'task',
52  'i',
53  InputOption::VALUE_REQUIRED,
54  'UID of a specific task'
55  )
56  ->addOption(
57  'force',
58  'f',
59  InputOption::VALUE_NONE,
60  'Force execution of the task which is passed with -i option'
61  )
62  ->addOption(
63  'stop',
64  's',
65  InputOption::VALUE_NONE,
66  'Stop the task which is passed with -i option'
67  );
68  }
69 
76  public function execute(InputInterface $input, OutputInterface $output)
77  {
78  // Make sure the _cli_ user is loaded
79  Bootstrap::getInstance()->initializeBackendAuthentication();
80 
81  $this->scheduler = GeneralUtility::makeInstance(Scheduler::class);
82 
83  if ((int)$input->getOption('task') > 0) {
84  $taskUid = (int)$input->getOption('task');
85  $stopTask = (bool)($input->hasOption('stop') && $input->getOption('stop'));
86  $force = (bool)($input->hasOption('force') && $input->getOption('force'));
87  $task = $this->getTask($taskUid, $stopTask || $force);
88 
89  if ($this->scheduler->isValidTaskObject($task)) {
90  if ($stopTask) {
91  $this->stopTask($task);
92  } else {
93  $this->scheduler->executeTask($task);
94  }
95  }
96  // Record the run in the system registry
97  $this->scheduler->recordLastRun('cli-by-id');
98  } else {
99  $this->loopTasks();
100  }
101  }
102 
108  protected function stopTask($task)
109  {
110  if ($this->scheduler->isValidTaskObject($task)) {
111  $task->unmarkAllExecutions();
112  }
113  }
114 
122  protected function getTask(int $taskUid, bool $force)
123  {
124  if ($force) {
125  $task = $this->scheduler->fetchTask($taskUid);
126  } else {
127  $whereClause = 'uid = ' . (int)$taskUid . ' 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 {
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 }
execute(InputInterface $input, OutputInterface $output)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']