From ae8ae60941f9c70adef65dab6888a730f5e95141 Mon Sep 17 00:00:00 2001 From: akbarjimi Date: Wed, 16 Mar 2022 18:04:03 +0330 Subject: [PATCH] third commit --- app/Console/Commands/Consumer.php | 28 +++++++++++++++++----------- app/Console/Commands/Publisher.php | 21 ++++++++++++++++++--- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/Console/Commands/Consumer.php b/app/Console/Commands/Consumer.php index 19000d1..f795291 100644 --- a/app/Console/Commands/Consumer.php +++ b/app/Console/Commands/Consumer.php @@ -22,19 +22,25 @@ class Consumer extends Command public function handle() { $time = -microtime(true); + $line = fn(string $string) => $string.\PHP_EOL; + + $handler = function (ConsumableMessage $message, AMQPMessage $otherMessage) use ($line) { + echo $line($body = $message->getMessageBody()); - $handler = function (ConsumableMessage $message, AMQPMessage $otherMessage) use (&$counter) { - echo $body = $message->getMessageBody(); - echo \PHP_EOL; try { - return match ($body) { - \strcmp($body, "finish") === 0 => $otherMessage->getChannel()->close(), - \strcmp($body, "requeue") === 0 => $message->nack(), - default => $body, - }; - $message->ack(); - } catch (Throwable) { - $message->nack(); + if (\str_contains($body, "finish")) { + echo $line("finish"); + $message->ack(); + $otherMessage->getChannel()->getConnection()->close(); + } elseif (\str_contains($body, "requeue")) { + echo $line("requeue"); + $message->reject(); + } else { + echo $line("ack"); + $message->ack(); + } + } catch (Throwable $e) { + echo $line($e->getMessage()); } }; diff --git a/app/Console/Commands/Publisher.php b/app/Console/Commands/Publisher.php index e262691..3cbf549 100644 --- a/app/Console/Commands/Publisher.php +++ b/app/Console/Commands/Publisher.php @@ -32,9 +32,24 @@ class Publisher extends Command $time = -microtime(true); echo PHP_EOL; + $requeue = "requeue #"; + $requeued = rand(1, ((int) $this->option('count'))-1); + + $finish = "finish #"; + $finished = rand($requeued, ((int) $this->option('count'))); + for ($index = 1; $index <= (int) $this->option('count'); $index++) { - $chance = rand(1, 100); - $payload = $chance <= 20 ? "finish #$index" : ($chance <= 40 ? "requeue #$index" : "Message #$index"); + $payload = "message #"; + + if ($index == $requeued) { + echo "HERE 1".PHP_EOL; + $payload = $requeue; + } + + if ($index == $finished) { + echo "HERE 2".PHP_EOL; + $payload = $finish; + } $message = [ 'content_type' => 'text/plain', @@ -79,7 +94,7 @@ class Publisher extends Command ]; $message = new ProducibleMessage( - message: $payload, + message: $payload.$index, properties: $publish + $message, );