Example of how to install and use PHP for gearman Manager


This paper introduces the installation and PHP usage of gearman manager. The details are as follows:

1、 Gearman comes with a gearadmin tool

View help information

> gearadmin --help

View status

> gearadmin --status

View worker information

> gearadmin --workers

2、 Why use gearman manager

Why use gearman manager to manage our workers? Because our workers written in PHP are resident in the background.
And once there are too many workers and the code is updated, do you want to manually restart one by one? Obviously, this is very troublesome. If there is an unexpected interrupt or memory leak in the middle, it is not easy to manage.

There are two kinds of solutions on the Internet
1. Restart the worker regularly through crontab
2. Through the pcntl extension of PHP, create multiple processes to run worker. The main process monitors the sub process. If the sub process exits, restart a sub process to run worker.

3、 Using gearman Manager

Gearman manager download address:


For example: gearman manager-2.1.0.tar.gz

1. We create a directory to store worker code

> mkdir -p /data/workers

2. Then we create a worker file of sum. PHP to process and calculate the sum of the two numbers.

The code of sum.php is as follows:

//Note that the function name is the same as the file name
function sum($job) {
 //Workload () gets the serialized data sent by the client
 $data = json_decode($job->workload(), true);
 //Calculate and return the result
 return $data[0] + $data[1];

Of course, the object-oriented code is as follows:

//Note that the class name is the same as the file name
class sum {
 public function run($job) {
  //Workload () gets the serialized data sent by the client
  $data = json_decode($job->workload(), true);
  //Calculate and return the result
  return $data[0] + $data[1];

3. Unzip the downloaded gearman manager package

> tar xf GearmanManager-2.1.0.tar.gz
> mv GearmanManager-2.1.0 /data/GearmanManager
> cd /data/GearmanManager

We only need to care about bin and etc. there are three configuration files in etc

Config-advanced.ini advanced configuration
Simple configuration of config.ini
Configuration of config.php mode

Profile description:

; Global configuration
; Specify the server, multiple servers can be added, separated by commas
; Worker file storage directory, you can add multiple, separated by commas
; The worker to be loaded is all by default
; Number of all worker processes running
; The number of worker specific processes, for example, the total number is 10, there are 10 workers, and each worker has one process
; Maximum valid period of worker, in seconds
; Monitor the code update of the worker directory. If the code update is found, kill the process and reload the code.
; Time out
timeout = 300
; Of course, we can also configure a specified worker
; Specify 5 processes
; All five processes only do sum work

4. We copy the configuration file, modify it as shown above, and save it

> mv etc/config-advanced.ini etc/GearmanManager.ini

5. Running gearman Manager

> cd /data/GearmanManager
> ./bin/pecl_manager.php -c /data/GearmanManager/etc/GearmanManager.ini -vvv

The parameters are described as follows:

-P management process PID file directory, configuration file through PID_ File
-D runs as a daemon
-U specifies the user of the daemons
-W specifies the worker directory. Of course, there is configuration in the configuration file, which can not be used here
-C specifies the configuration file
-L specify the log storage directory, if not specified, output to standard output stdout
-H specifies the server, for example:
-V only records start-up and shut-down information
-Creation and exit information of VV process
-VVV about worker information and work done
-Vvvv debugging information
-Vvvvvv all information

If / usr / bin / env: PHP: does not have that file or directory, add the PHP executable to the environment variable

> echo 'export PATH=/data/php56/bin:/data/php56/sbin:$PATH' >> /etc/profile
> source /etc/profile

For more information about PHP, readers interested in this site can see the following topics: summary of PHP process and thread operation skills, summary of PHP network programming skills, introductory course of PHP basic syntax, complete collection of PHP array operation skills, summary of PHP string usage “PHP + MySQL database operation tutorial” and “PHP common database operation skills summary”

I hope this article is helpful for PHP programming.