Normally, PHP is very poor at handling large, complex operations. PHP has no concept of “out-of-band” or “asynchronous” processing, so everything is processed within the scope of the current request:
1. A user makes a request to the web server.
2. PHP parses and executes the requested script and all included scripts in their entirety.
3. The web server returns the results of the PHP script to the user.
The larger or more complex the logic is, the slower Step 2 gets. This can become painfully obvious to the user when a “blocking” operation occurs–a large file is created, compressed, or encoded, a call to an API or remote server is made, etc. Worse for PHP is the particular way that PHP, Apache, and other common components of the server stack handle multiple requests. In certain cases, if one user is waiting on a “slow” request, many other users may experience slow responses.
Escher’s new job scheduler is designed to alleviate this problem. Instead of performing a “blocking” operation during a request, a script can create a job to perform the operation asynchronously, after the request has completed. Jobs can be set to run at some point in the future, but often, they are “scheduled” to be processed immediately.
Escher accomplishes this by making use of the Linux command-line interface. When a job is created, Escher sends a command to the command line that starts the job scheduler process. This process is non-blocking and exists outside of the current request.
This way, the request can complete quickly and send a response to the user. The background job has not completed at this point, but the response can provide feedback to the user: “Check back in a few minutes.”
A more sophisticated response might utilize Ajax to check the status of the job periodically. Here again, Escher’s job scheduler can come in handy. As the background job process, the scheduler collects information about the status of the job–error messages, return status (success or failure), and more. The job itself can even save progress information, human-readable messages, and other data to the database.
There are plenty of other features I want to add to the job scheduler–especially configurability, e.g., the ability to disable automatic processing (depending instead on a system cron) or pinging one or more separate “job servers” in lieu of local processing.