-
Notifications
You must be signed in to change notification settings - Fork 9
English plugin dev 3 3
Inside the MT installation, there is “$MT_DIR/tools/run-periodic-tasks”, that, well, run the periodic tasks. These do a lot of useful things, like delete old sessions.
But before we show how to add a periodic task to your plugin, we will start with warnings:
Do not make your plugin depend on the periodic tasks. A lot of sites does not run them at all.
If your plugin have to have these tasks running, please write in clearly in the documentation. (that nobody reads, btw)
Now that we are clear on the subject, lets start.
The default periodic tasks are:
- Publish scheduled entries in the designated time
- Building templates that are marked “Publish by queue” in the background
- Delete spam comments and trackbacks
- Delete temporary files
- Delete expired sessions
For all this to happen, you need to do any of the following:
- Run “$MT_DIR/tools/run-periodic-tasks” manually from the command line on regular basis
- Set a cron (on Linux) or a task scheduler (on Windows) to run this on regular basis
- Get the log feed on regular basis
- Use the XML-RPC API to run them, using mt.runPeriodicTasks
- Run every two minutes
- The task will just add “run scheduled tasks” line to the log
id: MyPlugin10 key: MyPlugin10 name: <__trans phrase="Sample Plugin Scheduled Tasks"> version: 1.1 description: <__trans phrase="_PLUGIN_DESCRIPTION"> author_name: <__trans phrase="_PLUGIN_AUTHOR"> author_link: http://www.example.com/about/ doc_link: http://www.example.com/docs/ l10n_class: MyPlugin10::L10N tasks: MyCustomTask: label: Do something every two minutes frequency: 120 code: $MyPlugin10::MyPlugin10::Tasks::do_task
- tasks
- This is the registry top for tasks
- MyCustomTask
- The name of the task to add
- label
- Description of the task
- frequency
- Task starting interval (in seconds)
- code
- The function to run, in the standard MT handler format
- $Plugin-name :: Perl Module :: handler name
package MyPlugin10::Tasks; use strict; sub do_task { doLog("run scheduled tasks"); } sub doLog { my ($msg, $class) = @_; return unless defined($msg); require MT::Log; my $log = new MT::Log; $log->message($msg); $log->level(MT::Log::DEBUG()); $log->class($class) if $class; $log->save or die $log->errstr; } 1;
-
do_task
is our periodic task function - On start, it call
doLog
, which we have already talked about in the Plugin Debugging chapter
We will use our trusty super-computer (that now got a little time off from calculating all these file names) to show how to run these tasks. Apparently, this computer have Linux running
$ cd $MT_DIR $ sudo -u apache ./tools/run-periodic-tasks $ sudo -u apache ./tools/run-periodic-tasks
- The periodic tasks should run under the same user/permissions as the webserver. In this example we use the apache user, (using the
sudo -u
command) but please adjust it to your environment (common cases: www-data, SuExec) - We just ran the run-periodic-tasks twice, without waiting
- Go to System => Tools => Log, and if there are too many logs, (Warning: Playing with the production server is bad for your sleeping hours) filter the level to show only debug messages
- You should see the “run scheduled tasks” message only once
There is only one message because we run the second command immidiately after the first, while the task is set to run only every two minutes.
If you will wait two minutes and then run it again, another message line will be added
Periodic tasks are a useful tool for doing time-based operations. For example, closing comments and trackbacks for old entries.
Still, for these to work, the periodic task script should be run on regular basis.
Prev:Callbacks and Hooks << Index >> Next:MT Objects and Database