我有一个控制器操作,用于接受批量用户数据,处理数据然后显示结果。由于这是一个繁琐的过程,我试图将它分配给一个工人,然后再提供结果。
我需要帮助在这里设置工作人员。
我当前的proc文件是
web: vendor/bin/heroku-php-apache2
worker: php yii hello/process
我正在通过运行
进行测试public function actionTest(){
$config = require(__DIR__ . '/../config/console.php');
$console = new \yii\console\Application($config);
$console->runAction("hello/send");
}
发送命令
public function actionSend($message = 'hello world')
{
$conn = $this->connection;
$ch = $conn->channel();
$exchange = 'amq.direct';
$queue = 'basic_get_queue';
$ch->queue_declare($queue, false, true, false, false);
//$ch->exchange_declare($exchange, 'direct', true, true, false);
//$ch->queue_bind($queue, $exchange);
$msg_body = $message;
$msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
$ch->basic_publish($msg,'', $queue);
/*
$retrived_msg = $ch->basic_get($queue);
var_dump($retrived_msg->body);
$ch->basic_ack($retrived_msg->delivery_info['delivery_tag']); */
$ch->close();
$conn->close();
}
处理命令
public function actionProcess(){
$conn = $this->connection;
$ch = $conn->channel();
$queue = 'basic_get_queue';
$ch->queue_declare($queue, false, true, false, false);
//$retrived_msg = $ch->basic_get($queue);
$callback = function($msg) {
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
echo $msg->body;
};
$ch->basic_qos(null, 1, null);
$ch->basic_consume($queue, '', false, false, false, false, $callback);
// loop over incoming messages
while(count($ch->callbacks)) {
$ch->wait();
}
$ch->close();
$conn->close();
}
但它并没有触发工人!
我打开了两个终端并尝试了,我可以在进程命令窗口中回显消息。
部署后,我可以看到工作人员正常运行
2017-03-13T10:25:24.114552+00:00 heroku[worker.1]: Starting process with command 'php yii hello/process'
2017-03-13T10:25:24.783227+00:00 heroku[worker.1]: State changed from starting to up
但是,即使从终端运行send command
,工作人员也不会触发。
请帮忙!
答案 0 :(得分:1)
这是我工作的最终代码!如果有更好的方法来实现这一点,我将非常感激。
<强>控制器强>
public function actionTest($message){
$url = parse_url(getenv('CLOUDAMQP_URL'));
$conn = new AMQPConnection($url['host'], 5672, $url['user'], $url['pass'], substr($url['path'], 1));
$ch = $conn->channel();
$exchange = 'amq.direct';
$queue = 'basic_get_queue';
$ch->queue_declare($queue, false, true, false, false);
$msg_body = $message;
$msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
$ch->basic_publish($msg,'', $queue);
$ch->close();
$conn->close();
}
<强> Procfile 强>
web: vendor/bin/heroku-php-apache2
worker: php yii hello/process
<强>命令强>
public function actionProcess(){
$url = parse_url(getenv('CLOUDAMQP_URL'));
$conn = new AMQPConnection($url['host'], 5672, $url['user'], $url['pass'], substr($url['path'], 1));
$ch = $conn->channel();
$queue = 'basic_get_queue';
$ch->queue_declare($queue, false, true, false, false);
$callback = function($msg) {
//save it for now
$mesg = new Messages();
$mesg->message = $msg->body;
$mesg->save(false);
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};
$ch->basic_qos(null, 1, null);
$ch->basic_consume($queue, '', false, false, false, false, $callback);
// loop over incoming messages
while(count($ch->callbacks)) {
$ch->wait();
}
$ch->close();
$conn->close();
}
我使用 cloudamqp (https://elements.heroku.com/addons/cloudamqp)作为我的AMQP服务。不要忘记为工人调整dynos!