Background Jobs in WordPress

In building an interactive site that, well, isn’t horribly slow, you frequently want to schedule background jobs, or queue asynchronous tasks, that should be run, but not right away. For example, if a user signs up and you want to send them a welcome email, you don’t necessarily want to do it while the page is rendering for the user, because if it takes a moment, it would slow things down. Spam checks, updating a search index, and so on might fall into the same category.

I’m starting to tinker with a half-baked idea for a WordPress plugin, and spent a while searching for how to do this. As best as I can tell, WordPress doesn’t provide a background worker per se. But it has a related concept that may suit your needs — the wp-cron system, particularly wp_schedule_event. (But check out all the wp-cron functions.)

Unlike a background worker that’s always running to pick up new tasks off the queue, wp-cron is a bit different. It appears that it can literally be invoked by cron on a *n.x system, or it can be woken up by page visits if it’s “time” for a scheduled event. I haven’t delved into it enough yet to be sure, but I suspect it wouldn’t be good for things where you assume that “queuing” them will almost-instantly run them. (Though that’s not really a safe assumption to make even for always-on background workers.) But if time isn’t quite of the essence, this may work very well.

One thought on “Background Jobs in WordPress

Leave a Reply

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

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>