Browse Source

third commit

mohammad
Mohammad Akbari 3 years ago
parent
commit
ae8ae60941
Signed by: akbarjimi GPG Key ID: 55726AEFECE5E683
  1. 28
      app/Console/Commands/Consumer.php
  2. 21
      app/Console/Commands/Publisher.php

28
app/Console/Commands/Consumer.php

@ -22,19 +22,25 @@ class Consumer extends Command
public function handle() public function handle()
{ {
$time = -microtime(true); $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 { 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());
} }
}; };

21
app/Console/Commands/Publisher.php

@ -32,9 +32,24 @@ class Publisher extends Command
$time = -microtime(true); $time = -microtime(true);
echo PHP_EOL; 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++) { 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 = [ $message = [
'content_type' => 'text/plain', 'content_type' => 'text/plain',
@ -79,7 +94,7 @@ class Publisher extends Command
]; ];
$message = new ProducibleMessage( $message = new ProducibleMessage(
message: $payload,
message: $payload.$index,
properties: $publish + $message, properties: $publish + $message,
); );

Loading…
Cancel
Save