Php timing script manager-PHP source code

Source: Internet
Author: User
Tags echo date server memory
Php timed script manager jumps

Server_ip = $ server_ip; $ this-> server_port = $ server_port; $ this-> job_path = $ job_path; $ this-> pipes = array (); $ this-> processes = array (); $ this-> log_file = $ log_file; $ this-> share_mem_file = $ share_mem_file;} // initialize socketpublic function init () {// start listening $ sock = @ socket_create (AF_INET, SOCK_STREAM, SOL_TCP); if (! $ Sock) {echo "socket_create () failed. \ n"; exit;} if (! Socket_set_option ($ sock, SOL_SOCKET, SO_REUSEADDR, 1) {echo "socket_set_option () failed. \ n"; exit ;}// bind the port and ipif (! ($ Ret = @ socket_bind ($ sock, $ this-> server_ip, $ this-> server_port) {echo "socket_bind () failed. \ n "; exit;} // wait for listening if (! ($ Ret = @ socket_listen ($ sock, 5) {echo "socket_listen () failed. \ n "; exit;} // Set $ this-> sock = $ sock; // Set the shared memory if (! File_exists ($ this-> pai_mem_file) {echo "share memory file not exists. \ n "; exit;} else {include_once ($ this-> pai_mem_file);} // initialize shared memory $ this-> 1__mem = new 1__memory ('shm _ key_of_server _'. $ this-> server_ip. '_'. $ this-> server_port); if (! $ This-> pai_mem-> attach () {echo "shm attach () failed. \ n "; exit ;}/// start listening for public function start () {// loop processing while (TRUE) {// wait for connection $ this-> server_echo ("Waiting for new command... "); $ this-> connect = @ socket_accept ($ this-> sock); if (! $ This-> connect) {$ this-> server_echo ("socket_accept () failed. \ n "); socket_write ($ this-> connect," socket_accept () failed. \ n "); break;} // read the input if (! ($ Input = @ socket_read ($ this-> connect, 1024) {$ this-> server_echo ("socket_read () failed. \ n "); socket_write ($ this-> connect," socket_read () failed. \ n "); break;} // analyze and execute the command $ input_arr = explode ('', trim ($ input); if (count ($ input_arr)> 1) {list ($ cmd, $ params) = explode ('', trim ($ input), 2);} else {$ cmd = $ input; $ params = '';} // Output Server content $ this-> server_echo (date ('Y-m-d H: I: s e '). "\ n $ cmd $ params \ n"); // The Traversal function is switc. H ($ cmd) {case 'status': // Get the process STATUS $ jobname = $ params; $ res = $ this-> backend_status ($ jobname ); socket_write ($ this-> connect, $ res ['MSG ']); break; case 'start': // enable the process $ params = explode ('', $ params ); $ params_len = count ($ params); if ($ params_len = 1) {// no input program path socket_write ($ this-> connect, 'params failed '); break;} $ jobname = array_shift ($ params); $ job_file = array_shift ($ params); $ script_cmd = $ this-> job_path. $ Job_file; $ res = $ this-> backend_start ($ jobname, $ script_cmd); socket_write ($ this-> connect, $ res ['MSG ']); break; case 'stop': // STOP process NAME 0 list ($ jobname, $ graceful) = explode ('', $ params ); $ res = $ this-> backend_stop ($ jobname, $ graceful); socket_write ($ this-> connect, $ res ['MSG ']); break; case 'servermem ': // read server memory usage $ mem = $ this-> my_memory_get_usage (); socket_write ($ this-> connect, $ mem); break; case 'Read': $ jobname = $ params; $ res = $ this-> pai_mem_read ($ jobname); socket_write ($ this-> connect, $ res ['MSG ']); break; case 'serverread': socket_write ($ this-> connect, implode ('', $ this-> server_output_buffer); break ;}}} // Obtain the path of the PHP parser running the current script: private function get_php_path () {return readlink ('/proc /'. getmypid (). '/exe');} // force end process private function force_stop_process ($ jobname) {$ this-> stop_process ($ jobname, FALSE );} // Elegant end process private function graceful_stop_process ($ jobname) {$ this-> stop_process ($ jobname, TRUE);} // end the process, and release the relevant resources private function stop_process ($ jobname, $ graceful) {if (! $ Graceful) {// force the process opened by proc_open to end $ status = proc_get_status ($ this-> processes [$ jobname]); exec ('kill-9 '. $ status ['pid']. '2>/dev/null> &->/dev/Null');} proc_terminate ($ this-> processes [$ jobname]); proc_close ($ this-> processes [$ jobname]); unset ($ this-> processes [$ jobname]);} // view the process status private function backend_status ($ jobname) {if (! Isset ($ this-> processes [$ jobname]) {// The process does not exist $ this-> server_echo ("DOWN. (process $ jobname does not exist .) \ n "); return array ('status' => false, 'MSG '=> 'drop ');} $ status = proc_get_status ($ this-> processes [$ jobname]); if (! $ Status) {$ this-> force_stop_process ($ jobname); $ this-> server_echo ("DOWN. (proc_get_status failed .) \ n "); return array ('status' => false, 'MSG '=> 'drop');} if ($ status ['running']) {$ this-> server_echo ("UP \ n"); return array ('status' => true, 'MSG '=> 'up ');} else {$ this-> server_echo ("DOWN \ n"); return array ('status' => false, 'MSG '=> 'low ');}} // enable the process private function backend_start ($ jobname, $ script_cmd) {// check If (isset ($ this-> processes [$ jobname]) {// Obtain the process status $ status = proc_get_status ($ this-> processes [$ jobname]); if (! $ Status) {$ this-> force_stop_process ($ jobname); $ this-> server_echo ("FAILED. (proc_get_status failed .) \ n "); return array ('status' => false, 'MSG '=>" FAILED. (proc_get_status failed .) \ n ") ;}// check whether the process is running if ($ status ['running']) {$ this-> server_echo (" FAILED. (process $ jobname has already exist .) \ n "); return array ('status' => false, 'MSG '=>" FAILED. (process $ jobname has already exist .) \ n ");} else {// stop $ this-> Force_stop_process ($ jobname) ;}} if (! File_exists ($ script_cmd) {// The file does not exist $ this-> server_echo ("FAILED. ($ script_cmd does not exist .) \ n "); return array ('status' => false, 'MSG '=>" FAILED. ($ script_cmd does not exist .) \ n ") ;}// run the background process $ descriptorspec = array (0 => array (" pipe "," r "), 1 => array (" pipe ", "w"), 2 => array ("file", $ this-> log_file, "a"); $ php_path = $ this-> get_php_path (); $ this-> processes [$ jobname] = proc_open ("{$ php_path} {$ script_cm D} ", $ descriptorspec, $ this-> pipes [$ jobname], dirname ($ script_cmd); if (! Is_resource ($ this-> processes [$ jobname]) {$ this-> server_echo ("FAILED. (proc_open failed .) \ n "); return array ('status' => false, 'MSG '=> 'failed. (proc_open failed .) ');} // Read in non-blocking mode $ output_pipe = $ this-> pipes [$ jobname] [1]; stream_set_blocking ($ output_pipe, 0 ); // record the number of Buffer rows $ extra_settings [$ jobname] = array ('bufferlines' => 10); // create a shared variable to store the output buffer $ output_buffer = array (); if (! $ This-> pai_mem-> put_var ($ jobname, $ output_buffer) {$ this-> server_echo ("shm put_var () failed. \ n "); return array ('status' => false, 'MSG '=>" shm put_var () failed. \ n ");} fclose ($ this-> pipes [$ jobname] [0]); // create a sub-process to read the process output $ pid = pcntl_fork (); if ($ pid =-1) {$ this-> server_echo ("pcntl_fork () failed. \ n "); return array ('status' => false, 'MSG '=>" pcntl_fork () failed. \ n ");} else if ($ pid) {// parent process $ child_pids [$ Jobname] = $ pid; pcntl_waitpid ($ t_pid, $ status); $ this-> server_echo ("start OK \ n"); return array ('status' => true, 'MSG '=> "SUCESS");} else {// create a new Sun Tzu process to avoid zombie processes $ t_pid = pcntl_fork (); if ($ t_pid =-1) {$ this-> server_echo ("pcntl_fork () failed. \ n "); return array ('status' => false, 'MSG '=>" pcntl_fork () failed. \ n ");} else if ($ t_pid) {// parent process exit ;} else {// Retrieve the output buffer in the shared memory $ output_buffer = $ this-> consumed _mem-> get_var ($ Jobname); while (TRUE) {$ read = array ($ output_pipe); $ write = NULL; $ response t = NULL; if (FALSE = ($ num_changed_streams = stream_select ($ read, $ write, $ response T, 3) {continue;} elseif ($ num_changed_streams> 0) {$ output = stream_get_contents ($ output_pipe); // cache output if ($ output! = '') {$ Buffer_lines = $ extra_settings [$ jobname] ['bufferlines'] + 1; $ output_lines = explode (" \ n ", $ output ); $ old_len = count ($ output_buffer); if ($ old_len> 0) {$ output_buffer [$ old_len-1]. = array_shift ($ output_lines) ;}$ output_buffer = array_merge ($ output_buffer, $ output_lines); $ output_buffer = bytes ($ output_buffer,-$ buffer_lines, $ buffer_lines ); // update the shared variable if (! $ This-> pai_mem-> put_var ($ jobname, $ output_buffer) {$ this-> server_echo ("shm put_var () failed. \ n ") ;}} else {break ;}} exit ;}}// end process // $ is_restart is a restart process. If yes, SOCKET does not output function backend_stop ($ jobname, $ graceful = FALSE) {if (! Isset ($ this-> processes [$ jobname]) {$ this-> server_echo ("FAILED. (process $ jobname does not exist .) \ n "); return array ('status' => false, 'MSG '=>" FAILED. (process $ jobname does not exist .) \ n ") ;}$ status = proc_get_status ($ this-> processes [$ jobname]); if (! $ Status) {$ this-> force_stop_process ($ jobname); $ this-> server_echo ("FAILED. (proc_get_status failed .) \ n "); return array ('status' => false, 'MSG '=>" FAILED. (proc_get_status failed .) \ n ");} if ($ graceful) {$ this-> graceful_stop_process ($ jobname);} else {$ this-> force_stop_process ($ jobname );} $ this-> server_echo ("OK \ n"); return array ('status' => true, 'MSG '=> 'sucess ');} // The server outputs private function server_echo ($ str) {$ This-> server_output_buffer [] = $ str; $ this-> server_output_buffer = array_slice ($ this-> server_output_buffer,-20, 20); echo $ str. "\ n";} // returns the actual memory occupied by the process. private function my_memory_get_usage () {$ pid = getmypid (); $ status = file_get_contents ("/proc/{$ pid}/status"); preg_match ('/VmRSS \: \ s + (\ d +) \ s + kB /', $ status, $ matches); $ vmRSS = $ matches [1]; return $ vmRSS * 1024;} // read process output buffer private function pai_mem_read ($ Jobname) {if (! Isset ($ this-> processes [$ jobname]) {// The process does not exist $ this-> server_echo ("NULL. (process does not exist .) \ n "); return array ('status' => false, 'MSG '=> 'process does not exist ');} $ status = proc_get_status ($ this-> processes [$ jobname]); if (! $ Status) {$ this-> force_stop_process ($ jobname); $ this-> server_echo ("NULL. (proc_get_status failed .) \ n "); return array ('status' => false, 'MSG '=> 'proc _ get_status failed ');} // Retrieve the output buffer in the shared memory $ output_buffer = $ this-> consumed _mem-> get_var ($ jobname); if ($ output_buffer) {$ content = implode ("\ n ", $ output_buffer ). "\ n"; return array ('status' => true, 'MSG '=> $ content);} else {return array ('status' => false, 'MSG '=>' There have not share mem! ');}}}

