Lately Matt Shaw from Scrumptious Brains published a post a few new library they created to assist in one in every of their merchandise. This library WP_Queue supplies a Laravel-like Activity control device for WordPress. A task queue is a device that lets you time table jobs to run sooner or later. We generally tend to make use of jobs for 2 causes. First, we might want to wait awhile, like if we wish to time table a follow-up electronic mail in every week. The opposite reason why is efficiency. Perhaps we want to do one thing computationally pricey and don’t need the consumer to attend.

A task supervisor provides us a scheduling device — some solution to retailer jobs till they want to be run — and a role runner — some software for working the roles. WordPress’ wp_cron type of suits this description. Alternatively, via the use of WP_Queue, I’ve discovered it suits my wishes higher.

The WP_Queue

The WP_Queue  bundle seems nice for a couple of reaons. First, is that the roles are abstracted from the runner and scheduler. I will be able to write a role magnificence and take a look at it as unit in isolation. Secondly, the roles scheduler is abstracted. Through default, jobs are recorded within the WordPress database, after which when they’re wanted, they’re scheduled with wp_cron. However I will be able to additionally use a building motive force that makes them synchronous and there’s a Redis motive force in growth. So let’s get began.

Set Up

The WP_Queue library is a composer bundle. First, set up the bundle for your plugin:

View the code on Gist.

It is very important upload the database tables for the scheduler. The Readme for the package has instructions. Upload this in your plugin’s activation hook or anywhere you upload your personal tables.

Developing Jobs

Should you’ve ever used Laravel’s activity queue, the construction of the WP_QueueJob magnificence, will likely be acquainted. Your activity magnificence has to have a take care of() means. That means is known as when the activity is administered. You most likely can have a __construct() means as smartly.

The important thing thought to grasp about those categories is that the homes of the category are serialized to the database when the activity is scheduled and used to instantiate the category when it’s run.

As an example, let’s say you sought after to create a role to run every time a submit is stored. Within the __construct() means, you could move the submit ID and use that argument to set a assets. When the take care of means runs, that assets will likely be set with the submit ID. Within the take care of means, you’ll be able to use that assets — the stored submit ID that was once stored with the activity — to get the stored submit from the database. Here’s what that appears like:

View the code on Gist.

Replica A Put up’s JSON To A Report When It Is Stored

What we’ve finished up to now is a role that will get a submit from the database. That’s cool, shall we get started a runner, time table this activity to run each time a submit is stored, and make this activity get the stored submit’s REST API reaction and write it to a JSON document. Let’s paintings via that record backward.

Why that order? That final requirement will likely be one magnificence, which I will be able to take a look at in isolation, and if it really works, then I will be able to setup the runner and hook into the save_post motion understanding that activity works. If I did it the wrong way round, I wouldn’t know if my drawback was once the activity, or the queue. That’s unhealthy science.

I’ve written about a similar appraoch to running jobs after a post is saved before with Torque using a different task runner.

Write A Put up’s JSON To A Report

Let’s stay operating on our activity magnificence. We have now the entirety we want to get the submit. We want to get the reaction object that the WordPress REST API would create for the submit, serialize it to json and write that JSON to a document.

I wrote a post for Torque awhile ago about how to get posts from the WordPress REST API without making HTTP requests. I stole maximum of this code from there:

View the code on Gist.

That is the entire activity. I used the WordPress REST API’s submit keep watch over to create a WP_REST_Response and used json_encode() to make it a string and stored it to a document named for the submit slug.

The place you saved the document and what you name it relies on your wishes.

Presently, we will be able to instantiate this magnificence to check if it really works. We will be able to run it immediately with one thing like this, the place $postId was once the ID of a broadcast submit:

View the code on Gist.

Then you definately will have to see within the trail you place for writing a document, a document with the JSON illustration of the submit. We will be able to additionally write an integration take a look at for it:

View the code on Gist.

Scheduling The Activity To Run When The Put up Is Up to date

Now that we all know our activity would paintings if it was once scheduled to run, we want to time table it to run, every time a submit is stored. The save_post motion fires when a submit is stored, so we will be able to use that. Within the callback serve as, we will be able to instantiate the activity magnificence and move it to wp_queue()->push().

View the code on Gist.

That’s sufficient to time table it to run, once imaginable.

Delaying Activity Execution

The second one argument of the rush()  means is the prolong time to run the activity. The final code instance didn’t use that argument. The activity will run as quickly because the queue will get to it. If we needed to prolong it via 5 mins, shall we move 600 — 5 mins in seconds — as the second one argument.

View the code on Gist.

Setup The Activity Runner

Remaining step: we want to setup the queue to run. We will be able to run the queue the use of wp_cron:

View the code on Gist.

If you find yourself doing native building, it’s higher to make use of the “sync” motive force, as a substitute of the database motive force so jobs run synchronously, IE immediately. This makes checking out more straightforward.

View the code on Gist.

Use With out WP-CRON

I’m no longer going to enumerate the numerous shortcomings of wp_cron. However, what’s in point of fact cool about WP_Queue is I will be able to arrange my very own device to run it. For optimum perfomance, I don’t wish to depend on WordPress to cause wp_cron and I don’t wish to upload extra “cron” jobs to its queue. As a substitute, I’d reasonably upload a REST API endpoint and ping it with an exterior cron activity, similar to one run with setCronJob.com or one thing.

We will be able to get entry to the queue with the serve as wp_queue(). That returns a Queue object that has a public means known as employee(). That returns a Employee object with a procedure() means that can run one activity if any jobs are to be had to be processed. We will be able to use some time loop to run jobs till procedure returns false, and use an incrementing integer to stay the loop from working for too many requests:

View the code on Gist.

On this instance, I’m hooking it as much as a REST API course, that might permit an exterior cron carrier to cause the queue. You arrange your server, it might be maximum safe to cause that serve as with an actual cron or a command that may simplest be run from inside the server/ container. If that’s no longer imaginable, at a public/secret key authentication test or one thing.

Josh Pollock

Josh is a WordPress developer and educator. He’s the founding father of CalderaWP, makers of superior WordPress gear together with Caldera Forms — a drag and drop, responsive WordPress shape builder.

The submit Using the WP Queue to Copy REST API Data to Files gave the impression first on Torque.

WordPress Agency

[ continue ]