Skip to content

Commit

Permalink
Merge pull request #2 from hsldymq/feat-worker-shutdown-event
Browse files Browse the repository at this point in the history
feat: worker 增加 shutdown 事件
  • Loading branch information
hsldymq authored Dec 29, 2023
2 parents 50a6fb7 + 30fee26 commit c7b3f88
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/01.basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
->registerEvent('error', function (string $reason, \Throwable $e, Worker $worker) {
echo "Worker Error. Reason: {$reason}, Message: {$e->getMessage()}\n";
$worker->shutdown();
})
->registerEvent("shutdown", function (Worker $worker) {
echo "worker shutdown event triggered: {$worker->getWorkerID()}\n";
});

$params = require __DIR__.'/amqp_params.php';
Expand Down
8 changes: 8 additions & 0 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* @event disconnected 与dispatcher的连接中断,即将退出
* 参数: \Archman\BugsBunny\Worker $worker
*
* @event shutdown worker退出
* 参数: \Archman\BugsBunny\Worker $worker
*
* @event error 发生错误
* 参数: string $reason, \Throwable $ex, \Archman\BugsBunny\Worker $worker
* $reason enum:
Expand Down Expand Up @@ -113,6 +116,10 @@ public function run()

$this->errorlessEmit('patrolling');
}

if (!$this->passiveShutdown) {
$this->errorlessEmit('shutdown');
}
}

/**
Expand Down Expand Up @@ -179,6 +186,7 @@ public function handleMessage(Message $msg)

if ($this->noMore) {
if ($this->passiveShutdown) {
$this->errorlessEmit('shutdown');
$this->sendMessage(new Message(MessageTypeEnum::KILL_ME, ''));
} else {
$this->sendMessage(new Message(MessageTypeEnum::I_QUIT, ''));
Expand Down

0 comments on commit c7b3f88

Please sign in to comment.