mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-24 00:55:35 +08:00
add KILLER PROMPT function
This commit is contained in:
@@ -16,6 +16,7 @@ use ZM\Console\Console;
|
|||||||
*/
|
*/
|
||||||
class SignalListener
|
class SignalListener
|
||||||
{
|
{
|
||||||
|
private static $manager_kill_time = 0;
|
||||||
/**
|
/**
|
||||||
* 监听Master进程的Ctrl+C
|
* 监听Master进程的Ctrl+C
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
@@ -29,6 +30,7 @@ class SignalListener
|
|||||||
} else {
|
} else {
|
||||||
echo "\r";
|
echo "\r";
|
||||||
Console::warning("Server interrupted(SIGINT) on Master.");
|
Console::warning("Server interrupted(SIGINT) on Master.");
|
||||||
|
Console::warning("Server will be shutdown.");
|
||||||
Process::kill($server->master_pid, SIGTERM);
|
Process::kill($server->master_pid, SIGTERM);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -48,6 +50,7 @@ class SignalListener
|
|||||||
} else {
|
} else {
|
||||||
Console::verbose("Interrupted in manager!");
|
Console::verbose("Interrupted in manager!");
|
||||||
}
|
}
|
||||||
|
self::processKillerPrompt();
|
||||||
};
|
};
|
||||||
Console::debug("Listening Manager SIGINT");
|
Console::debug("Listening Manager SIGINT");
|
||||||
if (version_compare(SWOOLE_VERSION, "4.6.7") >= 0) {
|
if (version_compare(SWOOLE_VERSION, "4.6.7") >= 0) {
|
||||||
@@ -65,15 +68,40 @@ class SignalListener
|
|||||||
public static function signalWorker(Server $server, $worker_id) {
|
public static function signalWorker(Server $server, $worker_id) {
|
||||||
Console::debug("Listening Worker #".$worker_id." SIGINT");
|
Console::debug("Listening Worker #".$worker_id." SIGINT");
|
||||||
Process::signal(SIGINT, function () use ($worker_id, $server) {
|
Process::signal(SIGINT, function () use ($worker_id, $server) {
|
||||||
if ($server->master_pid == $server->worker_pid) {
|
if ($server->master_pid == $server->worker_pid) { // 当Swoole以单进程模型运行的时候,Worker需要监听杀死的信号
|
||||||
echo "\r";
|
echo "\r";
|
||||||
Console::warning("Server interrupted(SIGINT) on Worker.");
|
Console::warning("Server interrupted(SIGINT) on Worker.");
|
||||||
swoole_timer_after(2, function() {
|
swoole_timer_after(2, function() {
|
||||||
Process::kill(posix_getpid(), SIGTERM);
|
Process::kill(posix_getpid(), SIGTERM);
|
||||||
});
|
});
|
||||||
|
self::processKillerPrompt();
|
||||||
}
|
}
|
||||||
//Console::verbose("Interrupted in worker");
|
//Console::verbose("Interrupted in worker");
|
||||||
// do nothing
|
// do nothing
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按5次Ctrl+C后强行杀死框架的处理函数
|
||||||
|
*/
|
||||||
|
private static function processKillerPrompt() {
|
||||||
|
if (self::$manager_kill_time > 0) {
|
||||||
|
if (self::$manager_kill_time >= 5) {
|
||||||
|
$file_path = _zm_pid_dir();
|
||||||
|
$flist = DataProvider::scanDirFiles($file_path, false, true);
|
||||||
|
foreach($flist as $file) {
|
||||||
|
$name = explode('.', $file);
|
||||||
|
if (end($name) == 'pid' && $name[0] !== 'manager') {
|
||||||
|
$pid = file_get_contents($file_path.'/'.$file);
|
||||||
|
Process::kill($pid, SIGKILL);
|
||||||
|
}
|
||||||
|
unlink($file_path.'/'.$file);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "\r";
|
||||||
|
Console::log("再按" . (5 - self::$manager_kill_time) . "次Ctrl+C所有Worker进程就会被强制杀死", 'red');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self::$manager_kill_time++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user