phalcon queueing使用心得

本來沒有用過phalcon的消息隊列,原本覺得很簡單,結果搞了半天,把步驟記錄一下。php

  1. phalcon的官網上沒有說須要安裝beanstalkd,我猜寫文檔的覺得我等默認會安裝,找了半天沒有找到windows下的beanstalk,有個國人寫的beanstalkd-win,沒敢用。
  2. 找了一臺Ubuntu,安裝上了beanstalkd,命令: sudo apt-get install beanstalkd
  3. 啓動:beanstalkd -l 10.0.1.5 -p 11300
  4. 打開11300端口,命令:iptables -A INPUT -ptcp –dport 11300 -j ACCEPT
  5. https://github.com/shruthi-ananth/phalcon-beanstalk 這裏有個例子,用來學習phalcon和beanstalk的集成,
    • Once you have Beanstalk server up, you can run producer and worker tasks in the console using ‘php console.php producer main’ and ‘php console.php worker main’. The producer puts a job on the queue and that works fine. The worker(Phalcon implementation) does not pick up a job from the queue to execute unlike the PheanstalkWorker Task.(‘php console.php pheanstalkworker main’). You use ‘php console.php pheanstalkstats main’ to check the server stats and keep both workers running to see which worker picks the job.
  6. 把例子程序裏的全部beanstalkd的host和端口號改一下。
  7. 開四個cmd,一個運行php console.php worker main,一個運行 php console.php pheanstalkworker main, 一個運行php console.php pheanstalkstats main ,最後一個運行php console.php producer main。就能夠看到任務寫入,任務執行過程了
  8. 基本上phalcon的消息隊列環境就搭好了。
  9. 照着phalcon官網上的例子寫,就能夠了:https://docs.phalconphp.com/zh/latest/reference/queue.html

 

Note:html

  1. 抄了一段別人寫的代碼,  若是把worker的這段代碼用supervisor作個守護進程,就更好了。參考http://www.phpddt.com/php/supervisor.html
    • This is not really Phalcon related but here it goes anyway. This shows a simple example of using the queue to generate a response. Your API endpoint only creates a job in the queue and can return immediately.When the item is picked from the queue the request to the external API is made and when a reponse is recieved it calls the callback supplied by the original callee. This example could be improved by splitting the job in two, one job for external api retrieval and a seperate job for the callback. This way the extarnal Api isn’t called agian when the callback fails. This also allows for multiple callback attempts by keeping a counter for failed deliveries.
    • ApiController
      public function myApiEndpointAction($myArgument, $callbackUrl) {
          $queue->put(array(
              'jobName' => $myArgument,
              'callbackUrl' => $callbackUrl
          ));
      
          return new Reponse("Item put in queue", 201);
      }

      Workerpython

      while (($job = $queue->peekReady()) !== false) {
      
          $jobBody = $job->getBody();
      
          if(isset($jobBody['jobName']) {
              $myArgument = $jobBody['jobName']; 
              $callbackUrl = $jobBody['callbackUrl']; 
      
              //Call external API
              $external = curl_init();
              curl_setopt_array($external, array(
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_URL => 'http://external.api.com'
              ));
      
              //The reponse from the external API
              $result = curl_exec($external);
              if(!$result) {
                  //External api failed
                  //Don't delete job, will be pickedup again the next round
                  curl_close($external);
                  return;
              }
              curl_close($external);
      
              //Return the response to the original callee
              $callback = curl_init();
              curl_setopt_array($callback, array(
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_URL => $callbackUrl, //The url which the original api caller gave
                  CURLOPT_POST => true,
                  CURLOPT_POSTFIELDS => array(
                      data => $result //The response from the external api call
                  )
              ));
              $deliverd = curl_exec($callback);
              if(!$deliverd) {
                  curl_close($callback);
                  return;
              }
              curl_close($callback);
          }
      
          //Remove the job from the queue
          $job->delete();
      }

 

PHP進程的生命週期很短,遇到意外狀況也會中斷,若是跟想要PHP在後臺不斷的跑腳本,就需重啓它。Supervisor是一個python開發的基於*nix上的管理和監控進程的client/server程序。當PHP進程中斷,會從新啓動它。
安裝:git

  1. wget http://pypi.python.org/packages/source/s/supervisor/supervisor-3.0b1.tar.gz
  2. tar –zxvf supervisor–3.0b1.tar.gz
  3. cd supervisor–3.0b1
  4. easy_install supervisor #安裝setuptools(yum -y install python-setuptools)

配置:
在/etc/supervisord.conf後添加:github

  1. [program:php]
  2. command= /usr/local/php54/bin/php –f /root/supervisor/test.php #被監控進程
  3. ;process_name=%(process_num)02d
  4. ;numprocs=5 #啓動幾個進程
  5. autostart=true #隨着supervisord的啓動而啓動
  6. autorestart=true #自動啓動
  7. startsecs=1 #程序重啓時候停留在runing狀態的秒數
  8. startretries=10 #啓動失敗時的最多重試次數
  9. redirect_stderr=true #重定向stderr到stdout
  10. stdout_logfile=/root/supervisor/test.log #stdout文件

命令:
supervisord :啓動supervisor sever
supervisorctl:啓動supervisor client的命令行窗口。redis

 

另:windows

https://docs.phalconphp.com/en/3.2/queueapi

https://github.com/iamfirst/Cli-queue-phalcon/blob/master/tasks/MainTask.phpcurl

https://github.com/alexboo/phalcon-redis-queue/blob/master/src/RedisQueue.phptcp

相關文章
相關標籤/搜索