Even though I'm currently having a great time at the TYPO3 Snowboard Tour in Zell am See I did some Magento hacking these days I want to share with you:
Using cron jobs in Magento is a very useful way to perform recurring tasks in the background. You can easily add tasks to your module by adding a couple of lines to your module's config.xml configuring what should be executed (model) in what intervals (cron_expr). Magento itself brings some tasks e.g. for processing reports or updating the currency rates. Here is an example of Aoe_Asynccache's configuration that will clear the cache in the background:
<config> <crontab> <jobs> <aoeasynccache> <schedule><cron_expr>*/15 * * * *</cron_expr></schedule> <run><model>aoeasynccache/cleaner::processQueue</model></run> </aoeasynccache> </jobs> </crontab> </config>
But there are some problems with those cron tasks: It's hard to see which tasks had been executed and which tasks will be executed next. Currently the only way is to have a look at the cron_schedule database table.
Developing cron tasks is not easy because it means waiting for the next scheduler run to execute your task. Debugging also is not easy as the scheduler runs those tasks in the background and won't give you any valuable feedback. Keeping an eye on the php error logs and writing own logs to see what is happening within your task is quite annoying.
My latest Magento extension "Scheduler" is the solution to these problems. It comes with two backend controllers showing grids of all available tasks and the scheduled ones, and a command line interface to help you handle the tasks, while adding some more handy features:
This view shows all available tasks. It scans the xml configuration from Magento's and your additional modules and generates a list of all tasks, while also considering those tasks, whose intervals can be configured dynamically within the Magento backend (e.g. sitemap generation).
Via the mass actions you can schedule a task to be executed as soon as possible. That means it will be executed on the next scheduler run, that is - depending on your server configuration - a couple of minutes after scheduling it.
You can also choose to run the task directly. Be careful with that, as the execution might last longer than a few seconds or might depend on some other command line environment settings. For testing small tasks this might still be a comfortable option.
A completely new feature is, that you can disable or enable a task. Magento does not offer this feature and will always execute all tasks as long as they have a valid model and cron expression. But some tasks may not be needed in your case (e.g. like newsletter_send_all) and can be turned off completely using this feature.
In this view you can see the tasks that were executed in the past and you can see what tasks will be executed in the near future. You can filter the list be job-code and status and you're even able to delete tasks.
There's a button in the top that allows you to regenerate the schedule.
The command line interface can be found in the shell folder. When calling it without any additional parameters you'll get an overview of available actions and their parameters:
> cd shell
> php scheduler.php
Available actions:
-action listAllCodes
-action scheduleNow -code <code> Schedule a job to be executed as soon as possible
-action runNow -code <code> Run a job directly
With the "listAllCodes" command you'll get an overview of all available tasks, their configured intervals and their status:
> php scheduler.php -action listAllCodes aggregate_sales_report_bestsellers_data 0 0 * * * disabled aggregate_sales_report_coupons_data 0 0 * * * disabled aggregate_sales_report_invoiced_data 0 0 * * * enabled aggregate_sales_report_order_data 0 0 * * * disabled aggregate_sales_report_refunded_data 0 0 * * * enabled aggregate_sales_report_shipment_data 0 0 * * * disabled aggregate_sales_report_tax_data 0 0 * * * enabled aoeasynccache */15 * * * * enabled aoecachecleaner 17 * * * * enabled catalog_product_alert 0 0 * * * enabled catalog_product_index_price_reindex_all 0 2 * * * enabled catalogrule_apply_all 0 1 * * * enabled currency_rates_update 0 0 * * * enabled log_clean enabled newsletter_send_all */5 * * * * disabled paypal_fetch_settlement_reports enabled sales_clean_quotes 0 0 * * * enabled sitemap_generate 0 17 * * * enabled
And finally you can schedule or even directly execute tasks from here, which is great for development or debugging of tasks:
> php scheduler.php -action runNow -code sitemap_generate
So, if you also miss those features in your Magento shop and want to see what's going on in the background, try my module that also includes clean model classes for schedules and task configurations:
Aoe_Scheduler (latest version from GitHub)
(As always the modman configuration file is included or simple extract the file into your Magento root directory).
Release notes:
New feature:
- Added possibility to have cron jobs return messages by having the cron method returning a value (on failure and on success. E.g. "Cleared XX cache entries. Duration YY seconds")
Fixed bugs:
- Cache will be cleared when disabling or enabling actions
- Fixed missing modules xml file
Info:
- Tested with Magento Community Edition 1.4.x and 1.5.x
Bugfix realease
Changes:
Checkout this blog post for details on the timeline feature
Bugfix release. Download latest version from GitHub.
This new version adds support for the Magento Webservice API, allowing triggering or scheduling tasks from a remote computer. This may be useful in different scenarios. (I need this to trigger tasks during acceptance tests.)
As always: Download the latest of this module version from GitHub.
Method name | Description | Arguments | Return value |
|---|---|---|---|
aoe_scheduler.runNow | Runs scheduler task immediately | code | array with task info |
aoe_scheduler.schedule | Schedules a task | code [, time] | array with task info |
aoe_scheduler.info | Retrieves information about a task | taskId | array with task info |
1. Create a web service user role:
Go to System > Web Services -> Roles -> Role Resources
and set "Resource Access" to "all" or to "Custom" and select the specific options you want to grant to your web services user:
<?php
$client = new SoapClient('http://yourhost/api/?wsdl');
$session = $client->login('your_username', 'your_api_key');
// Run a scheduler task
$result = $client->call($session, 'aoe_scheduler.runNow', array('aoecachecleaner'));
var_dump($result);
/*
array(7) {
["job_code"]=>
string(15) "aoecachecleaner"
["status"]=>
string(7) "success"
["created_at"]=>
string(19) "2011-11-15 20:55:41"
["scheduled_at"]=>
string(19) "2011-11-15 20:55:41"
["executed_at"]=>
string(19) "2011-11-15 20:55:41"
["finished_at"]=>
string(19) "2011-11-15 20:55:41"
["schedule_id"]=>
string(6) "150006"
}
*/
// Schedule a task to run (from a normal scheduler process) as soon a possible ...
$result = $client->call($session, 'aoe_scheduler.schedule', array('aoecachecleaner'));
// ... or at a given time (e.g. in 1 hour):
// $result = $client->call($session, 'aoe_scheduler.schedule', array('aoecachecleaner', time()+60*60));
var_dump($result);
/*
array(5) {
["job_code"]=>
string(15) "aoecachecleaner"
["status"]=>
string(7) "pending"
["created_at"]=>
string(19) "2011-11-15 21:06:07"
["scheduled_at"]=>
string(19) "2011-11-15 21:06:07"
["schedule_id"]=>
string(6) "150007"
}
*/
// Get information on an existing task
$result = $client->call($session, 'aoe_scheduler.info', array('150007'));
var_dump($result);
/*
array(5) {
["job_code"]=>
string(15) "aoecachecleaner"
["status"]=>
string(7) "pending"
["created_at"]=>
string(19) "2011-11-15 21:06:07"
["scheduled_at"]=>
string(19) "2011-11-15 21:06:07"
["schedule_id"]=>
string(6) "150007"
}
*/
Release notes:
Get the latest version of this module from GitHub or Magento Connect.
Btw, the Aoe_Scheduler module currently is on the landing page of Magento Connect.
Release notes:
Sometimes - for various reasons - Magento's scheduler might stop working. Often there are business critical tasks that need to be run on a regular basis, so there's a need in monitoring the if everything works fine behind the scenes. That's why I added a new dummy task "Heartbeat". By default it runs every 5 minutes. This can be configured or even deactivated completly. This tasks does nothing but showing that the scheduler is working correctly.
In addition to that there is a new shell command that allows you the check when a task was run successfully the last time (works for all scheduler tasks - not only for the heartbeat task):
> php scheduler.php -action lastRun -code aoescheduler_heartbeat
1327960203
By adding the "-secondsFromNow" parameter you'll get the duration since the last execution:
> php scheduler.php -action lastRun -code aoescheduler_heartbeat -secondsFromNow
35
So you can use this to integrate your instance to whatever monitoring you're using. Using the default configuration the last value must not be bigger than 5x60 = 300 seconds. If it is, there's something wrong with your scheduler.
In the future I might also add a controller to monitor this from outside, but currently this Magento shell script should do the trick.
Get the latest version of this module from GitHub or Magento Connect.
The latest version of Aoe_Scheduler is available
Recent Comments