Download - TYPO3 Scheduler

Transcript
Page 2: TYPO3 Scheduler

PRESENTATION ROADMAP

WHAT IS SCHEDULER?SCHEDULER FOR USERS

SCHEDULER FOR DEVELOPERS

WHAT IS CLI?CLI FOR USERS

CLI FOR DEVELOPER

INTERESTING EXTENSIONS

Krystian [email protected]

https://github.com/t33k/schedulerX

Page 3: TYPO3 Scheduler

SCHEDULERmodule to set up and monitor recurring things

Page 4: TYPO3 Scheduler

WHY DO WE NEED SCHEDULER?• system extensions and user extensions do not have to

repeat the code • TYPO3 installation is better movable between hostings • gives overview of what scheduler jobs are there in system • gives overview of what jobs are currently active/running

Page 5: TYPO3 Scheduler

SCHEDULERUSER/INTEGRATOR PERSPECTIVE

Page 6: TYPO3 Scheduler

NOT INSTALLED BY DEFAULT

Page 7: TYPO3 Scheduler
Page 8: TYPO3 Scheduler
Page 9: TYPO3 Scheduler
Page 10: TYPO3 Scheduler
Page 11: TYPO3 Scheduler

SETTING SCHEDULER MAIN CRONJOB

When logged to ssh console as www-data user: crontab -e */15 * * * * php /var/www/workspace-typo3/projects/62/typo3/cli_dispatch.phpsh scheduler When logged to ssh console as root then probably better is to: su www-data -c”crontab -e” */15 * * * * php /var/www/workspace-typo3/projects/62/typo3/cli_dispatch.phpsh scheduler

Go to /etc/cron.d/ and create file */15 * * * * www-data php /var/www/workspace-typo3/projects/62/typo3/cli_dispatch.phpsh scheduler

EDIT USERS CRONTABS.

EDIT SYSTEM WIDE CRONTABS.

Page 12: TYPO3 Scheduler

• Fake cron to run just php script by calling Apache (no CLI) • Different PHP version for CLI (works from BE not form CLI)

• php_memory limit low for CLI (works from BE not form CLI) • No way to see errors from CLI on shared hostings.

Different problems on limited ssh and shared hostings:

SETTING SCHEDULER MAIN CRONJOB

Page 13: TYPO3 Scheduler

<php exec('/usr/local/php /var/www/workspace-typo3/projects/62/typo3/cli_dispatch.phpsh scheduler');

General fake cron problem overcome:

SETTING SCHEDULER MAIN CRONJOB

AddHandler php5-cgi .php .phpsh

Different hosting - different variations• Hoster - ALL-INKL

/cron/cron.php /cron/.htaccess /fileadmin/ /typo3/ /typo3conf /etc…

<php exec('php /var/www/workspace-typo3/projects/62/typo3/cli_dispatch.phpsh scheduler');

<php exec('/usr/local/php-cgi /var/www/workspace-typo3/projects/62/typo3/cli_dispatch.phpsh scheduler');

Page 14: TYPO3 Scheduler

Be ready to fight for Scheduler cron!

SETTING SCHEDULER MAIN CRONJOB

"typo3 cli_dispatch.phpsh [hoster name]"Google for:

Do not hesitate to ask hoster admin!

Page 15: TYPO3 Scheduler

SUCCESS

Page 16: TYPO3 Scheduler

CRON FREQUENCY

10:00 10:15 10:30 10:45

