Skip to content

Commit

Permalink
Dropped the unused connections & added logs
Browse files Browse the repository at this point in the history
  • Loading branch information
smskin committed Oct 10, 2024
1 parent e503f91 commit 980cf1a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Commands/SupervisorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function handle()
{
$configuration = $this->getConfigResolver();
$configuration->declare();
// $configuration->getConnection()->close();
$configuration->getConnection()->close();

$this->start();
}
Expand Down
18 changes: 17 additions & 1 deletion src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ErrorException;
use Exception;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use LogicException;
use PhpAmqpLib\Channel\AMQPChannel;
Expand All @@ -30,7 +31,7 @@ class Worker
/**
* @param AMQPStreamConnection $connection
*/
public function __construct(AMQPStreamConnection $connection)
public function __construct(private readonly AMQPStreamConnection $connection)
{
$this->channel = $connection->channel();
}
Expand All @@ -47,6 +48,7 @@ public function start(Collection $consumers): void
return $this->registerConsumer($consumer);
});

$this->log('Worker started');
$this->channel->consume();
}

Expand All @@ -55,11 +57,14 @@ public function start(Collection $consumers): void
*/
public function terminate(): void
{
$this->log('Worker terminating');
$this->working = false;
sleep(1);
$this->consumers->each(function (IConsumer $consumer) {
$this->stopConsumer($consumer);
});
$this->connection->close();
$this->log('Worker terminated');
}

private function stopConsumer(IConsumer $consumer): void
Expand All @@ -77,9 +82,15 @@ private function registerConsumer(IConsumer $consumer): string
$consumer->isExclusive(),
$consumer->isNoWait(),
function (AMQPMessage $message) use ($consumer) {
$this->log('Handled message', ['body' => $message->body]);
try {
$consumer->handleMessage($message);
$this->log('Consumer executed');
} catch (Throwable $exception) {
$this->log('Consumer exception', [
'class' => get_class($exception),
'message' => $exception->getMessage(),
]);
$this->handleException($consumer, $message, $exception);
}

Expand Down Expand Up @@ -146,4 +157,9 @@ private function getPublisher(): Publisher
{
return app(Publisher::class);
}

private function log(string $message, array|null $data = null): void
{
Log::debug(get_class($this) . ':' . $message, $data);
}
}

0 comments on commit 980cf1a

Please sign in to comment.