Automation & Scheduling with Scheduler

Earlier this month, May 7th to be precise, the first Scheduler package was released after a lot of hard work from Bert on (among other things) the interface. Scheduler is an open source addon for MODX that enables running one-off or recurring tasks at specific times in the future without the hassle. It's sort of like a cron job meets message queue. 

To give an example of what you can do with Scheduler, here's what we run when a user signs up on modmore.com:

<?php
// Grab the Scheduler service
$path = $modx->getOption('scheduler.core_path', null, $modx->getOption('core_path') . 'components/scheduler/');
$scheduler = $modx->getService('scheduler', 'Scheduler', $path . 'model/scheduler/');

// Grab the task "signup_reminder_14d" in the "morebilling" namespace
$task = $scheduler->getTask('morebilling', 'signup_reminder_14d');
// Run in in 2 weeks with the passed data
$task->schedule('+14 days', array(
    'client' => $client->get('id'),
    'user' => $user->get('id'),
));

As the example illustrates we can tell Scheduler we want a Task (which is either a snippet, processor or simple php file) to run at some point in the future. This can be a relative date like above ("+14 days"), but it could also be a specific UNIX timestamp. A Task Run is then added to the Scheduler database and once that time rolls by it will execute the task passing along the data we provided before. 

The Scheduler Component provides a helpful interface to review and manage all tasks and runs. For example you can add new tasks, change or schedule new runs and check the history. 

We've been using Scheduler on the modmore site since December 2013 and over time more and more background processes are set up with it. It's super simple to integrate and it helps with performance to offload heavy processing and external API interactions to the background. 

Scheduler is available for free from the modmore.com package provider and on GitHub.