/etc/cron.d/*/15 * * * * www-data php /var/www/workspace-typo3/projects/acme/typo3/cli_dispatch.phpsh scheduler

11:00

10:0030 45 11:00

21-11-14 10:10

21-11-14 10:15

10 20 405 25 35 50 5515

Task with id=2 will be late by 10 minutes

Page 17: TYPO3 Scheduler
Page 18: TYPO3 Scheduler
Page 19: TYPO3 Scheduler
Page 20: TYPO3 Scheduler

SCHEDULERDEVELOPER PERSPECTIVE

Page 21: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK !

typo3conf/ext /Scheduler1 /Classes/Task/SimpleReportTask.php /ext_conf.php /ext_localconf.php

https://github.com/t33k/scheduler1

Page 22: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK Register new extension:

<?php !$EM_CONF[$_EXTKEY] = array( 'title' => 'Scheduler Test - The simplest extension with task', 'constraints' => array(), );

ext_conf.php

https://github.com/t33k/scheduler1

Page 23: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK Register new scheduler task:

<?php !$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['V\Scheduler1\Task\SampleTask'] = array( 'extension' => $_EXTKEY, 'title' => 'The simplest task ever', 'description' => 'Task description' );

ext_localconf.php

https://github.com/t33k/scheduler1

Page 24: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK Class with task:

<?php !namespace V\Scheduler1\Task; class SampleTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask { ! public function execute() { $success = FALSE; … … return $success; } !}

Classes/Task/SampleTask.php

https://github.com/t33k/scheduler1

Page 25: TYPO3 Scheduler
Page 26: TYPO3 Scheduler
Page 27: TYPO3 Scheduler
Page 28: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK II https://github.com/t33k/scheduler2

getAdditonalInformation()getProgress()

implements \TYPO3\CMS\Scheduler\ProgressProviderInterface()

Page 29: TYPO3 Scheduler

<?php !namespace V\Scheduler2\Task; !class SampleTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask implements \TYPO3\CMS\Scheduler\ProgressProviderInterface { ! public $email = '[email protected]'; ! public function execute() { return TRUE; } ! public function getAdditionalInformation() { return 'Time now: ' . strftime('%H:%m:%S', time()) . ', Next exec time: ' . strftime('%x', $this->getNextDueExecution()) . ', Email: ' . $this->email; } ! public function getProgress(){ return rand(0,100); } !}

Classes/Task/SampleTask.phphttps://github.com/t33k/scheduler2

SIMPLE EXT WITH SCHEDULER TASK II

Page 30: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK III

Standard scheduler task info

System flash messages

debug(ArrayUtility::convertObjectToArray($this));

Flash messages and debug:

GeneralUtility::devLog(…..

Page 31: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK III

<?php !namespace V\Scheduler3\Task; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Utility\ArrayUtility; !class SampleTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask{ public function execute() { $flashMessageOk = GeneralUtility::makeInstance( '\\TYPO3\\CMS\\Core\\Messaging\\FlashMessage', 'Message to be passed', 'OK Example', \TYPO3\CMS\Core\Messaging\FlashMessage::OK);

$defaultFlashMessageQueue = $this->getDefaultFlashMessageQueue(); $defaultFlashMessageQueue->enqueue($flashMessageOk); ! GeneralUtility::devLog('Message', 'scheduler3', 2, ArrayUtility::convertObjectToArray($this)); ! debug(ArrayUtility::convertObjectToArray($this)); return TRUE; } !}

Classes/Task/SampleTask.phphttps://github.com/t33k/scheduler3

Page 32: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK IV https://github.com/t33k/scheduler4Additional field

Page 33: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK IV

!class SampleTaskAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface{ ! public function getAdditionalFields (array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {…} ! public function validateAdditionalFields (array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {…} ! public function saveAdditionalFields (array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {…}

}

Classes/Task/SampleTaskAdditionalFieldProvider.php

https://github.com/t33k/scheduler4

<?php !$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['V\\Scheduler4\\Task\\SampleTask'] = array( 'extension' => $_EXTKEY, 'title' => 'Scheduler4 test - example for new field', 'description' => 'How to add new field to scheduler task.', 'additionalFields' => 'V\\Scheduler4\\Task\\SampleTaskAdditionalFieldProvider' );

ext_localconf.php

Additional field

Page 34: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK V https://github.com/t33k/scheduler5

Create new tasks automatically in FE and BE<?php !namespace V\Scheduler5\Task; use TYPO3\CMS\Core\Utility\GeneralUtility; !class SampleTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask { ! public function execute() { /** @var $newTask \V\Scheduler5\Task\SampleTask */ $newTask = GeneralUtility::makeInstance('\\V\\Scheduler5\\Task\\SampleTask'); $newTask->setDescription('Task description'); $newTask->setTaskGroup(0); $newTask->registerRecurringExecution($start = time(), $interval = 86400, $end = 0, $multiple = FALSE, $cron_cmd = ''); $newTask->email = 'test.drwho+' . rand(0,10) . '@gmail.com'; ! /** @var \TYPO3\CMS\Scheduler\Scheduler $scheduler */ $scheduler = GeneralUtility::makeInstance("\\TYPO3\\CMS\\Scheduler\\Scheduler"); $scheduler->addTask($newTask); ! return TRUE; } ! public function getAdditionalInformation() { return 'Email:' . $this->email; } !}

Classes/Task/SampleTask.php

Page 35: TYPO3 Scheduler

SIMPLE EXT WITH SCHEDULER TASK VII Autmaticaly disable task

