脚本宝典收集整理的这篇文章主要介绍了php – 使用System_Daemon包的Codeigniter守护进程,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这就是我所拥有的:
将消息注入AWS SQS队列的CI控制器(感谢[url = http://codeigniter.com/forums/member/196201/]coccodrillo[/url]提供有关如何将AWS SDK集成到CI中的优秀说明.请参见此处:Integrating AWS SDK as a library in Codeigniter).
一个CI控制器,它接收队列中的消息并将其写入日志文件,然后删除队列中的消息.
我想有一个CI守护进程,它将监听这个队列,当它们在那里时接收消息并对该消息做一些有用的事情,然后删除该消息.所以我从System_Daemon文档中的示例开始,并在接收程序的CI代码中添加.见下面的代码
这是正确的做法吗?你能指导我做“正确的方式”吗?我已经把各种知识渊博的论坛搞得一团糟了……请帮帮我吧!
Mmiz
#!/usr/bin/PHP -q <?PHP // Make it possible to test in source directory // This is for PEAR developers only ini_set('include_path',ini_get('include_path').':..'); // Include Class error_reporting(E_ALL); require_once "System/Daemon.PHP"; // Bare minimum SETUP System_Daemon::setOption("appName","receiveaws"); System_Daemon::setOption("LOGLocation","/tmp/log/receiveaws.log"); System_Daemon::setOption("appPidLocation","/tmp/log/receiveaws/receiveaws.pid"); System_Daemon::log(System_Daemon::LOG_INFO,"Daemon not yet started so this will be written on-screen"); // Spawn Deamon! System_Daemon::start(); System_Daemon::log(System_Daemon::LOG_INFO,"Daemon: '". System_Daemon::getOption("appName"). "' spawned! This will be written to ". System_Daemon::getOption("logLocation")); System_Daemon::log(System_Daemon::LOG_WARNING,'My PHP code starting'); class Receiveaws extends CI_Controller { public function index(){ if ($this->input->is_cli_request()) { //Load the aws library $this->load->library('awslib'); $sqs = new AmazonSQS(); //Get the queue to look at $res=$sqs->get_queue_url('example-queue'); //Get the queue's url $qurl=($res->body->GetQueueUrlResult->QueueUrl); System_Daemon::log(System_Daemon::LOG_INFO,$qurl); //Get a message From the queue $response = $sqs->receive_message($qurl); //If there was a message received,then do something if ($res->isOK()) { System_Daemon::log(System_Daemon::LOG_INFO,"Receive message successful"); //Now delete message from queue $res=$sqs->delete_message($qurl,$rcpt_hand); if ($res->isOK()) { System_Daemon::log(System_Daemon::LOG_INFO,"Delete message successful"); } } else { //go back to check for messages //How do you do that? } } else { //Access from URL - so bail out? //how do you not bail out of the daemon from here? } } } System_Daemon::stop(); ?>
无论如何,PHP并不擅长守护进程,因为在脚本结束之前,某些内存永远不会被释放.如果你的脚本永远不会结束(就像一个守护进程),它将占用所有可用内存(根据PHP配置)然后因错误而死亡.您必须非常小心地编写脚本代码以避免此类内存泄漏!
另外,请注意,每次向sqs库询问某些内容时,它都会向Amazon服务器发送http请求.经常这样做会非常昂贵.
为了补偿,我建议你使用每分钟运行一次的cronjob来检查新任务.这样可以避免内存泄漏(PHP进程在执行之间发生故障)和过多的网络使用(请求在一分钟之内完成).
最后一点,如果您不打算执行许多任务(这意味着,您的守护程序在99%的时间内都不执行任何操作),请考虑使用推送队列.使用推送队列,不再是您的脚本轮询队列,但是每次需要完成某项任务时,队列都会通知您的脚本(即:使用标准的http请求调用脚本).这样可以避免运行脚本不必要.
我不知道亚马逊是否提供推送队列,但是ironmq(另一个“免费”队列服务)可以提供它们.
更多信息:http://dev.iron.io/mq/reference/push_queues/
以上是脚本宝典为你收集整理的php – 使用System_Daemon包的Codeigniter守护进程全部内容,希望文章能够帮你解决php – 使用System_Daemon包的Codeigniter守护进程所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。