PHPの標準AMQPクラスに基づく単純なキュー ワーカーがあります。サーバーとしてRabbitMQで動作します。RabbitMQ で AMQP 接続を初期化するための Queue クラスがあります。以下のコードですべて正常に動作します。
$queue = new Queue('myQueue');
while($envelope = $queue->getEnvelope()) {
$command = unserialize($envelope->getBody());
if ($command instanceof QueueCommand) {
try {
if ($command->execute()) {
$queue->ack($envelope->getDeliveryTag());
}
} catch (Exception $exc) {
// an error occurred so do some processing to deal with it
}
}
}
ただし、キューコマンドの実行をフォークしたかったのですが、この場合、キューは最初のコマンドで何度も無限になります。$queue->ack(); でメッセージを受信したことを RabbitMQ に確認できません。私の分岐バージョン (テストのために子を 1 つだけ簡略化) は次のようになります。
$queue = new Queue('myQueue');
while($envelope = $queue->getEnvelope()) {
$command = unserialize($envelope->getBody());
if ($command instanceof QueueCommand) {
$pid = pcntl_fork();
if ($pid) {
//parent proces
//wait for child
pcntl_waitpid($pid, $status, WUNTRACED);
if($status > 0) {
// an error occurred so do some processing to deal with it
} else {
//remove Command from queue
$queue->ack($envelope->getDeliveryTag());
}
} else {
//child process
try {
if ($command->execute()) {
exit(0);
}
} catch (Exception $exc) {
exit(1);
}
}
}
}
どんな助けでも大歓迎です...