Skip to content

Instantly share code, notes, and snippets.

@pcdinh
Created May 13, 2010 15:29
Show Gist options
  • Select an option

  • Save pcdinh/399950 to your computer and use it in GitHub Desktop.

Select an option

Save pcdinh/399950 to your computer and use it in GitHub Desktop.
2 separated processes
=====================
+ Master agent which provides REST-like protocol. It is basically a socket server that supports minimum HTTP protocol
+ Worker agent, which is a big loop, is able to launch/close worker processes
Design
======
+ Trying not to abuse pcntl_fork() because I can not differentiate master agent process, worker agent process
and worker processes by a name when using top. Instead I used proc_open()
+ To increase concurrency, master agent process uses pcntl_fork() per request
+ However, still find no way to establish a direct communication between new forked child processes of the master agent and worker agent
[root@clipserver tmp]# ./workerctl.php
The process workerctl started with ID 5299
2010-05-13.22:15:36 Trying to start worker agent
Socket has been created.
Port 8888 has been binded to the GearmanManagerMasterAgent.
Socket has been created and binded.
Starting the data receiving loop.
Caught a SIGCHLD signal, a forked child process exiting
2010-05-13.22:16:37 The worker agent has stopped unexpectedly
2010-05-13.22:16:37 Trying to start worker agent
[root@clipserver ~]# ps -eaf | grep worker
root 5299 5200 0 22:15 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php ./workerctl.php
root 5300 5299 0 22:15 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php workeragent.php
root 5303 5300 0 22:15 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php workers/reverse.php
root 5304 5300 0 22:15 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php workers/reverse.php
root 5335 5300 8 22:16 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php workers/reverse.php
root 5337 5265 0 22:16 pts/4 00:00:00 grep worker
[root@clipserver ~]# kill -HUP 5300
[root@clipserver ~]# ps -eaf | grep worker
root 5299 5200 0 22:15 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php ./workerctl.php
root 5339 5299 0 22:16 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php workeragent.php
root 5340 5339 1 22:16 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php workers/reverse.php
root 5341 5339 1 22:16 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php workers/reverse.php
root 5342 5339 1 22:16 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php workers/reverse.php
root 5343 5339 1 22:16 pts/1 00:00:00 /usr/local/php5-fcgi/bin/php workers/reverse.php
root 5345 5265 0 22:16 pts/4 00:00:00 grep worker
# Basic REST-like Prototol: GET http://127.0.0.1:8888/master/stop
/master/stop
Stops master agent process. There is no /master/start
/agent/reload
Stops and stop worker agent to reload its configuration/source code or try to free resources
/agent/stop
Stops worker agent and its workers
/agent/start
Starts worker agent and its workers
/worker/name/[name]/stop
Stops all the processes of a worker provided with a name
/worker/name/[name]/start
Starts all the processes of a worker provided with a name
/worker/pid/[pid]/stop
Stops a worker process provided with a PID
/worker/name/[name]/reload
Stops and starts all the processes of a worker provided with a name
/worker/name/[name]/status
Returns number of processes of a worker, start time, their PIDs
/workers/status
Returns list of workers and their information: number of processes of a worker, start time, their PIDs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment