Scheduler

Often when working on a Craft project that is more than just a simple website there is a need to perform certain tasks at a specific point in time. To solve this and other related problems I created Scheduler during my time at Supercool and they recently kindly allowed me to release it.

The initial problem that gave birth to this plugin was the need to bust the template caches of an entry when it changes status from pending to live or from live to expired. Out of the box Craft doesn’t do anything when an entry changes status due to time just marching on, it has no way to know that it actually needs to without somehow waking up and checking the time.

At its core, that’s what this plugin does - wakes up Craft to see if anything needs to be done at a given point in time. Simply set up a cron job at whatever interval suits your project (go wild if you like and run it every minute), see the installation docs for the details.

Publish future entries

For the entry publishing problem we need to save a record in the database for each entry with either the date it goes live or when it is due to expire. We can then do something at that point in time, which to solve our initial problem was simply to re-save the entry. Re-saving the entry triggers the template caches to be bust, as well as anything else that is relying on listening to the elements.onSave event.

This particular problem has also been recently dealt with in Fred Carlsen’s plugin Published Event, which if it had been around at the time I might have just installed and moved on with my life. However, we also came across another need whilst building this thing namely to show a particular Matrix block at some point in the future.

Again, it could be built into the dependencies of the {% cache %} tag but that can get inefficient very quickly. We found a more performant approach was to create a field type that stores the future date and logs a job with the Scheduler for then which will re-save the entry at that point in time.

These two features come baked in with the plugin, you can simply use the field type or toggle the element re-saving on and off via the config file.

But wait, there’s more!

This plugin is actually a framework to allow anyone with some basic PHP chops to create their own jobs. Lets go over the possibilities here briefly and if you want to get down and dirty with some code then check out the docs.

On a current project I needed to email a bunch of users when a particular date on an entry had been reached, to do that I just created a new job type in my plugin to handle the emailing and surrounding logic. Then I listened to the entries.onSave event to determine when the job should be run and that was it. Now my users get emailed at the right point in the future if they haven’t yet completed something we need them to do.

Also, I can see myself using it in an e-commerce setting to prompt the seller when an un-shipped order has been lying around too long.


So that’s Scheduler, let me know how you might use this and perhaps I can help you get a custom job type working for your use-case, or just dive in and do it yourself!

Josh Angell

Josh Angell

I build custom software for Craft CMS.
Need a hand with something? Get in touch!
Or you can find me as @josh_angell on Twitter.