This commit is contained in:
2020-09-11 14:27:09 +08:00
parent 7e8a952208
commit 3f782e093b
4 changed files with 18 additions and 17 deletions
+14 -7
View File
@@ -22,25 +22,32 @@ class Command extends \Console\Command
public $description = 'server start|stop|reload|restart'; public $description = 'server start|stop|reload|restart';
const ACTIONS = ['start', 'stop', 'restart'];
/** /**
* @param Input $dtl * @param Input $dtl
* @throws ConfigException
* @throws Exception
* @return mixed|void * @return mixed|void
* @throws Exception
* @throws ConfigException
*/ */
public function onHandler(Input $dtl) public function onHandler(Input $dtl)
{ {
$manager = Snowflake::app()->server; $manager = Snowflake::app()->server;
$manager->setDaemon($dtl->get('daemon', 0)); $manager->setDaemon($dtl->get('daemon', 0));
if ($manager->isRunner()) {
$manager->shutdown(); if (!in_array($dtl->get('action'), self::ACTIONS)) {
return $this->error('I don\'t know what I want to do.');
} }
if ($manager->isRunner() && $dtl->get('action') == 'start') {
return $this->error('Service is running. Please use restart.');
}
$manager->shutdown();
if ($dtl->get('action') == 'stop') { if ($dtl->get('action') == 'stop') {
return; return;
} }
if (!in_array($dtl->get('action'), ['start', 'restart'])) {
return $this->error('I don\'t know what I want to do.');
}
$manager->start(); $manager->start();
} }
+4 -5
View File
@@ -31,7 +31,7 @@ class OnWorkerStart extends Callback
* @return mixed|void * @return mixed|void
* @throws Exception * @throws Exception
*/ */
public function onHandler(Server $server, $worker_id) public function onHandler(Server $server, int $worker_id)
{ {
Snowflake::setWorkerId($server->worker_pid); Snowflake::setWorkerId($server->worker_pid);
@@ -42,15 +42,14 @@ class OnWorkerStart extends Callback
if ($worker_id >= $server->setting['worker_num']) { if ($worker_id >= $server->setting['worker_num']) {
return; return;
} }
$this->setWorkerAction($server, $worker_id); $this->setWorkerAction($worker_id);
} }
/** /**
* @param $worker_id * @param $worker_id
* @param $socket
* @throws Exception * @throws Exception
*/ */
private function setWorkerAction($socket, $worker_id) private function setWorkerAction($worker_id)
{ {
try { try {
$this->debug(sprintf('Worker #%d is start.....', $worker_id)); $this->debug(sprintf('Worker #%d is start.....', $worker_id));
@@ -58,7 +57,7 @@ class OnWorkerStart extends Callback
if (!$event->exists(Event::SERVER_WORKER_START)) { if (!$event->exists(Event::SERVER_WORKER_START)) {
return; return;
} }
$event->trigger(Event::SERVER_WORKER_START); $event->trigger(Event::SERVER_WORKER_START, [$worker_id]);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
Snowflake::app()->getLogger()->write($exception->getMessage(), 'worker'); Snowflake::app()->getLogger()->write($exception->getMessage(), 'worker');
} }
-4
View File
@@ -48,10 +48,6 @@ class Queue extends \Snowflake\Process\Process
$this->waiting = Snowflake::createObject(Waiting::class); $this->waiting = Snowflake::createObject(Waiting::class);
$this->running = Snowflake::createObject(Running::class); $this->running = Snowflake::createObject(Running::class);
$this->complete = Snowflake::createObject(Complete::class); $this->complete = Snowflake::createObject(Complete::class);
Process::signal(9 | 15, function () {
$this->shutdown = true;
});
} }
-1
View File
@@ -38,5 +38,4 @@ abstract class Process extends \Swoole\Process
Snowflake::setWorkerId($this->pid); Snowflake::setWorkerId($this->pid);
} }
} }