This commit is contained in:
as2252258@163.com
2021-02-27 15:27:22 +08:00
parent 5ee9992afa
commit a3c6db9f5e
4 changed files with 477 additions and 457 deletions
+13 -19
View File
@@ -27,6 +27,8 @@ class OnWorkerStart extends Callback
private int $_taskTable = 0; private int $_taskTable = 0;
private int $signal = SIGTERM | SIGKILL | SIGUSR2 | SIGUSR1;
/** /**
* @param Server $server * @param Server $server
@@ -38,12 +40,7 @@ class OnWorkerStart extends Callback
*/ */
public function onHandler(Server $server, int $worker_id): void public function onHandler(Server $server, int $worker_id): void
{ {
Coroutine::set([ Coroutine::set(['enable_deadlock_check' => false]);
'enable_deadlock_check' => false,
// 'exit_condition' => function () {
// return Coroutine::stats()['coroutine_num'] === 0;
// }
]);
putenv('workerId=' . $worker_id); putenv('workerId=' . $worker_id);
$get_name = $this->get_process_name($server, $worker_id); $get_name = $this->get_process_name($server, $worker_id);
@@ -52,11 +49,13 @@ class OnWorkerStart extends Callback
} }
$this->onSignal($server, $worker_id); $this->onSignal($server, $worker_id);
$this->debug(sprintf(workerName($worker_id) . ' #%d is start.....', $worker_id));
if ($worker_id >= $server->setting['worker_num']) { if ($worker_id >= $server->setting['worker_num']) {
fire(Event::SERVER_TASK_START); fire(Event::SERVER_TASK_START);
putenv('environmental=' . Snowflake::TASK);
} else { } else {
putenv('environmental=' . Snowflake::WORKER);
Snowflake::setWorkerId($server->worker_pid); Snowflake::setWorkerId($server->worker_pid);
$this->setWorkerAction($worker_id); $this->setWorkerAction($worker_id);
} }
@@ -68,22 +67,17 @@ class OnWorkerStart extends Callback
*/ */
public function onSignal($server, $worker_id) public function onSignal($server, $worker_id)
{ {
Coroutine\go(function (Server $server, $worker_id) { $this->debug(sprintf(workerName($worker_id) . ' #%d is start.....', $worker_id));
$sigkill = Coroutine::waitSignal(SIGTERM | SIGKILL | SIGUSR2 | SIGUSR1); Coroutine\go(function (Server $server) {
$sigkill = Coroutine::waitSignal($this->signal);
Timer::clearAll();
if ($sigkill === false) { if ($sigkill === false) {
do { return $server->stop();
$number = Co::stats()['coroutine_num'];
var_dump($number);
if ($number === 0) {
break;
} }
while (Co::stats()['coroutine_num'] > 0) {
Coroutine::sleep(0.01); Coroutine::sleep(0.01);
} while (true);
} }
return $server->stop(); return $server->stop();
}, $server, $worker_id); }, $server);
} }
+2 -6
View File
@@ -246,15 +246,11 @@ class Server extends HttpService
return; return;
} }
foreach ($processes as $name => $process) { foreach ($processes as $name => $process) {
$is_enable_coroutine = true; $this->debug(sprintf('Process %s', $process));
if (is_array($process)) {
[$process, $is_enable_coroutine] = $process;
}
$this->debug(sprintf('Process %s', $name . '::' . $process));
if (!is_string($process)) { if (!is_string($process)) {
continue; continue;
} }
$system = new $process(Snowflake::app(), $name, $is_enable_coroutine); $system = new $process(Snowflake::app(), $name, true);
if (isset($this->params[$name])) { if (isset($this->params[$name])) {
$system->write(Json::encode($this->params[$name])); $system->write(Json::encode($this->params[$name]));
} }
+26 -10
View File
@@ -30,18 +30,34 @@ abstract class Process extends \Swoole\Process implements SProcess
*/ */
public function __construct($application, $name, $enable_coroutine = true) public function __construct($application, $name, $enable_coroutine = true)
{ {
$class = get_called_class(); parent::__construct([$this, '_load'], true, 1, $enable_coroutine);
parent::__construct(function ($process) use ($name, $class) {
fire(Event::SERVER_WORKER_START);
putenv('workerId=Process.0');
if (Snowflake::isLinux()) {
$prefix = ucfirst(rtrim(Snowflake::app()->id, ':'));
$this->name($prefix . ': ' . $class);
}
$this->onHandler($process);
}, false, 1, $enable_coroutine);
$this->application = $application; $this->application = $application;
Snowflake::setWorkerId($this->pid); Snowflake::setWorkerId($this->pid);
} }
/**
* @param Process $process
* @throws \Snowflake\Exception\ComponentException
*/
private function _load(Process $process)
{
putenv('environmental=' . Snowflake::PROCESS);
fire(Event::SERVER_WORKER_START);
if (Snowflake::isLinux()) {
$this->name($this->getPrefix());
}
$this->onHandler($process);
}
/**
* @return string
*/
private function getPrefix(): string
{
return ucfirst(rtrim(Snowflake::app()->id, ':') . ': ' . get_called_class());
}
} }
+14
View File
@@ -400,6 +400,20 @@ class Snowflake
private static array $_autoload = []; private static array $_autoload = [];
const PROCESS = 'process';
const TASK = 'task';
const WORKER = 'worker';
/**
* @return string|null
*/
public static function getEnvironmental(): ?string
{
return env('environmental');
}
/** /**
* @param $class * @param $class
* @param $file * @param $file