The new scheduler. What is it? When do I need it?
Sunday, March 14th, 2010 | Author: Benni
TYPO3 4.3 brought a lot of cool stuff with its release in November 2009. Last week, I made use of one of them for the first time: The scheduler.
Some time ago, Kasper introduced a way to access TYPO3 from the command line (CLI). This allowed us to use the command line, and thus OS-internal applications like “cron” to schedule TYPO3 tasks that were executed in a recurring pattern, or at a single particular point in the future. Kasper created this so-called “cli_dispatcher.sh” file (found in the typo3/ directory) that would delegate the executed task to extensions that were used as parameter. However, there were some drawbacks (every task needed its own BE user), and extensions like “direct_mail” still used their own CLI script. However, there was one extension developed by Christian Julle Jensen, called “gabriel” in TER, the “arch angel” that would serve as one global place to execute timed TYPO3 tasks, and every extension could hook in. However, “gabriel” wasn’t prominent enough to gain attraction so that it would be a standard for all the other extensions.
For TYPO3 4.3, Francois Suter, Ingo Renner and some others, refactored the “gabriel” extension, named it “scheduler” and integrated it into the TYPO3 Core. The new extension comes with a handy info page to see if the BE user was added, if the scheduler runs on time and shows which line to add to my crontab. Now, one can configure the scheduler through the TYPO3 Backend and doesn’t need to edit the crontab to modify the execution for every task. The crontab now only knows one entry.
/var/www/path/to/my/site/htdocs/typo3/cli_dispatch.phpsh scheduler
The dispatcher calls the scheduler, and the scheduler dispatches to the tasks that need to be run at the time of execution.
OK. cool thing, so how to set up a task?
There are three things you need to do in advance:
* Install the “scheduler” extension
* Create the CLI user (see the scheduler module “Setup check”, and click on the link to add the CLI user)
* Add the crontab to e.g. have the scheduler run every 5 minutes. Like this:
/5 * * * * /var/www/path/to/my/site/htdocs/typo3/cli_dispatch.phpsh scheduler
Please check before adding to the crontab, if the command executes correctly in your environment, maybe an additional php package (”php-cli”) is needed.
Alright, so everything is in place, but now you need to schedule the tasks, because right now the scheduler has nothing to execute. There are already two “dummy” task types, a “Scheduler Test Task” and a “Scheduler Sleep Task”, which both don’t do anything useful.
There are already a few extensions that use the scheduler as a common CLI task center. Direct Mail creates a new “task type” so the sending process of Newsletters is taken care of through the scheduler and configuring Direct Mail just got one step easier :-). Try it out.
OK. Let’s take it one step further: I will show you how to create your own scheduler task type. A common issue I have to face with quite often is that some data import needs to happen every night at 1am. As usual, I create a new, local extension for that. This extension needs to be configured in three places.
1) ext_localconf.php
The scheduler needs to know that there is a new task type, and needs to know where to delegate it to. For this, a new array is created.
$TYPO3_CONF_VARS['SC_OPTIONS']['scheduler']['tasks']['tx_benniext_scheduler_importtask'] = array(
‘extension’ => ‘benniext’,
‘title’ => ‘Bennis Importer Task’,
‘description’ => ‘Imports my specific data every night (or that is what I hope it does for me)’
);
Where the key of this associative array (tx_benniext_scheduler_importtask) is the class that is used fot the task.
2) The new PHP class, which I put in my extension directory and call it “class.tx_benniext_scheduler_importtask.php”. The file contains a class which subclasses the PHP scheduler task class and has a public function called “execute()”.
class tx_benniext_scheduler_importtask extends tx_scheduler_Task {
/**
* Function executed from the Scheduler.
*
* @return bool
*/
public function execute() {
// state if the execution went well
return true;
}
}
3) ext_autoload.php
Of course, the new file above needs to be found, and the new autoloader that also comes with TYPO3 4.3, is taking care of it as well.
return array(
'tx_benniext_scheduler_importtask' => t3lib_extMgm::extPath('benniext', 'class.tx_benniext_scheduler_importtask.php'),
);
If you’re creating a new extension, don’t forget to have an ext_icon.gif and ext_emconf.php in your extension directory.
The new task type is in place, and we can add a specific task, namely to execute this import task every day at 1am. This is done through the scheduler module by clicking on the “add task” button. Select the new “Bennis Importer Task” and add that it should be executed every night at 1am and that there is an interval of 86400 (60secs * 60mins * 24hs = 1 day = 86400 secs) seconds entered. Save the new task, and that’s it! Enjoy!
By the way: The scheduler module shows you if your task has been executed correctly on the next day.
Thanks for everyone involved in this feature. It really is so easy to use and extend. Thank you. The only thing I found confusing is the difference between these different task types, and a specific task I can create, but now I know ;-).
Category: TYPO3 | 7 Comments



