Skip to content

Commit

Permalink
feat: worker 增加 shutdown 事件
Browse files Browse the repository at this point in the history
  • Loading branch information
hsldymq committed Dec 29, 2023
1 parent 339bf06 commit 59dcf97
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 59dcf97

Please sign in to comment.