<?php !namespace V\Scheduler7\Task; !class SampleTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask { ! public $counter = 10; ! public function execute() { $this->counter = $this->counter - 1; if ($this->counter === 0) { // $this->remove(); $this->setDisabled(TRUE); } $this->save(); return TRUE; } ! public function getAdditionalInformation() { return $this->counter; } !}

Classes/Task/SampleTask.php

Page 36: TYPO3 Scheduler

THINGS TO REMEMBERObject is serialized once on creation of scheduler task so all future changes to methods or properties can lead to errors. The solution then is to delete the scheduler task and create new one.

Page 37: TYPO3 Scheduler

CLIUSER PERSPECTIVE

Page 38: TYPO3 Scheduler

CLI !

One of SAPI the PHP interact with different env - here with shell. Others SAPI examples: CGI, fpm , apache2handler

• No execution time limit by default. • No headers are send. • No path is changed while running by default

Command Line Interface

CLI points:

Page 39: TYPO3 Scheduler

CLI Command Line Interface

php typo3/cli_dispatch.phpsh

Page 40: TYPO3 Scheduler

lowlevel_admin setBElock

Page 41: TYPO3 Scheduler

lowlevel_cleaner [option] -r

Page 42: TYPO3 Scheduler

lowlevel_refindex

Page 43: TYPO3 Scheduler

extbase

Page 44: TYPO3 Scheduler

CLIDEVELOPER PERSPECTIVE

Page 45: TYPO3 Scheduler

EXTBASE COMMAND CENTER

$ php cli_dispatch.phpsh extbase scheduler6:sample:second --name=adam --number=12 --enabled

This will call:—> extension scheduler6 —> class SampleCommandController —> method secondCommand typo3conf/ext/scheduler6/Classes/Command/SampleCommandController.php

secondCommand($name, $number, $enabled = true)

$ php cli_dispatch.phpsh extbase <command identifier> --argumentName=value

Backport from TYPO3 FLOW

Page 46: TYPO3 Scheduler

/** * Class SampleCommandController * @package V\Scheduler6\Command */ class SampleCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController { ! /** * Get Faq title for given uid * * This text goes into description of CLI * so you see when you will do php typo3/cli_dispatch.phpsh extbase help scheduler6:sample:newsTitle. * Second line of description. * Third line of description. * * @param integer $faqUid Faq uid * @return void */ public function faqTitleCommand($faqUid) { $faqUid = intval($faqUid); if ($faqUid) { $faqRepository = $this->getFaqRepository(); $faq = $faqRepository->findByUid($faqUid); if(NULL !== $faq){ $this->outputLine($faq->getQuestion()); } } }

typo3conf/ext/scheduler6/Classes/Command/SampleCommandController.php

<?php !$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = 'V\\Scheduler6\\Command\\SampleCommandController';

ext_localconf.php

Page 47: TYPO3 Scheduler

php typo3/cli_dispatch.phpsh extbase help scheduler6:sample:faqTitleAll texts are taken from class comment / arguments !!!

Page 48: TYPO3 Scheduler

EXTBASE COMMAND CENTER

/** * Class SampleCommandController * @package V\Scheduler6\Command */ class SampleCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController { ! /** * Get Faq title for given uid * * This text goes into description of CLI * so you see when you will do php typo3/cli_dispatch.phpsh extbase help scheduler6:sample:newsTitle. * Second line of description. * Third line of description. * * @param integer $faqUid Faq uid * @return void */ public function faqTitleCommand($faqUid) { $faqUid = intval($faqUid); if ($faqUid) { $faqRepository = $this->getFaqRepository(); $faq = $faqRepository->findByUid($faqUid); if(NULL !== $faq){ $this->outputLine($faq->getQuestion()); } } }

typo3conf/ext/scheduler6/Classes/Command/SampleCommandController.php

<?php !$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = 'V\\Scheduler6\\Command\\SampleCommandController';

ext_localconf.php

Page 49: TYPO3 Scheduler
Page 50: TYPO3 Scheduler

EXTBASE COMMAND CENTER

Page 51: TYPO3 Scheduler

SCHEDULER/CLIINTERESTING EXTENSIONS

Page 52: TYPO3 Scheduler

CLEARTYPO3CACHE$ php cli_dispatch.phpsh cleartypo3cache all$ php cli_dispatch.phpsh cleartypo3cache pages

Create BE user "_cli_cleartypo3cache" with TS settingsoptions.clearCache.all=1 options.clearCache.pages=1 options.clearCache.system=1

$ php cli_dispatch.phpsh cleartypo3cache system

https://github.com/t33k/cleartypo3cache

Page 53: TYPO3 Scheduler

CLEARTYPO3CACHE https://github.com/t33k/cleartypo3cache

Page 54: TYPO3 Scheduler

T3DEPLOYphp typo3/cli_dispatch.phpsh t3deploy database updateStructure --verbose --execute

https://github.com/AOEmedia/t3deploy

php typo3/cli_dispatch.phpsh t3deploy database updateStructure --remove --verbose --execute

Update only (more safe)

Remove also