2. PRESENTATION ROADMAP
WHAT IS SCHEDULER?
SCHEDULER FOR USERS
SCHEDULER FOR DEVELOPERS
WHAT IS CLI?
CLI FOR USERS
CLI FOR DEVELOPER
INTERESTING EXTENSIONS
https://github.com/t33k/schedulerX
Krystian Szymukowicz
k.szymukowicz@sourcebroker.net
4. 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
11. SETTING SCHEDULER MAIN CRONJOB
EDIT USERS CRONTABS.
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
EDIT SYSTEM WIDE CRONTABS.
Go to /etc/cron.d/ and create file
*/15 * * * * www-data php /var/www/workspace-typo3/projects/62/typo3/cli_dispatch.phpsh scheduler
12. SETTING SCHEDULER MAIN CRONJOB
Different problems on limited ssh and shared hostings:
• 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.
13. SETTING SCHEDULER MAIN CRONJOB
General fake cron problem overcome:
<php
exec('/usr/local/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');
Different hosting - different variations
• Hoster - ALL-INKL
<php
exec('php /var/www/workspace-typo3/projects/62/typo3/cli_dispatch.phpsh scheduler');
AddHandler php5-cgi .php .phpsh
/cron/cron.php
/cron/.htaccess
/fileadmin/
/typo3/
/typo3conf
/etc…
14. SETTING SCHEDULER MAIN CRONJOB
Be ready to fight for Scheduler cron!
Google for:
"typo3 cli_dispatch.phpsh [hoster name]"
Do not hesitate to ask hoster admin!
24. SIMPLE EXT WITH SCHEDULER TASK
Class with task:
Classes/Task/SampleTask.php
<?php
!
namespace VScheduler1Task;
class SampleTask extends TYPO3CMSSchedulerTaskAbstractTask {
!
public function execute() {
$success = FALSE;
…
…
return $success;
}
!
}
https://github.com/t33k/scheduler1
25.
26.
27.
28. SIMPLE EXT WITH SCHEDULER TASK II
https://github.com/t33k/scheduler2
getAdditonalInformation()
getProgress()
implements TYPO3CMSSchedulerProgressProviderInterface()
29. SIMPLE EXT WITH SCHEDULER TASK II
Classes/Task/SampleTask.php
<?php
!
namespace VScheduler2Task;
!
class SampleTask extends TYPO3CMSSchedulerTaskAbstractTask
implements TYPO3CMSSchedulerProgressProviderInterface {
!
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);
}
!
}
https://github.com/t33k/scheduler2
30. SIMPLE EXT WITH SCHEDULER TASK III
Flash messages and debug:
System flash messages
Standard scheduler task info
GeneralUtility::devLog(…..
debug(ArrayUtility::convertObjectToArray($this));
31. SIMPLE EXT WITH SCHEDULER TASK III
Classes/Task/SampleTask.php
<?php
!
namespace VScheduler3Task;
use TYPO3CMSCoreUtilityGeneralUtility;
use TYPO3CMSExtbaseUtilityArrayUtility;
!
class SampleTask extends TYPO3CMSSchedulerTaskAbstractTask{
public function execute() {
$flashMessageOk = GeneralUtility::makeInstance(
'TYPO3CMSCoreMessagingFlashMessage',
'Message to be passed',
'OK Example',
TYPO3CMSCoreMessagingFlashMessage::OK);
$defaultFlashMessageQueue = $this->getDefaultFlashMessageQueue();
$defaultFlashMessageQueue->enqueue($flashMessageOk);
!
GeneralUtility::devLog('Message', 'scheduler3', 2, ArrayUtility::convertObjectToArray($this));
!
debug(ArrayUtility::convertObjectToArray($this));
return TRUE;
}
!
}
https://github.com/t33k/scheduler3
32. SIMPLE EXT WITH SCHEDULER TASK IV
Additional field https://github.com/t33k/scheduler4
33. SIMPLE EXT WITH SCHEDULER TASK IV
Additional field
ext_localconf.php
Classes/Task/SampleTaskAdditionalFieldProvider.php
!
class SampleTaskAdditionalFieldProvider implements TYPO3CMSSchedulerAdditionalFieldProviderInterface{
!
public function getAdditionalFields
(array &$taskInfo, $task, TYPO3CMSSchedulerControllerSchedulerModuleController $parentObject) {…}
!
public function validateAdditionalFields
(array &$submittedData, TYPO3CMSSchedulerControllerSchedulerModuleController $parentObject) {…}
!
public function saveAdditionalFields
(array $submittedData, TYPO3CMSSchedulerTaskAbstractTask $task) {…}
}
https://github.com/t33k/scheduler4
<?php
!
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['VScheduler4TaskSampleTask'] =
array(
'extension' => $_EXTKEY,
'title' => 'Scheduler4 test - example for new field',
'description' => 'How to add new field to scheduler task.',
'additionalFields' => 'VScheduler4TaskSampleTaskAdditionalFieldProvider'
);
34. SIMPLE EXT WITH SCHEDULER TASK V
Create new tasks automatically in FE and BE
https://github.com/t33k/scheduler5
Classes/Task/SampleTask.php
<?php
!
namespace VScheduler5Task;
use TYPO3CMSCoreUtilityGeneralUtility;
!
class SampleTask extends TYPO3CMSSchedulerTaskAbstractTask {
!
public function execute() {
/** @var $newTask VScheduler5TaskSampleTask */
$newTask = GeneralUtility::makeInstance('VScheduler5TaskSampleTask');
$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 TYPO3CMSSchedulerScheduler $scheduler */
$scheduler = GeneralUtility::makeInstance("TYPO3CMSSchedulerScheduler");
$scheduler->addTask($newTask);
!
return TRUE;
}
!
public function getAdditionalInformation() {
return 'Email:' . $this->email;
}
!
}
35. SIMPLE EXT WITH SCHEDULER TASK VII
Autmaticaly disable task
Classes/Task/SampleTask.php
<?php
!
namespace VScheduler7Task;
!
class SampleTask extends TYPO3CMSSchedulerTaskAbstractTask {
!
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;
}
!
}
36. THINGS TO REMEMBER
Object 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.
38. CLI
Command Line Interface
!
One of SAPI the PHP interact with different env - here with
shell. Others SAPI examples: CGI, fpm , apache2handler
CLI points:
• No execution time limit by default.
• No headers are send.
• No path is changed while running by default
45. EXTBASE COMMAND CENTER
Backport from TYPO3 FLOW
$ php cli_dispatch.phpsh extbase <command identifier> --argumentName=value
$ 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)
46. ext_localconf.php
<?php
!
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = 'VScheduler6Command
SampleCommandController';
typo3conf/ext/scheduler6/Classes/Command/SampleCommandController.php
/**
* Class SampleCommandController
* @package VScheduler6Command
*/
class SampleCommandController extends TYPO3CMSExtbaseMvcControllerCommandController { !
/**
* 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());
}
}
}
48. EXTBASE COMMAND CENTER
ext_localconf.php
<?php
!
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = 'VScheduler6Command
SampleCommandController';
typo3conf/ext/scheduler6/Classes/Command/SampleCommandController.php
/**
* Class SampleCommandController
* @package VScheduler6Command
*/
class SampleCommandController extends TYPO3CMSExtbaseMvcControllerCommandController { !
/**
* 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());
}
}
}