phalcon: queueing使用心得,需要安装相应的软体
http://flyhighest.com/archives/50
原本没有用过phalcon的消息队列,本来以为很简单,结果搞了半天,把步骤记录一下。
- phalcon的官网上没有说需要安装beanstalkd,我猜写文档的以为我等默认会安装,找了半天没有找到windows下的beanstalk,有个国人写的beanstalkd-win,没敢用。
- 找了一台Ubuntu,安装上了beanstalkd,命令: sudo apt-get install beanstalkd
- 启动:beanstalkd -l 10.0.1.5 -p 11300
- 打开11300端口,命令:iptables -A INPUT -ptcp –dport 11300 -j ACCEPT
- 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.
- 把例子程序里的所有beanstalkd的host和端口号改一下。
- 开四个cmd,一个运行php console.php worker main,一个运行 php console.php pheanstalkworker main, 一个运行php console.php pheanstalkstats main ,最后一个运行php console.php producer main。就可以看到任务写入,任务执行过程了
- 基本上phalcon的消息队列环境就搭好了。
- 照着phalcon官网上的例子写,就可以了:https://docs.phalconphp.com/zh/latest/reference/queue.html
Note:
- 抄了一段别人写的代码, 如果把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);
}Worker
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进程中断,会重新启动它。
安装:
- wget http://pypi.python.org/packages/source/s/supervisor/supervisor-3.0b1.tar.gz
- tar –zxvf supervisor–3.0b1.tar.gz
- cd supervisor–3.0b1
- easy_install supervisor #安装setuptools(yum -y install python-setuptools)
配置:
在/etc/supervisord.conf后添加:
- [program:php]
- command= /usr/local/php54/bin/php –f /root/supervisor/test.php #被监控进程
- ;process_name=%(process_num)02d
- ;numprocs=5 #启动几个进程
- autostart=true #随着supervisord的启动而启动
- autorestart=true #自动启动
- startsecs=1 #程序重启时候停留在runing状态的秒数
- startretries=10 #启动失败时的最多重试次数
- redirect_stderr=true #重定向stderr到stdout
- stdout_logfile=/root/supervisor/test.log #stdout文件
命令:
supervisord :启动supervisor sever
supervisorctl:启动supervisor client的命令行窗口。
phalcon: queueing使用心得,需要安装相应的软体的更多相关文章
- GitHub学习心得之 安装配置与多帐号管理
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 GitHub学习心得之 安装配置与多帐号管理 1.前言2.GitHub Linux安装(ub ...
- wireshark使用心得 centos7安装wireshark: yum install wireshark wireshark-gnome
centos7 安装wireshark 安装 yum install wireshark yum install wireshark-gnome 关于pcap文件的文件解析网上资料有很多,我在这就不说 ...
- idea使用心得(2)-安装设置与创建web项目
idea 是与eclipse齐名的IDE(集成开发工具),以智能闻名,不过对于熟悉eclipse的的用户来说,初次接触idea有些让人搞不清方向,下面介绍一下简单的使用 方式. 1.安装 官网下载ul ...
- windows7股票的,win8残疾人,安装Han澳大利亚sinoxn个时间,sinox它支持大多数windows软体
腾讯科技: 正如先前所宣布,微软,10一个月31迄今,Windows 7家庭基础版.家庭高级版和旗舰版盒装版本将不再销售.而微软是不会再OEM制造商授予许可数量的三个版本. windows7股票的.由 ...
- 每天一个linux命令(23):Linux 目录结构
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...
- linux文件目录详解
文件系统的是用来组织和排列文件存取的,所以她是可见的,在Linux中,我们可以通过ls等工具来查看其结构,在Linux系统中,我们见到 的都是树形结构:比如操作系统安装在一个文件系统中,他表现为由/起 ...
- linux命令:目录结构
可分享的(shareable) 不可分享的(unshareable) 不变的(static) /usr (软件放置处) /etc (配置文件) /opt (第三方协力软件) /boot (开机与核 ...
- 深入理解linux系统的目录结构(总结的非常详细)
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...
- linux系统的目录结构
前言 对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要 ...
随机推荐
- UML实践详细经典教程----用例图、顺序图、状态图、类图、包图、协作图
面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language™),这篇课程的目的是展示出UML ...
- Android中直播视频技术探究之---摄像头Camera视频源数据采集解析
一.前言 在视频直播中一般都是两种视频数据源,一个是摄像头数据,一个是录制桌面数据,而一般来说美女妹子直播都是来自于摄像头数据,游戏直播都是录制桌面数据的,那么今天就来看看第一个数据源数据采集分析,A ...
- Android编译中m、mm、mmm的区别
准备工作 在AndroidSource Code中有envsetup.sh档案,当执行过此档案后,可以大幅将build的过程简单化.自动化 此档案在src(android source code 位置 ...
- FastReport处理空日期
[IIf([D.ApproveDate]==DateTime.MinValue,"",[D.ApproveDate])]
- File Checksum Integrity Verifier
Microsoft (R) File Checksum Integrity Verifier V2.05 README file =================================== ...
- Android 杂记 - 存货盘点用的客户端
最近有个盘点用的东西,要放到移动设备,本来用 .Net Compact Framework,CAB 部署在 CE 系统的移动条码设备.技术太旧,我用了这个周末两天时间,把这东西在试试实现在安卓上面,给 ...
- 4,帮助命令man
一:man man是manual的缩写,文档的意思 man man(1),代表man下是分用户级别的,
- apue和error
在做进程环境测试的时候,测试demo中出现了apue.h,而标准库中没有这个头文件和其中的函数定义,经查找需要在/usr/include中添加apue.h和error.c.原型可以去这个网站查找. ...
- Servlet&jsp基础:第二部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- yii 验证码的使用
在HappyController 中加入 public function actions(){ return array( // captcha action renders the CAPTCHA ...