‪TYPO3CMS  ‪main
AbstractTask.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
26 
32 abstract class ‪AbstractTask implements LoggerAwareInterface
33 {
34  use LoggerAwareTrait;
35 
36  public const ‪TYPE_SINGLE = 1;
37  public const ‪TYPE_RECURRING = 2;
38 
44  protected ‪$scheduler;
45 
49  protected int ‪$taskUid = 0;
50 
56  protected ‪$disabled = false;
57 
63  protected ‪$runOnNextCronJob = false;
64 
70  protected ‪$execution;
71 
77  protected ‪$executionTime = 0;
78 
82  protected string ‪$description = '';
83 
89  protected ‪$taskGroup = 0;
90 
94  public function ‪__construct()
95  {
96  // Using makeInstance instead of setScheduler() here as the logger is injected due to LoggerAwareTrait
97  $this->scheduler = GeneralUtility::makeInstance(Scheduler::class);
98  $this->execution = GeneralUtility::makeInstance(Execution::class);
99  }
100 
110  abstract public function ‪execute();
111 
120  public function ‪getAdditionalInformation()
121  {
122  return '';
123  }
124 
130  public function ‪setTaskUid($id): void
131  {
132  $this->taskUid = (int)$id;
133  }
134 
140  public function ‪getTaskUid(): int
141  {
142  return ‪$this->taskUid;
143  }
144 
150  public function ‪getTaskTitle()
151  {
152  return $this->‪getLanguageService()->sL(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][static::class]['title'] ?? '');
153  }
154 
160  public function ‪getTaskDescription()
161  {
162  return $this->‪getLanguageService()->sL(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][static::class]['description'] ?? '');
163  }
164 
170  public function ‪getTaskClassName()
171  {
172  return static::class;
173  }
174 
180  public function ‪isDisabled()
181  {
182  return ‪$this->disabled;
183  }
184 
190  public function ‪setDisabled($flag)
191  {
192  if ($flag) {
193  $this->disabled = true;
194  } else {
195  $this->disabled = false;
196  }
197  }
198 
204  public function ‪setRunOnNextCronJob($flag)
205  {
206  $this->runOnNextCronJob = $flag;
207  }
208 
214  public function ‪getRunOnNextCronJob()
215  {
217  }
218 
224  public function ‪setExecutionTime($timestamp)
225  {
226  $this->executionTime = (int)$timestamp;
227  }
228 
234  public function ‪getTaskGroup()
235  {
236  return ‪$this->taskGroup;
237  }
238 
244  public function ‪setTaskGroup(‪$taskGroup)
245  {
246  $this->taskGroup = (int)‪$taskGroup;
247  }
248 
254  public function ‪getExecutionTime()
255  {
257  }
258 
264  public function ‪setDescription(‪$description): void
265  {
266  $this->‪description = (string)‪$description;
267  }
268 
274  public function ‪getDescription()
275  {
276  return ‪$this->description;
277  }
278 
284  public function ‪setScheduler()
285  {
286  $this->scheduler = GeneralUtility::makeInstance(Scheduler::class);
287  $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
288  }
289 
297  public function ‪unsetScheduler()
298  {
299  $this->scheduler = null;
300  unset($this->logger);
301  }
302 
309  public function ‪registerSingleExecution($timestamp)
310  {
311  ‪$execution = GeneralUtility::makeInstance(Execution::class);
312  ‪$execution->‪setStart($timestamp);
314  ‪$execution->‪setEnd($timestamp);
318  // Replace existing execution object
319  $this->execution = ‪$execution;
320  }
321 
332  public function ‪registerRecurringExecution($start, $interval, $end = 0, $multiple = false, $cron_cmd = '')
333  {
334  ‪$execution = GeneralUtility::makeInstance(Execution::class);
335  // Set general values
336  ‪$execution->‪setStart($start);
337  ‪$execution->‪setEnd($end);
338  ‪$execution->‪setMultiple($multiple);
339  if (empty($cron_cmd)) {
340  // Use interval
341  ‪$execution->‪setInterval($interval);
343  } else {
344  // Use cron syntax
346  ‪$execution->‪setCronCmd($cron_cmd);
347  }
348  // Replace existing execution object
349  $this->execution = ‪$execution;
350  }
351 
358  public function ‪setExecution(Execution ‪$execution)
359  {
360  $this->execution = ‪$execution;
361  }
362 
369  public function ‪getExecution()
370  {
371  return ‪$this->execution;
372  }
373 
380  public function ‪getNextDueExecution()
381  {
382  // NOTE: this call may throw an exception, but we let it bubble up
383  return $this->execution->getNextExecution();
384  }
385 
392  public function ‪areMultipleExecutionsAllowed()
393  {
394  return $this->execution->getMultiple();
395  }
396 
403  public function ‪save()
404  {
405  return GeneralUtility::makeInstance(SchedulerTaskRepository::class)->update($this);
406  }
407 
413  public function ‪stop()
414  {
415  $this->execution = GeneralUtility::makeInstance(Execution::class);
416  }
417 
424  public function ‪getType()
425  {
426  if (!empty($this->‪getExecution()->getInterval()) || !empty($this->‪getExecution()->getCronCmd())) {
428  }
429  return ‪self::TYPE_SINGLE;
430  }
431 
432  protected function ‪logException(\‪Exception $e)
433  {
434  $this->logger->error('A Task Exception was captured.', ['exception' => $e]);
435  }
436 
437  protected function ‪getLanguageService(): ?‪LanguageService
438  {
439  return ‪$GLOBALS['LANG'] ?? null;
440  }
441 }
‪TYPO3\CMS\Scheduler\Task\AbstractTask\$scheduler
‪TYPO3 CMS Scheduler Scheduler null $scheduler
Definition: AbstractTask.php:43
‪TYPO3\CMS\Scheduler\Execution\setStart
‪setStart($start)
Definition: Execution.php:73
‪TYPO3\CMS\Scheduler\Task\AbstractTask\save
‪bool save()
Definition: AbstractTask.php:397
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getTaskUid
‪int getTaskUid()
Definition: AbstractTask.php:134
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getRunOnNextCronJob
‪bool getRunOnNextCronJob()
Definition: AbstractTask.php:208
‪TYPO3\CMS\Scheduler\Task\AbstractTask\setTaskUid
‪setTaskUid($id)
Definition: AbstractTask.php:124
‪TYPO3\CMS\Scheduler\Execution\setCronCmd
‪setCronCmd($cmd)
Definition: Execution.php:153
‪TYPO3\CMS\Scheduler\Execution\setIsNewSingleExecution
‪setIsNewSingleExecution($isNewSingleExecution)
Definition: Execution.php:180
‪TYPO3\CMS\Scheduler\Domain\Repository\SchedulerTaskRepository
Definition: SchedulerTaskRepository.php:38
‪TYPO3\CMS\Scheduler\Task
Definition: AbstractTask.php:16
‪TYPO3\CMS\Scheduler\Task\AbstractTask\setRunOnNextCronJob
‪setRunOnNextCronJob($flag)
Definition: AbstractTask.php:198
‪TYPO3\CMS\Scheduler\Task\AbstractTask\setDisabled
‪setDisabled($flag)
Definition: AbstractTask.php:184
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getType
‪int getType()
Definition: AbstractTask.php:418
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getLanguageService
‪getLanguageService()
Definition: AbstractTask.php:431
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getTaskTitle
‪string getTaskTitle()
Definition: AbstractTask.php:144
‪TYPO3\CMS\Scheduler\Task\AbstractTask\$taskGroup
‪int null $taskGroup
Definition: AbstractTask.php:83
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getNextDueExecution
‪int getNextDueExecution()
Definition: AbstractTask.php:374
‪TYPO3\CMS\Scheduler\Task\AbstractTask\$executionTime
‪int $executionTime
Definition: AbstractTask.php:72
‪TYPO3\CMS\Scheduler\Task\AbstractTask\$execution
‪Execution $execution
Definition: AbstractTask.php:66
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getExecution
‪Execution object null getExecution()
Definition: AbstractTask.php:363
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getTaskClassName
‪string getTaskClassName()
Definition: AbstractTask.php:164
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getAdditionalInformation
‪string getAdditionalInformation()
Definition: AbstractTask.php:114
‪TYPO3\CMS\Scheduler\Task\AbstractTask\setScheduler
‪setScheduler()
Definition: AbstractTask.php:278
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:33
‪TYPO3\CMS\Scheduler\Execution
Definition: Execution.php:26
‪TYPO3\CMS\Scheduler\Task\AbstractTask\registerSingleExecution
‪registerSingleExecution($timestamp)
Definition: AbstractTask.php:303
‪TYPO3\CMS\Scheduler\Scheduler
Definition: Scheduler.php:34
‪TYPO3\CMS\Scheduler\Execution\setInterval
‪setInterval($interval)
Definition: Execution.php:113
‪TYPO3\CMS\Scheduler\Task\AbstractTask\logException
‪logException(\Exception $e)
Definition: AbstractTask.php:426
‪TYPO3\CMS\Scheduler\Task\AbstractTask\isDisabled
‪bool isDisabled()
Definition: AbstractTask.php:174
‪TYPO3\CMS\Scheduler\Task\AbstractTask\setDescription
‪setDescription($description)
Definition: AbstractTask.php:258
‪TYPO3\CMS\Scheduler\Task\AbstractTask\areMultipleExecutionsAllowed
‪bool areMultipleExecutionsAllowed()
Definition: AbstractTask.php:386
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getExecutionTime
‪int getExecutionTime()
Definition: AbstractTask.php:248
‪TYPO3\CMS\Scheduler\Execution\setEnd
‪setEnd($end)
Definition: Execution.php:93
‪TYPO3\CMS\Scheduler\Exception
Definition: Exception.php:25
‪TYPO3\CMS\Scheduler\Task\AbstractTask\$runOnNextCronJob
‪bool $runOnNextCronJob
Definition: AbstractTask.php:60
‪TYPO3\CMS\Scheduler\Task\AbstractTask\$description
‪string $description
Definition: AbstractTask.php:77
‪TYPO3\CMS\Scheduler\Task\AbstractTask\TYPE_RECURRING
‪const TYPE_RECURRING
Definition: AbstractTask.php:37
‪TYPO3\CMS\Scheduler\Task\AbstractTask\stop
‪stop()
Definition: AbstractTask.php:407
‪TYPO3\CMS\Scheduler\Task\AbstractTask\execute
‪bool execute()
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Scheduler\Task\AbstractTask\TYPE_SINGLE
‪const TYPE_SINGLE
Definition: AbstractTask.php:36
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getTaskGroup
‪int null getTaskGroup()
Definition: AbstractTask.php:228
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Scheduler\Task\AbstractTask\__construct
‪__construct()
Definition: AbstractTask.php:88
‪TYPO3\CMS\Scheduler\Task\AbstractTask\registerRecurringExecution
‪registerRecurringExecution($start, $interval, $end=0, $multiple=false, $cron_cmd='')
Definition: AbstractTask.php:326
‪TYPO3\CMS\Scheduler\Task\AbstractTask\$disabled
‪bool $disabled
Definition: AbstractTask.php:54
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Redirects\Message\description
‪identifier description
Definition: RedirectWasHitMessage.php:32
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getDescription
‪string getDescription()
Definition: AbstractTask.php:268
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Scheduler\Task\AbstractTask\setExecution
‪setExecution(Execution $execution)
Definition: AbstractTask.php:352
‪TYPO3\CMS\Scheduler\Execution\setMultiple
‪setMultiple($multiple)
Definition: Execution.php:133
‪TYPO3\CMS\Scheduler\Task\AbstractTask\unsetScheduler
‪unsetScheduler()
Definition: AbstractTask.php:291
‪TYPO3\CMS\Scheduler\Task\AbstractTask\$taskUid
‪int $taskUid
Definition: AbstractTask.php:48
‪TYPO3\CMS\Scheduler\Task\AbstractTask\setTaskGroup
‪setTaskGroup($taskGroup)
Definition: AbstractTask.php:238
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getTaskDescription
‪string getTaskDescription()
Definition: AbstractTask.php:154
‪TYPO3\CMS\Scheduler\Task\AbstractTask\setExecutionTime
‪setExecutionTime($timestamp)
Definition: AbstractTask.php:218