Skip to content

Commit

Permalink
修复执行php bin/swoft stop命令时master进程异常未退出的问题,以及停止失败后,pid文件被删除的问题 (#134)
Browse files Browse the repository at this point in the history
* 修复执行php bin/swoft stop命令时master进程异常未退出的问题;
修复执行php bin/swoft stop失败,但pid文件仍被删除的问题。

* Update AbstractServer.php
  • Loading branch information
xiangjihan authored and inhere committed Jul 20, 2018
1 parent 15c89b4 commit 3abb03d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
26 changes: 13 additions & 13 deletions src/framework/src/Bootstrap/Server/AbstractServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,25 @@ public function reload($onlyTask = false)
*/
public function stop(): bool
{
//获取master进程ID
$masterPid = $this->serverSetting['masterPid'];
//使用swoole_process::kill代替posix_kill
$result = \swoole_process::kill($masterPid);
$timeout = 60;
$startTime = time();
$this->serverSetting['masterPid'] && posix_kill($this->serverSetting['masterPid'], SIGTERM);

$result = true;
while (1) {
$masterIslive = $this->serverSetting['masterPid'] && posix_kill($this->serverSetting['masterPid'], SIGTERM);
if ($masterIslive) {
if (time() - $startTime >= $timeout) {
$result = false;
break;
while (true) {
//检测进程是否退出
if(!\swoole_process::kill($masterPid, 0)) {
//判断是否超时
if(time() - $startTime >= $timeout) {
return false;
}
usleep(10000);
continue;
}

break;
return true;
}
return $result;
}

/**
Expand All @@ -159,7 +159,7 @@ public function isRunning(): bool
$pFile = $this->serverSetting['pfile'];

// Is pid file exist ?
if (file_exists($pFile)) {
if (\file_exists($pFile)) {
// Get pid file content and parse the content
$pidFile = file_get_contents($pFile);
$pids = explode(',', $pidFile);
Expand Down
3 changes: 2 additions & 1 deletion src/http-server/src/Command/ServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public function stop()
$serverStatus = $httpServer->getServerSetting();
$pidFile = $serverStatus['pfile'];

@unlink($pidFile);
\output()->writeln(sprintf('<info>Swoft %s is stopping ...</info>', input()->getScript()));

$result = $httpServer->stop();
Expand All @@ -131,6 +130,8 @@ public function stop()
if (!$result) {
\output()->writeln(sprintf('<error>Swoft %s stop fail</error>', input()->getScript()), true, true);
}
//删除pid文件
@unlink($pidFile);

output()->writeln(sprintf('<success>Swoft %s stop success!</success>', input()->getScript()));
}
Expand Down
3 changes: 2 additions & 1 deletion src/rpc-server/src/Command/RpcCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function stop()
$serverStatus = $rpcServer->getServerSetting();
$pidFile = $serverStatus['pfile'];

@unlink($pidFile);
\output()->writeln(sprintf('<info>Swoft %s is stopping ...</info>', input()->getFullScript()));

$result = $rpcServer->stop();
Expand All @@ -110,6 +109,8 @@ public function stop()
if (! $result) {
\output()->writeln(sprintf('<error>Swoft %s stop fail</error>', input()->getFullScript()));
}
//删除pid文件
@unlink($pidFile);

\output()->writeln(sprintf('<success>Swoft %s stop success</success>', input()->getFullScript()));
}
Expand Down
3 changes: 2 additions & 1 deletion src/websocket-server/src/Command/WsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public function stop()
$serverOpts = $server->getServerSetting();
$pidFile = $serverOpts['pfile'];

@unlink($pidFile);
\output()->writeln(sprintf('<info>Swoft %s is stopping ...</info>', input()->getScript()));

$result = $server->stop();
Expand All @@ -129,6 +128,8 @@ public function stop()
if (!$result) {
\output()->writeln(sprintf('<error>Swoft %s stop fail</error>', input()->getScript()), true, true);
}
//删除pid文件
@unlink($pidFile);

\output()->writeln(sprintf('<success>Swoft %s stop success!</success>', input()->getScript()));
}
Expand Down

0 comments on commit 3abb03d

Please sign in to comment.