php – 使用System_Daemon包的Codeigniter守护进程
我正在尝试使用带有CodeIgniter CLI的System_Daemon包创建一个守护进程.对我来说这是一个新领域,我正在努力.
这就是我所拥有的: 一个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(另一个“免费”队列服务)可以提供它们. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |