dsqdpn31467 2013-05-20 07:07
浏览 117
已采纳

php,如何在后台为消费者创建/保持服务?

im running an ec2 instance, with ubuntu 12.04.

i am using aws infastructure, and right now im trying to implement a consumer worker which will consume an sqs queue (queue of msgs, not very important).

to do that i created a php file, which "harvest" the queue for 30 seconds. on top i have a crontab running this page every 30 seconds.

what is a more elegant/proper solution? how do i make a background php proccess, and how i do check if its alive, and kill it restart it, if needed?

  • just a clarification - i am asking about the process which consumes the queue, regardless of which/what mechanizem is selected (sqs, rabbitMQ)

thanks for your help

  • 写回答

2条回答 默认 最新

  • dsfdsfds521521 2013-05-23 17:22
    关注

    create a monitor script utilizing the class process (which i copied from php docs), which launches your worker script if the process is not running. the monitor script can keep the pid in a file.

    then simply add the monitor to crontab.

    class Process{
        private $pid;
        private $command;
    
        public function __construct($cl=false){
            if ($cl != false){
                $this->command = $cl;
                $this->runCom();
            }
        }
        private function runCom(){
            $command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!';
            exec($command ,$op);
            $this->pid = (int)$op[0];
        }
    
        public function setPid($pid){
            $this->pid = $pid;
        }
    
        public function getPid(){
            return $this->pid;
        }
    
        public function status(){
            $command = 'ps -p '.$this->pid;
            exec($command,$op);
            if (!isset($op[1]))return false;
            else return true;
        }
    
        public function start(){
            if ($this->command != '')$this->runCom();
            else return true;
        }
    
        public function stop(){
            $command = 'kill '.$this->pid;
            exec($command);
            if ($this->status() == false)return true;
            else return false;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效