Skip to content

Commit

Permalink
放置dispatcher优雅退出死循环
Browse files Browse the repository at this point in the history
  • Loading branch information
hsldymq committed May 14, 2019
1 parent 36dd881 commit c90c143
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class Dispatcher extends AbstractMaster implements ConsumerHandlerInterface
*/
private $shutdownError;

/**
* @var int 关闭的超时时间,超过后dispatcher强制自行退出.
*/
private $shutdownTimeoutSec = 30;

public function __construct(AMQPConnectionInterface $connection, WorkerFactoryInterface $factory)
{
parent::__construct();
Expand Down Expand Up @@ -382,6 +387,20 @@ public function setPatrolPeriod(int $seconds): self
return $this;
}

/**
* 设置退出超时时间.
*
* @param int $sec
*
* @return Dispatcher
*/
public function setShutdownTimeoutSec(int $sec): self
{
$this->shutdownTimeoutSec = $sec;

return $this;
}

/**
* @return array
*/
Expand Down Expand Up @@ -565,24 +584,31 @@ private function flushCached()
*/
private function informWorkersQuit()
{
foreach ($this->workersInfo as $workerID => $each) {
try {
$this->sendLastMessage($workerID);
} catch (\Throwable $e) {
// TODO 如果没有成功向所有进程发送关闭,考虑是否需要做重试机制
$this->errorlessEmit('error', ['shuttingDown', $e]);
$startInformTime = time();
$informCount = 0;
do {
// 每10秒进行一次通知
if ((time() - $startInformTime) / 10 >= $informCount) {
$informCount++;
foreach ($this->workersInfo as $workerID => $each) {
try {
$this->sendLastMessage($workerID);
} catch (\Throwable $e) {
// TODO 如果没有成功向所有进程发送关闭,考虑是否需要做重试机制
$this->errorlessEmit('error', ['shuttingDown', $e]);
}
}
}
}

do {
if ($this->countWorkers() === 0) {
$this->state = self::STATE_SHUTDOWN;
break;
}

$this->process(0.5);
$this->waitChildren();
} while (true);
// 防止子进程无响应,这里循环一定时间后直接退出
} while ((time() - $startInformTime) < $this->shutdownTimeoutSec);
}

private function waitChildren()
Expand Down

0 comments on commit c90c143

Please sign in to comment.