Super useful PHP tools: Part 1

Pankaj Kargirwar
4 min readNov 18, 2017

--

What? You are still using PHP? Haven’t jumped the sinking ship yet? Oh, You don’t think it is sinking in the first place? Alright then! Let’s get down to business! I might have something useful for you.

For all the bad press PHP gets I have found it to be perfectly capable of solving real world problems with minimum fuss. PHP also has a huge ecosystem and a plethora of frameworks to choose from. I prefer to use a minimalistic framework such as Slim and build the app by using PHP tools/components from various sources. Some tools have made such a huge difference to my workflow that I have wondered how I got along without them earlier. But the very fact that it was a while before I discovered them tells me that they are relatively obscure. I plan to write a series of articles about these tools.Here is the first one.

Gearman

Roughly PHP works like this: Whenever a new request comes the PHP interpreter runs the entry point script, the script does its job , returns the output to the interpreter and exits. What if the script takes a long time to finish? Too bad. The caller must keep waiting. Some times there is no other option. For example if the script is running a database query which is taking a long time, there is nothing anyone can do about it.

But there are occasions when it makes no sense to keep the caller waiting. Say you are required to build a big report in PDF format by running several database queries and then email the report to a certain address. It makes no sense to keep the caller waiting till the email is sent. It would be nice to inform the user that we are on the job and would they please check their email after sometime? What we need here is a way to schedule a task in the background.

It is pretty hard to do that with PHP alone. (The trolls did remind you that PHP is single threaded right ?). You can get by using cron job sometimes. But they are not very flexible.

Enter Gearman. Quoting from the website:

A Gearman powered application consists of three parts: a client, a worker, and a job server. The client is responsible for creating a job to be run and sending it to a job server. The job server will find a suitable worker that can run the job and forwards the job on. The worker performs the work requested by the client and sends a response to the client through the job server.

Exactly what we want! Our script will send the “report” task to Gearman server and return success to the user. The server will send the job to a worker which will create the report and send email to the user in background. If you want to see all of this in action head to github. The code is adapted from PHP docs.

There are just two classes: Client.php and Worker.php. The Client class creates a Gearman client in the constructor:

There is just one method: run. It accepts user’s email address to which report should be sent:

Note the type hinting. No one stops you from writing good code in PHP!

One gotcha here is that second parameter to doBackground function is mandatory. Even if you don’t care about it do pass an empty string. Sending null does not seem to work.

The Worker constructor looks like this

It just creates a new worker object and registers a function named “report” to execute when it receives a new job. The important bit to note here is the second parameter to addFunction. Since we are in a class context we have to pass the function name in an array whose first parameter is “$this”. This part may be unfamiliar to new PHP devs.

Worker methods are as follows:

The run method goes into an infinite loop waiting for new jobs from the job server. The report method performs the useful work. In this case just sleep for 1 second.

There is one important concern when using Gearman in this fashion. What happens when the worker crashes due to some unforseen error? There has to be a mechanism which monitors the workers and restart them when needed. My favorite tool for doing this is supervisor. More about that in some other post though.

We have discussed just one way of using Gearman here. There are many other use cases possible. Hope this post has given you enough pointers to explore other ways of using Gearman in your work. Do leave your feedback in the comments.

(Series index: Part1 Part2)

--

--