Data, Maps, Usability, and Performance

WordPress Plugin with Background Process, Queue, and Debugging

Last updated on December 14, 2016 in Development

background processing sample WP plugin for async tasks

WordPress Plugins are often used to run a large process in the background like importing, exporting, bringing data from other APIs, pushing data out, and other tasks that should run asynchronously. How do you do that? TechCrunch came out with wp-async-task and WP Background Processing extended that by adding the ability to queue tasks. The author already included a good sample plugin and article but I wanted to see if I can write a more basic example. So, today, I will create a WordPress plugin that uses WP Background Process to queue and run PHP tasks in the background.

First, I download and activate WP Background Processing, as well as create a new example plugin where we extend the WP_Background_Process class and instruct the backend to call a function (backgroundProcess) for each item in the queue. To make sure that we can extend the class in our plugin, I had to use the WordPress plugins_loaded hook.

You can do anything you want in the backgroundProcess function, like making Ajax calls and storing data in the database, but printing there with something like var_dump will not display anywhere as it runs in the background. So, to make things simple and show how you can print for debugging, the example plugin sends the passed text to the error_log. You can print to anywhere, so if you have a log.txt file with appropriete write permissions, you could use the PHP file_put_contents call to print to a local file.

The plugin function that listens for the Ajax call (prefix_ajax_admin_test) takes a string passed as POST data and puts it twice into the queue before dispatching the process. It returns some text back to the client side and kills itself with wp_die(). So, if you pass a string “go pokeman go”, it puts that into queue two times and responds with “success”. WP Background Processing passes that string to our custom backgroundProcess function that writes it to the error_log and to the plugin’s log.txt file.

I have added a PHP sleep command in there so that I can see if PHP will assign a different PID (Apache process ID) to perform that action and the error log shows it:


//cat /var/log/apache2/error_log
[Mon Jul 18 08:18:12.679852 2016] [:error] [pid 19524] [client ::1:60299] go pokeman go
[Mon Jul 18 08:18:32.829665 2016] [:error] [pid 19528] [client ::1:60303] go pokeman go

This depends on your server setup and resource availability, so if I sleep for only 10 seconds, the same PID is used but when I sleep for 20 seconds, a different PID was used. WP Background Processing will take care of this and use system resource appropriately. I have initiated the Ajax call from the JavaScript console to run the tests above.

Check out the plugin on GitHub using the link below, it is less than 100 lines and a great starting point to build a WordPress plugin that runs multiple processes asynchronously in the background with PHP.

Background Process WordPress Plugin

External:

Run PHP Task Asynchronously
Use WP Cron to Trigger Asynchronous Background Tasks in WordPress
Print to Access Log with PHP

Tags: , ,

Facebook Twitter Hacker News Reddit More...