TYPO3 CMS  TYPO3_6-2
AbstractTask.php
Go to the documentation of this file.
1 <?php
3 
24 abstract class AbstractTask {
25 
31  protected $scheduler;
32 
38  protected $taskUid;
39 
45  protected $disabled = FALSE;
46 
52  protected $execution;
53 
59  protected $executionTime = 0;
60 
66  protected $description = '';
67 
73  protected $taskGroup;
74 
78  public function __construct() {
79  $this->setScheduler();
80  $this->execution = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Scheduler\\Execution');
81  }
82 
92  abstract public function execute();
93 
102  public function getAdditionalInformation() {
103  return '';
104  }
105 
112  public function setTaskUid($id) {
113  $this->taskUid = (int)$id;
114  }
115 
121  public function getTaskUid() {
122  return $this->taskUid;
123  }
124 
130  public function getTaskTitle() {
131  return $GLOBALS['LANG']->sL($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][get_class($this)]['title']);
132  }
133 
139  public function getTaskDescription() {
140  return $GLOBALS['LANG']->sL($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][get_class($this)]['description']);
141  }
142 
148  public function getTaskClassName() {
149  return get_class($this);
150  }
151 
157  public function isDisabled() {
158  return $this->disabled;
159  }
160 
167  public function setDisabled($flag) {
168  if ($flag) {
169  $this->disabled = TRUE;
170  } else {
171  $this->disabled = FALSE;
172  }
173  }
174 
181  public function setExecutionTime($timestamp) {
182  $this->executionTime = (int)$timestamp;
183  }
184 
190  public function getTaskGroup() {
191  return $this->taskGroup;
192  }
193 
200  public function setTaskGroup($taskGroup) {
201  $this->taskGroup = (int)$taskGroup;
202  }
203 
209  public function getExecutionTime() {
210  return $this->executionTime;
211  }
212 
219  public function setDescription($description) {
220  $this->description = $description;
221  }
222 
228  public function getDescription() {
229  return $this->description;
230  }
231 
237  public function setScheduler() {
238  $this->scheduler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Scheduler\\Scheduler');
239  }
240 
248  public function unsetScheduler() {
249  unset($this->scheduler);
250  }
251 
257  public function registerSingleExecution($timestamp) {
259  $execution = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Scheduler\\Execution');
260  $execution->setStart($timestamp);
261  $execution->setInterval(0);
262  $execution->setEnd($timestamp);
263  $execution->setCronCmd('');
264  $execution->setMultiple(0);
265  $execution->setIsNewSingleExecution(TRUE);
266  // Replace existing execution object
267  $this->execution = $execution;
268  }
269 
280  public function registerRecurringExecution($start, $interval, $end = 0, $multiple = FALSE, $cron_cmd = '') {
282  $execution = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Scheduler\\Execution');
283  // Set general values
284  $execution->setStart($start);
285  $execution->setEnd($end);
286  $execution->setMultiple($multiple);
287  if (empty($cron_cmd)) {
288  // Use interval
289  $execution->setInterval($interval);
290  $execution->setCronCmd('');
291  } else {
292  // Use cron syntax
293  $execution->setInterval(0);
294  $execution->setCronCmd($cron_cmd);
295  }
296  // Replace existing execution object
297  $this->execution = $execution;
298  }
299 
306  $this->execution = $execution;
307  }
308 
314  public function getExecution() {
315  return $this->execution;
316  }
317 
323  public function getNextDueExecution() {
324  // NOTE: this call may throw an exception, but we let it bubble up
325  return $this->execution->getNextExecution();
326  }
327 
333  public function areMultipleExecutionsAllowed() {
334  return $this->execution->getMultiple();
335  }
336 
342  public function isExecutionRunning() {
343  $isRunning = FALSE;
344  $queryArr = array(
345  'SELECT' => 'serialized_executions',
346  'FROM' => 'tx_scheduler_task',
347  'WHERE' => 'uid = ' . $this->taskUid,
348  'LIMIT' => 1
349  );
350  $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryArr);
351  if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
352  if (strlen($row['serialized_executions']) > 0) {
353  $isRunning = TRUE;
354  }
355  }
356  $GLOBALS['TYPO3_DB']->sql_free_result($res);
357  return $isRunning;
358  }
359 
366  public function markExecution() {
367  $queryArr = array(
368  'SELECT' => 'serialized_executions',
369  'FROM' => 'tx_scheduler_task',
370  'WHERE' => 'uid = ' . $this->taskUid,
371  'LIMIT' => 1
372  );
373  $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryArr);
374  $runningExecutions = array();
375  if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
376  if (strlen($row['serialized_executions']) > 0) {
377  $runningExecutions = unserialize($row['serialized_executions']);
378  }
379  }
380  $GLOBALS['TYPO3_DB']->sql_free_result($res);
381  // Count the number of existing executions and use that number as a key
382  // (we need to know that number, because it is returned at the end of the method)
383  $numExecutions = count($runningExecutions);
384  $runningExecutions[$numExecutions] = time();
385  // Define the context in which the script is running
386  $context = 'BE';
387  if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
388  $context = 'CLI';
389  }
390  $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_scheduler_task', 'uid = ' . $this->taskUid, array(
391  'serialized_executions' => serialize($runningExecutions),
392  'lastexecution_time' => time(),
393  'lastexecution_context' => $context
394  ));
395  return $numExecutions;
396  }
397 
405  public function unmarkExecution($executionID, \Exception $failure = NULL) {
406  // Get the executions for the task
407  $queryArr = array(
408  'SELECT' => 'serialized_executions',
409  'FROM' => 'tx_scheduler_task',
410  'WHERE' => 'uid = ' . $this->taskUid,
411  'LIMIT' => 1
412  );
413  $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryArr);
414  if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
415  if (strlen($row['serialized_executions']) > 0) {
416  $runningExecutions = unserialize($row['serialized_executions']);
417  // Remove the selected execution
418  unset($runningExecutions[$executionID]);
419  if (count($runningExecutions) > 0) {
420  // Re-serialize the updated executions list (if necessary)
421  $runningExecutionsSerialized = serialize($runningExecutions);
422  } else {
423  $runningExecutionsSerialized = '';
424  }
425  if ($failure instanceof \Exception) {
426  // Log failed execution
427  $logMessage = 'Task failed to execute successfully. Class: ' . get_class($this) . ', UID: ' . $this->taskUid . '. ' . $failure->getMessage();
428  $this->scheduler->log($logMessage, 1, $failure->getCode());
429  // Do not serialize the complete exception or the trace, this can lead to huge strings > 50MB
430  $failureString = serialize(array(
431  'code' => $failure->getCode(),
432  'message' => $failure->getMessage(),
433  'file' => $failure->getFile(),
434  'line' => $failure->getLine(),
435  'traceString' => $failure->getTraceAsString(),
436  ));
437  } else {
438  $failureString = '';
439  }
440  // Save the updated executions list
441  $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_scheduler_task', 'uid = ' . $this->taskUid, array(
442  'serialized_executions' => $runningExecutionsSerialized,
443  'lastexecution_failure' => $failureString
444  ));
445  }
446  }
447  $GLOBALS['TYPO3_DB']->sql_free_result($res);
448  }
449 
455  public function unmarkAllExecutions() {
456  // Set the serialized executions field to empty
457  $result = $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_scheduler_task', 'uid = ' . $this->taskUid, array(
458  'serialized_executions' => ''
459  ));
460  return $result;
461  }
462 
468  public function save() {
469  return $this->scheduler->saveTask($this);
470  }
471 
478  public function stop() {
479  $this->execution = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Scheduler\\Execution');
480  }
481 
487  public function remove() {
488  $this->scheduler->removeTask($this);
489  }
490 
491 }
unmarkExecution($executionID, \Exception $failure=NULL)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
setExecution(\TYPO3\CMS\Scheduler\Execution $execution)