在webman
中,開啟自定義進(jìn)程
在onWorkerStart
中,使用 while (true)
來達(dá)到實時消費的目的
阿里云MNS文檔
while (true) {
try {
pcntl_signal(SIGINT, function () {
Worker::stopAll();
});
//waitSeconds非0表示這次receiveMessage是一次http long polling,如果queue內(nèi)沒有message,那么這次request會在server端等到queue內(nèi)有消息才返回。最長等待時間為waitSeconds的值,最大為30。
$res = $queue->receiveMessage(30);
$receiptHandle = $res->getReceiptHandle();
$message = $res->getMessageBody();
Log::channel('default')->info('received message : ' . $message);
sleep(5);//模擬消費數(shù)據(jù)
Log::channel('default')->info('handle success');
$res = $queue->deleteMessage($receiptHandle);
pcntl_signal_dispatch();
} catch (\Throwable $throwable) {
}
}
但當(dāng)使用 stop
命令時,會報worker[gain-star:37993] exit with status 9
參考了這篇帖子
在while(true)
中加入了
pcntl_signal(SIGINT, function () {
Worker::stopAll();
});
和
pcntl_signal_dispatch();
使用stop
命令時,依然會報 worker[gain-star:37993] exit with status 9
問題:
如果想達(dá)到實時消費的目的,有更好的思路或者辦法嗎?
$queue->receiveMessage(30);
改成 $queue->receiveMessage(1);
至于sleep(5);
,執(zhí)行stop后workerman會kill掉2秒沒反應(yīng)的進(jìn)程,所以sleep(5)
這里會導(dǎo)致進(jìn)程被kill產(chǎn)生exit with status 9
。
關(guān)于2秒的限制參考 http://wtbis.cn/q/8213#answer_12601 ,如果你的業(yè)務(wù)需要5秒才能安全停止,需要改動workerman源碼 KILL_WORKER_TIMR_TIME 改成大于5秒的值。關(guān)于這個超時將在下個workerman和webman版本將支持配置。
最后一個優(yōu)化,
pcntl_signal(SIGINT, function () {
Worker::stopAll();
});
這個在onWorkerStart里只執(zhí)行一次就行了,不用放到while循環(huán)里。
請問老大
$queue->receiveMessage(30); 改成 $queue->receiveMessage(1);
這里為啥要改成1呢
我修改了之后還是 每30秒多自動消費一次