Automated, Scheduled "Tasks" with Craft CMS

Craft CMS is still the relatively new kid on the block when it comes content management systems, but I am doing more work with it these days. Compared to the 500-pound gorilla in the room, WordPress, there are a lot of differences. Of course, both have their advantages and disadvantages. As time goes on, however, Craft seems to be taking full advantage of hard learned lessons by other platforms, including WordPress.

Mainly, Craft is still new, and, as such, has a lot of catching up to do in terms of extensibility. That comes as more users and use cases are presented. For example, there is huge disparity in “hooks”, or places for developers to alter core outputs, when comparing Craft to WordPress.

With that said, I do want to talk about a feature of Craft that I feel it handles much better than its counterparts: automated background tasks.

I recently created a few plugins for Craft Commerce that required batch processing functionality for orders. The problem I had to solve was to routinely check a third-party service for an order’s shipped status and take specific actions based on the result. If an order had indeed shipped, update the order status in Craft Commerce, triggering an email with tracking information for the customer.

Craft “Tasks” To The Rescue

For periodic, automated processing like this, Craft provides a “tasks” feature. It’s similar to wp_cron in that it is a “pseudo”-cron job that can be created, but must actually be triggered via an HTTP request.1The way to get these sort of “pseudo”-cron scripts to run on a predictable schedule is to use cURL from the crontab requesting a specific URL that triggers the script.

There are a ton of articles on wp_cron, so I won’t dive into that for now. Suffice it to say, it’s a large, complicated class that has a ton of options for scheduling and running a script.

Craft’s “tasks” focuses on the actual job being performed both logically and efficiently. Whereas other automated scripts do nothing to batch operations, Craft gives developers a way to create individual “steps” within a large job. Each “step” is it’s own operation, being run sequentially among other “steps” scheduled to be run. You can also use “subtasks” to break down operations into even smaller processes.

As an example, let’s say you need to update 1 million rows in a database. Normal scripts would just start the job as a whole and report final results after the millionth row. With Craft, each row can exist as its own “step” in the batch. This leads to lower execution time and memory usage as each “step” is its own request.

Improvements Could Be Made

Of course, the system isn’t perfect, and I’d like to see some more work done for tasks. One problem is that if one step fails, the entire task is halted. I’d like to see the remaining steps without issues be completed. Also, if a task does fail, there’s no way to pass error messages to the admin. Instead, “Failed task” is simply displayed in the sidebar of the admin panel with only two options: “Try Again” or “Cancel”. I’d like to see some methods available to bubble up specific error messages so that the admin may try to resolve issues before attempting the task again.2As it stands now, I’ve just been logging my own errors to a custom log file for my plugins. Admins can at least reference that via the control panel to track down an task issues.

Results from a Craft CMS automated "task."

When a task fails, Craft doesn’t provide much information on why, nor does it offer any resolutions aside from “Try again” or “Cancel”.

Although Craft Tasks aren’t perfect, I really like the solid foundation they’ve laid for the future here. If you’re looking for a good place to get started with your own “tasks” plugin, Pixel & Tonic has a very basic example here. If you’re in need of additional help, please contact me.

About

Web Developer

References   [ + ]

1. The way to get these sort of "pseudo"-cron scripts to run on a predictable schedule is to use cURL from the crontab requesting a specific URL that triggers the script.
2. As it stands now, I've just been logging my own errors to a custom log file for my plugins. Admins can at least reference that via the control panel to track down an task issues.

Add Your Thoughts

Your email address will not be published. Required fields are marked *