【发布时间】: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