typo3 scheduler

Post on 10-Jul-2015

934 Views

Category:

Internet

6 Downloads

Preview:

Click to see full reader

DESCRIPTION

TYPO3 Scheduler - presentations from typo3camp.pl

TRANSCRIPT

SCHEDULER & CLI

Krystian Szymukowiczk.szymukowicz@sourcebroker.net

PRESENTATION ROADMAP

WHAT IS SCHEDULER?SCHEDULER FOR USERS

SCHEDULER FOR DEVELOPERS

WHAT IS CLI?CLI FOR USERS

CLI FOR DEVELOPER

INTERESTING EXTENSIONS

Krystian Szymukowiczk.szymukowicz@sourcebroker.net

https://github.com/t33k/schedulerX

SCHEDULERmodule to set up and monitor recurring things

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

SCHEDULERUSER/INTEGRATOR PERSPECTIVE

NOT INSTALLED BY DEFAULT

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.

• 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

<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');

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!

SUCCESS

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

SCHEDULERDEVELOPER PERSPECTIVE

SIMPLE EXT WITH SCHEDULER TASK !

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

https://github.com/t33k/scheduler1

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

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

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

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

getAdditonalInformation()getProgress()

implements \TYPO3\CMS\Scheduler\ProgressProviderInterface()

<?php !namespace V\Scheduler2\Task; !class SampleTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask implements \TYPO3\CMS\Scheduler\ProgressProviderInterface { ! public $email = 'dr_who@universe.thr'; ! 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

SIMPLE EXT WITH SCHEDULER TASK III

Standard scheduler task info

System flash messages

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

Flash messages and debug:

GeneralUtility::devLog(…..

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

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

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

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

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

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.

CLIUSER PERSPECTIVE

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:

CLI Command Line Interface

php typo3/cli_dispatch.phpsh

lowlevel_admin setBElock

lowlevel_cleaner [option] -r

lowlevel_refindex

extbase

CLIDEVELOPER PERSPECTIVE

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

/** * 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

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

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

EXTBASE COMMAND CENTER

SCHEDULER/CLIINTERESTING EXTENSIONS

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

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

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

THANK YOU!

Krystian Szymukowiczk.szymukowicz@sourcebroker.net

top related