A-A+

关于php守护进程的配置及实例

2018年01月26日 PHP技术文章 暂无评论 阅读 0 views 次

其实守护进程看起来很复杂的样子,其实做起来也是很简单的,前文已经写到了,其实就是一个死循环而已,程序代码不难,重点在于php守护进程的配置文件有点麻烦,想要理解起来确实要费一点脑劲,我用的是tp系统,所以首先需要先搞一个配置文件,在根目录下新建一个 webservice 文件夹,入口文件为 index.php,这个就不用说了。

另外还有两个文件,一个是 webservice.php,主要是php守护进程的一些function方法,文件如下:

  1. #!/usr/bin/env php  
  2.   
  3. <?php  
  4. /** 
  5.  * @file webservice.php 
  6.  *  
  7.  * 网站服务管理脚本说明 
  8.  * 
  9.  * 1. 首先需要根据实际环境对脚本进行配置,请参考 config.php 文件的 Config::getServiceConfig() 方法 
  10.  * 2. 调用方法: /path/to/php /path/to/webservice/webservice.php [--help|--force-reboot-all|--force-stop-all] [>> /tmp/webservice.log &] 
  11.  *  
  12.  * @author lgh_2002@163.com 
  13.  * @version v3(修正) 
  14.  * @date 2014-11-04 
  15.  */  
  16.   
  17. class websiteService  
  18. {  
  19.     /** 
  20.      *  单例模式 
  21.      *  @string public 
  22.      */  
  23.     static public $_instance = NULL;  
  24.   
  25.     /** 
  26.      *  参数命令 
  27.      *  @string public 
  28.      */  
  29.     public $command = "";  
  30.   
  31.     /** 
  32.      *  脚本运行环境:dev|branches|trunk 
  33.      *  @access protected 
  34.      */  
  35.     protected $_env = "";  
  36.   
  37.     /** 
  38.      *  脚本执行绝对路径 
  39.      *  @access protected 
  40.      */  
  41.     protected $_script_path = "";  
  42.   
  43.     /** 
  44.      *  服务进程标识符 
  45.      *  @access protected 
  46.      */  
  47.     protected $_service_process = "";  
  48.   
  49.     /** 
  50.      *  应用名称 
  51.      *  @access protected 
  52.      */  
  53.     protected $_app_name = "chengdun";  
  54.   
  55.     /** 
  56.      *  是否显示签名 
  57.      *  @access public   
  58.      */  
  59.     public $show_signature = true;  
  60.   
  61.     /** 
  62.      *  某个服务 
  63.      *  @access protected 
  64.      */  
  65.     public $one_service = '';  
  66.   
  67.     /** 
  68.      *  本脚本文件名 
  69.      *  @access protected 
  70.      */  
  71.     public $php_self = '';  
  72.       
  73.     /** 
  74.      *  日志文件名 
  75.      *  @access protected 
  76.      */  
  77.     protected $_log_filename = '';    
  78.   
  79.     /** 
  80.      * 构造函数   
  81.      * 
  82.      * @return   
  83.      */  
  84.     public function __construct($command = ""$show_signature = true)  
  85.     {  
  86.         $this->command = $command;  
  87.         $this->show_signature = $show_signature;  
  88.         $this->php_self = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/')+1);  
  89.     }  
  90.   
  91.     /** 
  92.      * 析构函数  
  93.      * 
  94.      * @return 
  95.      */  
  96.     public function __destruct()  
  97.     {  
  98.         $this->command = '';  
  99.     }  
  100.   
  101.     /** 
  102.      * 脚本运行入口  
  103.      * 
  104.      * @return null  
  105.      */  
  106.     public function execute()  
  107.     {  
  108.         //签名  
  109.         if($this->show_signature) self::showSignature();  
  110.   
  111.         //命令使用帮助  
  112.         if(emptyempty($this->command) || "--help" === $this->command) $this->showHelp();  
  113.   
  114.         $now_time = self::getNowTime();  
  115.         $msg = $now_time . "\033[33m脚本运行开始......\033[33m";  
  116.         $this->showMsg($msg);  
  117.   
  118.         //校验执行锁: 对于写操作-脚本本身必须以单例模式运行  
  119.         if("--check" != $this->command)  
  120.         {  
  121.             $lock_exist = $this->isLockExist();  
  122.   
  123.             if($lock_exist)  
  124.             {  
  125.                 $msg = "检测到有【webservice.php】进程在运行, 请核实...拒绝服务...";  
  126.                 $this->showMsg($msg, false, true);   
  127.             }  
  128.         }  
  129.   
  130.         //创建必须的日志目录  
  131.         try{  
  132.             if(!file_exists(LOG_PATH) || !is_dir(LOG_PATH)) self::createMultiDirectory(LOG_PATH);  
  133.         }catch(Exception $e){  
  134.             $msg ='日志目录' . LOG_PATH . "不可写,请提供可写的日志目录!!!";  
  135.             $this->showMsg($msg, false, true);  
  136.         }  
  137.   
  138.         $config = $this->washConfig();  
  139.         if(emptyempty($config) || !is_array($config))  
  140.         {  
  141.             $msg = "检测到异常:无效操作,配置文件内容无效或提供的命令参数无效";  
  142.             $this->showMsg($msg, false, true);   
  143.         }  
  144.   
  145.         foreach($config as $app => $data)  
  146.         {  
  147.             $now_time = self::getNowTime();  
  148.             $msg = $now_time . "\033[33m开始检测项目【" . $app . "】上的服务......\033[33m";  
  149.             $this->showMsg($msg);  
  150.             $this->_app_name = $app;  
  151.             sleep(1);  
  152.   
  153.             foreach($data as $k => $arg)  
  154.             {  
  155.                 $index = $k + 1;  
  156.                 $now_time = self::getNowTime();  
  157.   
  158.                 if(emptyempty($arg['env']))  
  159.                 {  
  160.                     $this->_env = "";  
  161.                     $cn_env = "【默认】环境";  
  162.                 }  
  163.                 else  
  164.                 {  
  165.                     $this->_env = $arg['env'];  
  166.                     $cn_env = "【" . $arg['env'] . "】环境";  
  167.                 }  
  168.   
  169.                 $this->_script_path = $arg['path'];  
  170.                 $this->_service_process = trim($arg['process']);  
  171.                 $this->_log_filename = trim($arg['log_name']);  
  172.                 $msg = $now_time . "\033[33m正在检测" . $cn_env . "第(" . $index . ")项服务:【" . $this->_service_process . "】\033[33m";  
  173.                 $this->showMsg($msg, true);  
  174.   
  175.                 if(!is_file($this->_script_path) || !file_exists($this->_script_path))  
  176.                 {  
  177.                     $tmp_script_path = emptyempty($this->_script_path) ? '' : "【" . $this->_script_path . "】";  
  178.                     $msg = $now_time . "\033[33m检测结果: 所需脚本文件" . $tmp_script_path"不存在\033[33m";  
  179.                     $this->showMsg($msg, true);  
  180.                     continue;  
  181.                 }  
  182.   
  183.                 //检测服务进程  
  184.                 $pids = $this->_checkService();  
  185.   
  186.                 //是否有强制行为  
  187.                 if(in_array($this->command, array('--force-reboot-all', '--force-stop-all')))  
  188.                 {  
  189.                     '--force-stop-all' === $this->command && $action = 2;  
  190.                     '--force-reboot-all' === $this->command && $action = 3;  
  191.                 }  
  192.                 else  
  193.                 {  
  194.                     $now_time = self::getNowTime();  
  195.   
  196.                     if(emptyempty($pids)) {  
  197.                         $msg = $now_time . "\033[33m检测结果:服务没有启动:【" . $this->_service_process. "】\033[33m";  
  198.                     } else {  
  199.                         $pids = "【进程ID:" . join("|"$pids) . "】";  
  200.                         $msg = $now_time . "\033[33m检测结果: 服务正常【" . $this->_service_process . "】 - {$pids} \033[33m";  
  201.                     }  
  202.   
  203.                     $this->showMsg($msg, true);  
  204.   
  205.                     if('--check' === $this->command)  
  206.                     {  
  207.                         continue;  
  208.                     }  
  209.   
  210.                     if(false !== strpos($this->command, "--human"))  
  211.                     {  
  212.                         echo "\r\n" . SPACE . "确定要继续操作吗? 【y|n】";  
  213.   
  214.                         $op = trim($this->confirm());  
  215.   
  216.                         //如果拒绝继续操作,则校验下一个服务  
  217.                         if('y' != $op)   
  218.                         {  
  219.                             echo "\r\n";  
  220.                             continue;  
  221.                         }  
  222.   
  223.                         echo "\r\n" . SPACE . "请选择操作行为:【1/2/3/4】\r\n\r\n";  
  224.                         $action = trim($this->selectAction()->confirm());  
  225.                     }  
  226.                     else  
  227.                     {  
  228.                         $action = $this->getAction();  
  229.                     }  
  230.                 }  
  231.   
  232.                 switch($action)  
  233.                 {  
  234.                     case 1:  
  235.                         //正常启动  
  236.                         $now_time = self::getNowTime();  
  237.                         $pid = $this->startService();  
  238.                         $msg = "启动服务失败:【" . $this->_service_process . "】, 跳过操作......";  
  239.                         $pid && $msg = "已成功启动服务:【" . $this->_service_process. "】-【" . $pid . "】";  
  240.                         $this->showMsg($msg, true);  
  241.                         break;  
  242.                     case 2:  
  243.                         //停止  
  244.                         $this->stopMultiService();  
  245.                         break;  
  246.                     case 3:  
  247.                         //重启  
  248.                         $this->stopMultiService();  
  249.                         $msg = "准备重新启动服务:【" . $this->_service_process. "】";  
  250.                         $pid = $this->showMsg($msg, true)->startService();  
  251.                         $msg = "重新启动服务失败:【" . $this->_service_process. "】";  
  252.                         $pid && $msg = "重新启动服务成功:【" . $this->_service_process. "】-【" . $pid . "】";  
  253.                         $this->showMsg($msg, true);  
  254.                         break;  
  255.                     case 4:  
  256.                         //退出  
  257.                         $msg = "退出行为,跳过操作......";  
  258.                         $this->showMsg($msg, true);  
  259.                         break;  
  260.                     default:  
  261.                         //默认  
  262.                         $msg = "无效操作,跳过操作......";  
  263.                         $this->showMsg($msg, true);  
  264.                         break;  
  265.                 }  
  266.   
  267.                 //休眠2秒  
  268.                 $sleep_time = 2;  
  269.                 $msg = "系统正在休眠,请等待{$sleep_time}秒......";  
  270.                 $this->showMsg($msg, true);  
  271.                 sleep($sleep_time);  
  272.                 echo "\r\n";  
  273.             }  
  274.         }  
  275.   
  276.         $now_time = self::getNowTime();  
  277.         $msg = $now_time . "\033[33m脚本运行结束, 安全退出......\033[33m";  
  278.         $this->showMsg($msg, false, true);  
  279.     }  
  280.   
  281.     /** 
  282.      * 获取命令行为 
  283.      * 
  284.      * @return int 
  285.      */  
  286.     public function getAction()  
  287.     {  
  288.         if(false !== strpos ($this->command, "start")){  
  289.             $action = 1;  
  290.         }else if(false !== strpos ($this->command, "stop")){  
  291.             $action = 2;  
  292.         }else if(false !== strpos ($this->command, "reboot")){  
  293.             $action = 3;  
  294.         }else{  
  295.             $action = 0;  
  296.         }  
  297.   
  298.         return $action;  
  299.     }  
  300.   
  301.     /** 
  302.      * 启动服务  
  303.      * 
  304.      * @return int  
  305.      */  
  306.     public function startService()  
  307.     {  
  308.         $output = $this->_checkService();  
  309.   
  310.         if(emptyempty($output) || !is_array($output))   
  311.         {  
  312.               
  313.             $cmd = "nohup " . PHP_BIN . " $this->_script_path  $this->_service_process >> " . LOG_PATH . "{$this->_log_filename}.log 2>&1 & ";  
  314.             exec($cmd);  
  315.   
  316.             $output = $this->_checkService();  
  317.         }  
  318.   
  319.         $pid = emptyempty($output) ? 0 : (int)trim($output[0]);  
  320.   
  321.         return $pid;  
  322.     }  
  323.   
  324.     /** 
  325.      * 停止服务  
  326.      * 
  327.      * @return bool  
  328.      */  
  329.     public function stopService($pid)  
  330.     {  
  331.         if($pid <= 0) return false;  
  332.   
  333.         $cmd = "kill -9 {$pid} > /dev/null &";  
  334.   
  335.         exec($cmd$output$status);  
  336.   
  337.         if(0 === $statusreturn true;  
  338.   
  339.         return false;  
  340.     }  
  341.   
  342.     /** 
  343.      * 停止多个服务  
  344.      * 
  345.      * @return mixed  
  346.      */  
  347.     public function stopMultiService()  
  348.     {  
  349.         $pids = $this->_checkService();  
  350.   
  351.         if(emptyempty($pids) || !is_array($pids))  
  352.         {  
  353.             $msg = "停止服务失败,检测到服务没有启动:【" . $this->_service_process. "】";  
  354.             $this->showMsg($msg, true);  
  355.             return false;  
  356.         }  
  357.   
  358.         foreach($pids as $k => $pid)  
  359.         {  
  360.             $msg = "当前进程操作无效:【" . $this->_service_process. "】" . "-" . "【" . $pid"】";  
  361.             $rs = $this->stopService($pid);  
  362.   
  363.             if($rs)  
  364.             {  
  365.                 $msg = "当前进程已杀死:【" . $this->_service_process. "】" . "-" . "【" . $pid"】";  
  366.             }  
  367.   
  368.             $this->showMsg($msg, true);  
  369.         }  
  370.     }  
  371.   
  372.     /** 
  373.      * 检测服务进程: 返回进程ID集合 
  374.      * 
  375.      * @return array  
  376.      */  
  377.     protected function _checkService()  
  378.     {  
  379.         //注意!!!:防止进程没有及时退出,这里必须休眠一会儿  
  380.         sleep(2);  
  381.   
  382.         $enviroment = emptyempty($this->_env) ? "" : "| grep " . $this->_env;  
  383.         $cmd = "ps aux | grep -v " . $this->php_self . " | grep " . $this->_app_name . "| grep " . $this->_service_process . $enviroment . "| grep -v grep | awk '{print $2}'";  
  384.   
  385.         /* 
  386.          *$msg = "执行命令如下:" . $cmd; 
  387.          *$this->showMsg($msg); 
  388.          */  
  389.   
  390.         exec($cmd$pids);  
  391.   
  392.         return $pids;  
  393.     }  
  394.   
  395.     /** 
  396.      * 消息弹窗 
  397.      * 
  398.      * @param  int  $msg 
  399.      * @param  int  $indent 
  400.      * @param  int  $exit 
  401.      * 
  402.      * @return mixed 
  403.      */  
  404.     public function showMsg($msg$indent = false, $exit = false)  
  405.     {  
  406.         emptyempty($msg) && $msg = "unknow msg....";  
  407.         !is_string($msg) && $msg = json_encode($msg);  
  408.         $indent_content = "";  
  409.         $indent && $indent_content = SPACE;  
  410.         $sign = str_repeat("-", 110);  
  411.         echo START_COLOR . $indent_content . $sign . END_COLOR;  
  412.         echo START_COLOR . $indent_content . $msg . END_COLOR;  
  413.   
  414.         if($exit)   
  415.         {  
  416.             echo START_COLOR . $indent_content . $sign . END_COLOR;  
  417.             exit;  
  418.         }  
  419.   
  420.         return $this;  
  421.     }  
  422.   
  423.     /** 
  424.      * 消息确认: 流  
  425.      * 
  426.      * @return string   
  427.      */  
  428.     public function confirm()  
  429.     {  
  430.         $fp = fopen('php://stdin', 'r');  
  431.         $input = fgets($fp, 255);  
  432.         fclose($fp);  
  433.   
  434.         return $input;  
  435.     }  
  436.   
  437.     /** 
  438.      * 操作行为分支  
  439.      * 
  440.      * @return  object  
  441.      */  
  442.     public function selectAction()  
  443.     {  
  444.         $a = "start";  
  445.         $b = "stop";  
  446.         $c = "reboot";  
  447.         $d = "exit";  
  448.   
  449.         echo START_COLOR . SPACE . "==================================================" . END_COLOR;  
  450.         echo START_COLOR . SPACE . "(1) $a" . END_COLOR;  
  451.         echo START_COLOR . SPACE . "(2) $b" . END_COLOR;  
  452.         echo START_COLOR . SPACE . "(3) $c" . END_COLOR;  
  453.         echo START_COLOR . SPACE . "(4) $d" . END_COLOR;  
  454.         echo START_COLOR . SPACE . "==================================================" . END_COLOR;  
  455.         echo "\r\n";  
  456.   
  457.         return $this;  
  458.     }  
  459.   
  460.     /** 
  461.      * 获取当前系统时间  
  462.      * 
  463.      * @return string 
  464.      */  
  465.     public function getNowTime()  
  466.     {  
  467.         $now_time = date("Y-m-d H:i:s");  
  468.         $now_time = "\033[33m【" . $now_time . "】\033[33m";  
  469.   
  470.         return $now_time;  
  471.     }  
  472.   
  473.     /** 
  474.      * 单进程执行锁 
  475.      * 
  476.      * @return bool 
  477.      */  
  478.     public function isLockExist()  
  479.     {  
  480.         $cmd = "ps aux | grep -i 'webservice.php' | grep -v grep | grep -v vi | grep -v vim | wc -l";  
  481.         exec($cmd$result);  
  482.   
  483.         //运行锁  
  484.         if(!emptyempty($result) && $result[0] > 1)  
  485.         {  
  486.             return true;  
  487.         }  
  488.   
  489.         return false;  
  490.     }  
  491.   
  492.   
  493.     /**   
  494.      * 创建多级目录 
  495.      * 
  496.      * @param  string  $dir 层级目录: a/b/c 
  497.      * @param  string  $mode 权限 
  498.      * 
  499.      * @return boolean 
  500.      */  
  501.     static public function createMultiDirectory($dir$mode = 0777)  
  502.     {      
  503.         if(is_dir($dir))  
  504.         {      
  505.             return true;  
  506.         }      
  507.   
  508.         if(!self::createMultiDirectory(dirname($dir), $mode))  
  509.         {      
  510.             return false;  
  511.         }      
  512.   
  513.         return mkdir($dir$mode);  
  514.     }     
  515.   
  516.     /** 
  517.      * 脚本使用帮助信息 
  518.      * 
  519.      * @return exit 
  520.      */  
  521.     public function showHelp()  
  522.     {  
  523.         $config = Config::getServiceConfig();  
  524.   
  525.         $head = SERVICE_SPACE . "【项目名称】      【服务名称】     ". SERVICE_SPACE . SERVICE_SPACE . SERVICE_SPACE ."【备注说明】\r\n";  
  526.         $service_list = SERVICE_SPACE;  
  527.         foreach($config as $app => $v)  
  528.         {  
  529.             foreach($v as $k1 => $v1)  
  530.             {  
  531.                 $v1['remark_space'] <= 0 && $v1['remark_space'] = 1;  
  532.                 $remark_space = $v1['remark_space'] <= 0 ? 1 : str_repeat(" "$v1['remark_space']);  
  533.                 $service_list .= " {$app}" . SERVICE_SPACE . $v1['process'] . SERVICE_SPACE . $remark_space . $v1['remark'] . "\r\n" . SERVICE_SPACE;  
  534.             }  
  535.         }  
  536.         $service_list = $head . $service_list;  
  537.   
  538.         $self_script = $_SERVER['PHP_SELF'];  
  539.         $sign = str_repeat("=", 136) . "\r\n";  
  540.         echo $sign;  
  541.         echo "\r\n";  
  542.         echo "     全站服务管理脚本使用说明\r\n";  
  543.         echo "\r\n";  
  544.         echo "     1. 首先请根据实际环境对 /path/to/webservice/config.php 脚本进行配置, 请参考 Config::getServiceConfig() 方法\r\n";  
  545.         echo "\r\n";  
  546.         echo "     2. 调用方法:/path/to/php /path/to/" . $self_script . " [--help|--check|--force-reboot-all|--force-stop-all] [>> /tmp/webservice.log &]\r\n";  
  547.         echo "\r\n";  
  548.         echo "     3. 每一个服务进程都有对应的运行日志记录, 存放路径请参考 LOG_PATH 定义段 \r\n";  
  549.         echo "\r\n";  
  550.         echo "     4. 参数说明:\r\n";  
  551.         echo "\r\n";  
  552.         echo "        --help                    打印脚本使用说明\r\n";  
  553.         echo "        --check                   检测所有配合服务运行情况 \r\n";  
  554.         echo "        --check-server-name       检测 server-name 服务运行情况 \r\n";  
  555.         echo "        --human                   启用交互模式操作服务\r\n";  
  556.         echo "        --human-server-name       启用交互模式操作 server-name 服务\r\n";  
  557.         echo "        --start-server-name       启动 server-name 服务\r\n";  
  558.         echo "        --stop-server-name        停止 server-name 服务\r\n";  
  559.         echo "        --reboot-server-name      启动 server-name 服务\r\n";  
  560.         echo "        --force-reboot-all        强制重启所有服务 \r\n";  
  561.         echo "        --force-stop-all          强制停止所有服务 \r\n";  
  562.         echo "\r\n";  
  563.         echo "     5. 可用的服务列表: \r\n";  
  564.         echo "\r\n";  
  565.         echo $service_list;  
  566.         echo "\r\n";  
  567.         echo $sign;  
  568.         echo "\r\n";  
  569.         exit;  
  570.     }  
  571.   
  572.     /** 
  573.      * 签名  
  574.      * 
  575.      * @return string 
  576.      */  
  577.     static public function showSignature()  
  578.     {  
  579.         $timer = 0;  
  580.         $data = '';  
  581.         $total = 62;  
  582.         while($timer <= $total)  
  583.         {  
  584.             $data = $timer == $total/2 ? "全站服务管理" : "▆";  
  585.             echo $data;  
  586.             usleep(10000);  
  587.             $timer++;  
  588.         }  
  589.         echo "\r\n";  
  590.     }  
  591.   
  592.     /** 
  593.      * 清洗配置文件 
  594.      * 
  595.      * @return   
  596.      */  
  597.     public function washConfig()  
  598.     {  
  599.         $config = array();  
  600.   
  601.         //读取配置文件  
  602.         $_config = Config::getServiceConfig();  
  603.   
  604.         if(in_array($this->command, array('--force-reboot-all', '--force-stop-all', '--check', '--human')))  
  605.         {  
  606.             return $_config;  
  607.         }  
  608.   
  609.         //重新清洗配置文件  
  610.         $pos = strpos($this->command, "-", 2);  
  611.         $command = substr($this->command, $pos+1);  
  612.         foreach($_config as $k => $v)  
  613.         {  
  614.             foreach($v as $k1 => $v1)  
  615.             {  
  616.                 if($command == $v1['process'])  
  617.                 {  
  618.                     $config[$k][$k1] = $v1;  
  619.                 }  
  620.             }  
  621.         }  
  622.   
  623.         return $config;  
  624.     }  
  625.   
  626.     /** 
  627.      * 获取单例 
  628.      * 
  629.      * @return object  
  630.      */  
  631.     static public function getInstance($command = ''$show_signature = true)  
  632.     {  
  633.         if(!self::$_instance instanceof self)  
  634.         {  
  635.             self::$_instance = new self($command$show_signature);  
  636.         }  
  637.   
  638.         return self::$_instance;  
  639.     }  
  640. }  
  641. //www.xiariboke.net  
  642. //加载配置文件  
  643. if(!file_exists(dirname(__FILE__) . "/config.php")) exit("配置文件加载失败......, 不存在?请检查下!!!\n\n");  
  644. require(dirname(__FILE__) . "/config.php");  
  645.   
  646. //开始执行脚本  
  647. $command = isset($argv[1]) ? $argv[1] : "";  
  648. websiteService::getInstance($command, SHOW_SIGNATURE)->execute();  

这个是很关键的一个文件,如果要启动某个php守护进程的话,是需要调用这里面的方法的,看着这个方法有些复杂,其实执行下来,是很直观的,我们再来看一下config.php配置文件,主要是用来配置进程的,在这个里面来启动进程,配置文件如下:

  1. <?php  
  2. /** 
  3.  * @file config.php 
  4.  *  
  5.  * @author lgh_2002@163.com 
  6.  * @version v3(修正) 
  7.  * @date 2014-11-04 
  8.  */  
  9.   
  10. PHP_SAPI == 'cli' or die('拒绝服务!!');  
  11. set_time_limit(0);  
  12. ignore_user_abort(true);  
  13. date_default_timezone_set('Asia/Shanghai');  
  14.   
  15. define("WEBSERVICE_ROOT_PATH", dirname(__FILE__));  
  16. define("START_COLOR""\033[32m");  
  17. define("END_COLOR""\033[0m\r\n");  
  18. define("SPACE""                  ");  
  19. define("SERVICE_SPACE""         ");  
  20. define("PHP_BIN""/application/php/bin/php ");  
  21. !defined("SHOW_SIGNATURE") && define("SHOW_SIGNATURE", false);  
  22. !defined("LOG_PATH") && define("LOG_PATH""/tmp/websiteService/");  
  23.   
  24. class Config  
  25. {  
  26.     /** 
  27.      *  获取配置信息  
  28.      * 
  29.      * @return array  
  30.      */  
  31.     static public function getServiceConfig()  
  32.     {  
  33.         //注意:脚本路径一定要配置正确  
  34.         $path = array(  
  35.             "p1" => "/application/nginx/html/www/chengdun/webservice/index.php",  
  36.         );  
  37.   
  38.         $config = array(  
  39.             /*'chengdun' => array( 
  40.                 array( 
  41.                     "env"   => "", 
  42.                     "path"  => $path['p1'], 
  43.                     "process" => "Home-Index-demo", 
  44.                     "remark" => "测试", 
  45.                     "remark_space" => 6, 
  46.                     'log_name' => 'demo', 
  47.                 ), 
  48.  
  49.                 array( 
  50.                     "env"   => "branches", 
  51.                     "path"  => $path['p1'], 
  52.                     "process" => "default-daemon-secondaryrepay", 
  53.                 ), 
  54.                 array( 
  55.                     "env"   => "branches", 
  56.                     "path"  => $path['p1'], 
  57.                     "process" => "default-daemonfastrepay-run", 
  58.                 ), 
  59.                 array( 
  60.                     "env"   => "branches", 
  61.                     "path"  => $path['p1'], 
  62.                     "process" => "default-daemon-remind", 
  63.                 ), 
  64.                 array( 
  65.                     "env"   => "branches", 
  66.                     "path"  => $path['p1'], 
  67.                     "process" => "default-daemon-luckchance", 
  68.                 ), 
  69.                 array( 
  70.                     "env"   => "dev", 
  71.                     "path"  => $path['p3'], 
  72.                     "process" => "admin-daemon-sms", 
  73.                 ), 
  74.                 array( 
  75.                     "env"   => "dev", 
  76.                     "path"  => $path['p3'], 
  77.                     "process" => "admin-daemon-email", 
  78.                 ), 
  79.             ),*/  
  80.   
  81.             /*'baibaodai' => array( 
  82.                 array( 
  83.                     "env"   => "dev", 
  84.                     "path"  => $path['p2'], 
  85.                     "process" => "default-daemon-secondaryrepay", 
  86.                 ), 
  87.                 array( 
  88.                     "env"   => "dev", 
  89.                     "path"  => $path['p2'], 
  90.                     "process" => "default-daemonfastrepay-run", 
  91.                 ), 
  92.                 array( 
  93.                     "env"   => "dev", 
  94.                     "path"  => $path['p2'], 
  95.                     "process" => "default-daemon-remind", 
  96.                 ), 
  97.             ),*/  
  98.             'Credit' => array(  
  99.                 array(  
  100.                     "env"   => "",  
  101.                     "path"  => $path['p1'],  
  102.                     "process" => "Credit-Daemon-insuredata",  
  103.                     "remark" => "同步保函",  
  104.                     "remark_space" => 13,  
  105.                     'log_name' => 'insuredata_log',  
  106.                 ),  
  107.                 array(  
  108.                     'env' => "",  
  109.                     'path' => $path['p1'],  
  110.                     "process" => "Credit-Daemon-giftCoupon",  
  111.                     "remark" => "同步礼券",  
  112.                     "remark_space" => 13,  
  113.                     'log_name' => 'giftCoupon_log',  
  114.                 ),  
  115.                 array(  
  116.                     'env' => "",  
  117.                     'path' => $path['p1'],  
  118.                     "process" => "Credit-Daemon-company",  
  119.                     "remark" => "同步公司信息",  
  120.                     "remark_space" => 16,  
  121.                     'log_name' => 'company_log',  
  122.                 ),                
  123.             ),  
  124.             'Angent' => array(  
  125.                 array(  
  126.                     "env"   => "",  
  127.                     "path"  => $path['p1'],  
  128.                     "process" => "Angent-Daemon-spend_count",  
  129.                     "remark" => "更新消费总额",  
  130.                     "remark_space" => 12,  
  131.                     'log_name' => 'spend_count_log',  
  132.                 ),  
  133.                 array(  
  134.                     "env"   => "",  
  135.                     "path"  => $path['p1'],  
  136.                     "process" => "Angent-Daemon-give_experience",  
  137.                     "remark" => "处理用户经验",  
  138.                     "remark_space" => 12,  
  139.                     'log_name' => 'give_experience_log',  
  140.                 ),  
  141.                 array(  
  142.                     "env"   => "",  
  143.                     "path"  => $path['p1'],  
  144.                     "process" => "Angent-Daemon-return_insure_subsidy",  
  145.                     "remark" => "",  
  146.                     "remark_space" => 12,  
  147.                     'log_name' => 'return_insure_subsidy',  
  148.                 ),                
  149.             ),  
  150.         );  
  151. //www.xiariboke.net  
  152.         return $config;  
  153.     }  
  154.   
  155. }  

好了,到此为止,php守护进程配置文件以及方法文件就怎么多了,好了之后,我们就可以在这个基础上去写控制器文件了,其实就是public function一个死循环文件而已,看下面一个具体实例:

  1. /** 
  2.  * 处理保函补贴 
  3.  */  
  4. public function return_insure_subsidy(){  
  5.     while(true){  
  6.         $row = \Helper_Redis::instance()->pop('list_mapi_insure_subsidy');  
  7.         if($row){  
  8.             $res = D('Angent/InsureData')->return_insure_subsidy($row['company_id'], $row['product_id'], $row['money'], $row['id']);  
  9.             pprint($row);  
  10.         }//www.xiariboke.net  
  11.         usleep(100000);  
  12.     }  
  13. }  

我们只要打开这个守护进程,这个程序便会一直循环下去,只要不stop,便不会停止,只要有订单数据,就会马上同步,像这种守护进程,更多的适合用在执行同步发送邮件,或者短信,以及其它一些及时同步的应用上面。

标签:

给我留言