Main process
$module =basename ($_server[' script_filename '), '. php '); $__doc__= "usage:{$module}.php start|stop|restart \ n"; $cmd = Isset ($_server[' argv '][1])? Strtolower ($_server[' argv '][1]): ' Start '; $procNum =isset ($_server[' argv '][2])? Intval ($_server[' argv '][2]): 1; $daemon =new forkpool (Array (' module ' = ' Rsyncdata ', ' Piddir ' = '/home/services/daemon/'); $msg =new Msgtask (ipc_key _file_export,ipc_msgqueue_size, ' Msghandle '); $log = Resourcemanager::get_logger (); $invoice = resourcemanager::get_ Logger (' Rsync_invoice '); $db = Resourcemanager::getdatabase (' Chaohaowan '); $daemon->settask ($msg, $procNum); Switch ($cmd) {case ' start ': $daemon->start (); Break;case ' Stop ': $daemon->stop (); Break;case ' restart ': $daemon- >restart (); Break;default:echo $__doc__;echo "Please input [start | Stop]\n "; break;} function Msghandle ($data) {global $invoice; $invoice->err (' data: '. Var_export ($data, True)); if (Empty ($data[' cmd ') | | Empty ($data [' data])) return, $cmd = ABS (Intval ($data [' cmd])), switch ($cmd) {case Cmd_rsync_add_post:rsync_add_post ( $data [' data ']; break;case cmd_rsync_delete_post:rsync_delete_post ($data [' data ']); Break;case cmd_rsync_mod_post: Rsync_modify_post ($data [' Data ']), Break;case cmd_rsync_add_tag:rsync_add_term ($data [' Data '], Termtype_tag); Case Cmd_rsync_mod_tag:rsync_modify_term ($data [' data ']); break;case cmd_rsync_add_category:rsync_add_term ($data [' Data '], termtype_category); break;case cmd_rsync_mod_category:rsync_modify_term ($data [' data ']); Break;case CMD_ Rsync_add_user:rsync_add_user ($data [' data ']); break;case cmd_rsync_mod_user:rsync_modify_user ($data [' data ']); Break;case Cmd_rsync_mod_postmeta:rsync_modify_postmeta ($data [' data ']); break;case Cmd_rsync_bbs:rsync_bbs ($data [ ' Data '); Case Cmb_rsync_bbs_gamestore:rsync_gamestore ($data [' data ']); break;default:break;} return;}
Thread pool
The adornment method name of the class Forkpool {/** * task that implements the adornment Forkpool * */Const FUCTIONNAME_DRESS = ' dressfork '; Public $_options=array (' piddir ' = './', ' module ' = ', ' maxperchild ' =>36000, ' Startnum ' =>1);p rotected $_ Running=true;protected $_proclist=array ();/** * fork_task * * @var frok_task */protected $_task=array ();p rotected $_ procnum=1;protected $_pidfile;public function __construct (array $cfg = Array ()) {$this->_options=array_merge ($ This->_options, $cfg), if (Empty ($this->_options[' module ')) {$this->_options[' module ']=basename ($_server [' Script_filename '], '. php ');}} Public Function Settask (fork_task $task, $procNum = ') {$procNum = (empty ($procNum))? $this->_options[' Startnum ']: I Ntval ($procNum), if ($procNum > 1) {require_once Base_path. '/baselib/fork/multi.php '; $this->_task=new Fork_multi ($task);;} else {$this->_task= $task;} /* Decorate method to invoke task */$func _name_dress=self::fuctionname_dress;if (method_exists ($task, $func _name_dress)) {$task->$ Func_Name_dress ($this);} /**/$this->_pidfile=rtrim ($this->_options[' Piddir '], "\\/"). ‘/‘ . $this->_options[' module '). '. PID '; /**/$this->_procnum= $procNum;} Public Function Start () {if ($this->isrunning ()) {echo "daemon is running\n"; exit;} $pid =pcntl_fork (), if ($pid = =-1) {Die ("fork (1) failed!\n"),} elseif ($pid > 0) {//Let the user-initiated process quit exit;} Create a new session that differs from Terminal posix_setsid (), $pid =pcntl_fork (), if ($pid = =-1) {Die ("fork (2) failed!\n"), ElseIf ($pid &G T 0) {//parent process exits, remaining child process becomes the final independent process exit;} Set Process signal Filter pcntl_signal (SIGHUP, sig_ign);p cntl_signal (Sigttin, sig_ign);p cntl_signal (Sigttou, sig_ign);p Cntl_ Signal (Sigquit, sig_ign);p cntl_signal (SIGINT, sig_ign);p cntl_signal (SIGTERM, sig_ign);p cntl_signal (SIGUSR1, Array ( $this, ' interrupt '); File_put_contents ($this->_pidfile, Posix_getpid ()); $log =logger::getlogger (); $log Server (' daemon started. '); if ($this->_procnum > 1) {$space =ceil ($this->_options[' maxperchild ']/$this->_procnum); for ($i = 0; $i < $this->_procnum; + + $i) {$pid = $this->_task->run ($space * ($i + 1)), $this->_proclist[$pid]=time (); Usleep (100000);} Unset ($space); $count =0; $status =0;while (True) {$pid =pcntl_wait ($status, Wnohang), if ($pid > 0) {unset ($this->_p roclist[$pid]);} if ($this->_running) {if ($pid > 0) {//$pid = $this->_task->start ($this->_options[' maxperchild ']); this->_proclist[$pid]=time (); $log->server (' fork new process pid: '. $pid);}} else {if ($pid > 0) {$count = 0;} + + $count; if (($count > 500) | | (Count ($this->_proclist) = = = 0)) {break;} Usleep (* (COUNT ($this->_proclist) + 1));} Usleep (10000);}} else {while ($this->_running) {$this->_task->run ();}} echo "Daemon stopped.\n"; $log->server (' daemon stoped. ');} Public Function isrunning () {Clearstatcache (); if (Is_file ($this->_pidfile)) {$pid =intval (file_get_contents ($ This->_pidfile)); if (Is_link ('/proc/'. $pid. '/exe ') {$execLink =readlink ('/proc/'. $pid. '/exE '); if ($execLink = = Php_bindir. '/php ') {$cmdline =file_get_contents ('/proc/'. $pid. '/cmdline '); $execName =basename ($_server[' script_filename ')); if (Strpos ($cmdline, $execName) = = = = False) {} else {// Runningreturn true;}}}} return false;} Public Function Stop () {if (Is_file ($this->_pidfile)) {$pid =intval (file_get_contents ($this->_pidfile)); PID > 0) {if (Is_link ('/proc/'. $pid. '/exe ') {$execLink =readlink ('/proc/'. $pid. '/exe '); if ($execLink = = Php_bindir. '/php ') {$cmdline =file_get_contents ('/proc/'. $pid. '/cmdline '); $execName =basename ($_server[' script_filename '); if (Strpos ($cmdline, $execName) = = = = False)) {// Non-self-initiated process echo "process check error!\n";} else {$ret =posix_kill ($pid, SIGUSR1), if ($ret = = = False) {echo "Stop daemon error!\n";} else {echo "Stop daemon....\n"; /unlink pid Fileunlink ($this->_pidfile);}} else {echo "process check error!\n";}} else {echo "process has exited!\n";}}} else {echo "pid file not find!\n";}} Public Function RestArt () {$this->stop (); echo "restarting.......\n"; sleep (1); $this->start (); echo "Restart finished\n";} Public Function interrupt () {$log =logger::getlogger (); $this->_running=false;if ($this->_procnum > 1) { foreach ($this->_proclist as $pid = + $info) {$ret =posix_kill ($pid, SIGUSR2), if ($ret = = = False) {$log->err ("s Topped process $pid error! ");} else {$log->server ("Process $pid stopped!");}}}}
Process Pool Derivation Process
Class Fork_multi {protected $_taskruns=0;protected $_running=true;/** * Wrap Task * * @var fork_task */protected $_task=nu ll;protected $_pid=0;public function __construct (fork_task $task) {$this->_task= $task;} Public function Run ($taskRuns = 3600) {$pid =pcntl_fork (); if ($pid = = = 1) {exit;} elseif ($pid = = 0) {pcntl_signal (SI GUSR2, Array ($this, ' Stop '), $this->_taskruns= $taskRuns; $log =logger::getlogger (); $this->_pid=posix_getpid ( ) while ($this->_running) {if ($this->_taskruns < 1) {//stopexit (0);} $this->_task->run ();--$this->_taskruns;}} ElseIf ($pid > 0) {return $pid;} Must exitexit;} Public Function Stop () {$this->_running=false;}}
Actual Work process
Class Msgtask implements Fork_task {protected $_date, $log;p ublic $code = 0;public $msg = ";p rivate $delay = 50 00;protected $callback, $key, $size;p rotected $msg _queue;public function __construct ($key =null, $size = null, $callback = NULL) {$this-_date = Date (' y-m-d '); $this-log = Logger::getlogger (); $this->key = $key; $this->size = $size; $this->msg_queue = Msgqueue::init ($this->key)->open ($this->size); $this->callback = $callback; }public function Run () {$this->main (); Usleep ($this->delay);} Public Function Dressfork (Forkpool & $fork _pool) {$fork _pool->_options[' module '].= ' _ '. $this->key;} Public Function Main () {if (is_callable ($this->callback)) {$this->getanddo (); }else{$this->log->fatal ("This handle has not method:msghandle!"); Exit (3); }} Public Function Setkey ($key) {$this->key= $key; return $this; }public function Setdelay ($microseconds) {$this->delay = $microseconds; return $this;} Public Function Setcallback ($callback) {$this->callback= $callback; return $this;} Public Function Getanddo ($callback =null) {if (Is_null ($callback)) $callback = $this->callback; $content = $this Msg_queue->get (); if (empty ($content)) return false; if (is_callable ($callback) &&!empty ($content)) {$ret =call_user_func_array ($callback, Array ($c ontent)); }}protected function _clearerr () {$this->code=0; $this->msg= ';} Public Function __destruct () {}}
&NBSP;IPC Communication
/** * Queue * */interface Queue {public function open ($size =0xffff, $seq _key = 1);/** * Get a record from the queue * * @return Mix */public F Unction get ();/** * Add to Queue * * @param mix $v * @return bool */public function Add ($v);} /** * Queue implemented through Message Queuing * */class Msgqueue implements queue {protected $key = '; protected $msg _queue=null; protected $msgtype = 0; function __construct ($key) {$this->key= $key; return $this; /** * Create a queue based on message queue * * @param string $key key, can use Ftok (__file__, ' Q ') * @return msgqueue */static function init ($key) {return new self ($key);} /** * Get Queue * * @param int $size * @param int $msgtype * @return Msgqueue */function open ($size =0xffff, $msgtype =1) { $key = $this->key; $this->size= $size; $this->msgtype= $msgtype; Permission to be enough, otherwise the httpd process will not be written in $this->msg_queue = Msg_get_queue ($key, 0777); {(! $this->msg_queue) {$this->msg_queu E = Msg_get_queue ($key, 0777), if (! $this->msg_queue) {//$this->log->fatal ("Fail Get queue,key:{$key}."); return FALSE;}} return $this; The function get () {$content = null; $code = 0; $dummy = Null;//echo microtime (TRUE). " \ n "; if (!is_resource ($this->msg_queue)) {return FALSE; }//-----------------queue, expected type, real type, maximum number of bytes, content callback, deserialization, blocking, error code//maximum bytes will lead to insufficient memory $ret = msg_receive ($this->msg_queue, $ This->msgtype, $dummy, $this->size, $content, TRUE, msg_ipc_nowait, $code)//echo microtime (TRUE). " \n\n++ ";//Packet error if (! $ret) {//$this->log->fatal (" Fail receive queue, retrying ", $code);//Packet error, retrieve message queue and return FALSE;} return $content; } function Add ($content) {if (!is_resource ($this->msg_queue)) {return FALSE; }//write return msg_send to message queue ($this->msg_queue, 1, $content, TRUE); } function Destory () {return msg_remove_queue ($this->msg_queue); }}/** * Queue based on shared memory * */class Shmqueue implements queue {protected $key = ';p rotected $sem _id, $shm _id,$seq _key;public function __construct ($key) {$this->key = $key; return $this;} /** * Create a queue based on shared memory * * @param string $key memory address key, can use Ftok (__file__, ' Q ') * @return shmqueue */static function init ($key) { return new self ($key);} /** * Open Memory space * * @param int $size space Size (bytes) * @param mix $seq _key Memory key * @return unknown */public function open ($size =0 XFFFF, $seq _key = 1) {$IPC _key = $this->key;if (! $this->sem_id) {$sem _id = sem_get ($IPC _key);//Create or obtain an existing one to $IPC _key is the semaphore of key $this->sem_id = $sem _id; $this->seq_key = $seq _key;} else {$sem _id = $this->sem_id; $seq _key = $this->seq_key;} $this->shm_id = $shm _id = Shm_attach ($IPC _key, $size, 0666); Create or associate an existing shared memory Sem_acquire ($sem _id) with $ipc_key as key; Possession of the semaphore, equivalent to lock, at the same time only one process run this code $val = @shm_get_var ($shm _id, $seq _key); Get values from shared memory if ($val && Is_array ($val)) {} else {$val = array (); Shm_put_var ($shm _id, $seq _key, $val);} Sem_release ($sem _id); Releases the semaphore equivalent to unlocking the return $val;} /** * Close Shared Memory Association * */PUBlic function Close () {$shm _id = $this->shm_id;//Create or associate an existing shared memory to $ipc_key as key Shm_detach ($shm _id);//Turn off shared memory affinity}/* * * Destroy all data in shared memory * */public function destory () {$sem _id = & $this->sem_id;if ($this->shm_id) {shm_remove ($this-& GT;SHM_ID); Shm_detach ($this->shm_id); Close the Shared Memory Association}if ($sem _id) {sem_remove ($sem _id);}} /** * Get a record from the queue * * @return mix */public function Get () {$shm _id = $this->shm_id; $sem _id = $this->sem_id;if (! ($sem _id && $shm _id)) Return False;sem_acquire ($sem _id); Possession of the semaphore, equivalent to lock, at the same time only one process run this code $val = @shm_get_var ($shm _id, $this->seq_key); Get values from shared memory if ($val && Is_array ($val)) {$v = Array_shift ($val); Shm_put_var ($shm _id, $this->seq_key, $val); Writes the modified value to shared memory} else {$v = NULL;} Sem_release ($sem _id); Releases the semaphore equivalent to unlocking the return $v;} /** * Add a record to the queue * * @param mix $v * @return bool */public function Add ($v) {$shm _id = $this->shm_id; $sem _id = $this-&G T;sem_id;if (! ($sem _id && $shm _id)) Return FalsE;sem_acquire ($sem _id); Possession of the semaphore, equivalent to lock, only one process in the same time run this code//$this->shm_id= $shm _id = Shm_attach ($this->key); $val = Shm_get_var ($shm _id, $ This->seq_key); Get values from shared memory if ($val && Is_array ($val)) {Array_push ($val, $v);} else {$val = array ($v);} $status = Shm_put_var ($shm _id, $this->seq_key, $val); Writes the modified value to the shared memory Sem_release ($sem _id); Release semaphore equivalent to unlock//shm_detach ($shm _id);//close Shared Memory Association return $status;} /** * Add more than one record to a queue at a time * * @param mix $v * @return bool */public function AddAll ($v) {$shm _id = $this->shm_id; $sem _id = $th Is->sem_id;if (! ($sem _id && $shm _id)) Return False;sem_acquire ($sem _id); Possession of the semaphore, equivalent to lock, at the same time only one process run this code $val = Shm_get_var ($shm _id, $this->seq_key); Get values from shared memory if ($val && Is_array ($val)) {$val =array_merge ($val, $v);} else {$val = $v;} $status = Shm_put_var ($shm _id, $this->seq_key, $val); Writes the modified value to the shared memory Sem_release ($sem _id); Release the semaphore, which is equivalent to unlocking the return $status;}} /** * Synchronizing data to the old database * * @param int $cMD command * @param int $type type to synchronize * @param array $data synchronized data */function rsync_data ($cmd, $type, $data, & $code, & $msg {$code = 0; $msg = '; if ($cmd < Cmd_rsync_add | | $cmd > cmd_rsync_update) {$code = code::P aram_scale_err; $msg = "cmd {$cmd} is valid "; return false;} if ($type < Type_rsync_post | | $type > Type_rsync_user) {$code = code::P aram_scale_err; $msg = "type {$type} is valid" ; return false;} if (empty ($data)) {$code = code::P aram_is_null; $msg = "Data is empty"; return false;} Static $queue, if (Empty ($queue)) $queue = Msgqueue::init (ipc_key_file_export)->open (ipc_msgqueue_size); $queue- >add (' cmd ' = = $cmd, ' type ' = = $type, ' data ' = $data); return true; End of script
PHP Multi-process instances