【问题标题】:Codeigniter Daemon using System_Daemon package使用 System_Daemon 包的 Codeigniter 守护程序
【发布时间】:2012-03-07 22:46:09
【问题描述】:

我正在尝试使用 System_Daemon 包和 CodeIgniter 的 CLI 创建一个守护程序。这对我来说是一个新领域,我正在苦苦挣扎。

这是我所拥有的: 将消息注入 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 代码。见下方代码

这是正确的做法吗?你能指导我以“正确的方式”做到这一点吗?我浏览了各种知识渊博的论坛,但都做得很短……请帮帮我!

嗯嗯

#!/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 codeigniter daemon


    【解决方案1】:

    守护进程是在后台“永远”运行的进程。 在这里,您所做的只是检查队列中的一条新消息,然后退出。 基本上,您必须添加一个循环来获取所有需要执行的代码。您需要在循环中执行睡眠,以避免您的守护进程占用所有可用资源。

    无论如何,php 不擅长守护进程,因为直到脚本结束时才会释放一些内存。如果您的脚本永远不会结束(如守护程序),它将耗尽所有可用内存(根据 php 配置)然后因错误而死。您必须非常小心地编写脚本以避免此类内存泄漏!

    另外,请注意,每次您向 sqs 库询问某些内容时,它都会向 Amazon 服务器发送一个 http 请求。过于频繁地这样做可能会非常昂贵。

    作为补偿,我建议您使用每分钟运行一次的 cronjob 来检查新任务。这样可以避免内存泄漏(php 进程在执行之间停止)和过多的网络使用(请求在一分钟内完成)。

    最后一点,如果您不打算执行很多任务(也就是说,您的守护程序在 99% 的情况下什么都不做),请考虑使用推送队列。使用推送队列,不再是您的脚本轮询队列,而是每次需要完成某些任务时,队列都会通知您的脚本(即:使用标准 http 请求调用您的脚本)。这样可以避免不必要地运行脚本。

    我不知道亚马逊是否提供推送队列,但 ironmq(另一种“免费”队列服务)可以提供。 更多信息:http://dev.iron.io/mq/reference/push_queues/

    【讨论】:

    • 感谢您的建议!我也得出结论,cron 工作是要走的路。也感谢 push_queues 指针。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    相关资源
    最近更新 更多