2. [File]Client. class. php ~ 2 KB
Server_ip = $ server_ip; $ this-> server_port = $ server_port;} // process status query: UP (normal), DOWN (on-premise) public function status ($ jobname) {return $ this-> _ cmd ("STATUS {$ jobname}");} // enable the new process OK (successful), FAILED (FAILED) public function start ($ jobname, $ script_cmd) {return $ this-> _ cmd ("START {$ jobname} {$ script_cmd}") ;}// the end process returns: OK (successful), FAILED (FAILED) public function stop ($ jobname, $ graceful = FALSE) {$ p2 = $ graceful? 1: 0; return $ this-> _ cmd ("STOP {$ jobname} {$ p2}");} // restart the process OK (successful), FAILED (FAILED) public function restart ($ jobname, $ graceful = FALSE) {$ p2 = $ graceful? 1: 0; return $ this-> _ cmd ("RESTART {$ jobname} {$ p2}");} // read the process server's output buffer and return: public function servermem () {return $ this-> _ cmd ("SERVERMEM");} // read process output buffer // return: process output buffer content public function read ($ jobname) {return substr ($ this-> _ cmd ("READ {$ jobname}"), 0,-1 );} // read the output buffer of the process server // return: public function serverread () {return $ this-> _ cmd ("SERVERREAD ");} // execute the command and return the result private function _ cmd ($ primitive) {if (! ($ Sock = @ socket_create (AF_INET, SOCK_STREAM, SOL_TCP) {return FALSE;} if (! @ Socket_connect ($ sock, $ this-> server_ip, $ this-> server_port) {return FALSE;} socket_write ($ sock, $ primitive ); $ rt = socket_read ($ sock, 1024); socket_close ($ sock); return $ rt ;}}

3. [File]Pai_memory.php ~ 3KB
Shm_key = $ this-> _ gen_key ($ shm_name); $ this-> shm_size = $ shm_size;}/** connect to shared memory ** @ return bool success TRUE, failed to FALSE */public function attach () {try {$ this-> shm_id = shm_attach ($ this-> shm_key, $ this-> shm_size ); // initialize the Semaphore $ this-> sem = sem_get ($ this-> shm_key, 1);} catch (Exception $ e) {return FALSE;} return TRUE ;} /** disconnect from shared memory ** @ return bool success TRUE, failure FALSE */public function detach () {shm_detach ($ this-> shm_id); return TRUE ;} /** delete shared memory ** @ return bool success TRUE, failure FALSE */public function remove () {shm_remove ($ this-> shm_id); return TRUE ;} /** write the variable name and corresponding value to the shared memory ** @ param string $ varname variable name * @ param string $ value variable value ** @ return bool write success TRUE, failure FALSE */public function put_var ($ varname, $ value) {$ varkey = $ this-> _ gen_key ($ varname); sem_acquire ($ this-> sem ); $ result = shm_put_var ($ this-> shm_id, $ varkey, $ value); sem_release ($ this-> sem); if ($ result) return TRUE ;} /** retrieve the variable value from the shared memory ** @ param string $ varname variable name ** @ return the value corresponding to the mixed variable name, FALSE */public function get_var ($ varname) {$ varkey = $ this-> _ gen_key ($ varname); return shm_get_var ($ this-> shm_id, $ varkey);}/** delete variable from shared memory ** @ param string $ varname variable name ** @ return mixed deleted successfully TRUE, FALSE */public function remove_var ($ varname) {$ varkey = $ this-> _ gen_key ($ varname); sem_acquire ($ this-> sem ); $ result = shm_remove_var ($ this-> shm_id, $ varkey); sem_release ($ this-> sem); return $ result ;} /** generate the key corresponding to the specified string ** @ param string $ name string ** @ return int key */private function _ gen_key ($ str) {// assume that the collision probability is relatively low. return hexdec (substr (md5 ($ str), 8, 8 ));}}

4. [File]Job_test.php ~ 507B
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.