This commit is contained in:
2021-03-02 18:16:34 +08:00
parent 104a0bfe73
commit 967bfa2b37
6 changed files with 28 additions and 8 deletions
-3
View File
@@ -33,8 +33,6 @@ abstract class Callback extends HttpService
protected function clear($server, $worker_id, $message) protected function clear($server, $worker_id, $message)
{ {
try { try {
Timer::clearAll();
fire(Event::SYSTEM_RESOURCE_CLEAN); fire(Event::SYSTEM_RESOURCE_CLEAN);
\logger()->insert(); \logger()->insert();
@@ -52,7 +50,6 @@ abstract class Callback extends HttpService
} }
const EVENT_ERROR = 'WORKER:ERROR'; const EVENT_ERROR = 'WORKER:ERROR';
const EVENT_STOP = 'WORKER:STOP'; const EVENT_STOP = 'WORKER:STOP';
const EVENT_EXIT = 'WORKER:EXIT'; const EVENT_EXIT = 'WORKER:EXIT';
+6
View File
@@ -7,7 +7,9 @@ namespace HttpServer\Events;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Event;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Server; use Swoole\Server;
@@ -28,6 +30,10 @@ class OnWorkerError extends Callback
*/ */
public function onHandler(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) public function onHandler(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
{ {
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_WORKER_ERROR);
$event->offName(Event::SERVER_WORKER_ERROR);
$this->clear($server, $worker_id, self::EVENT_ERROR); $this->clear($server, $worker_id, self::EVENT_ERROR);
if (!Config::has('email')) { if (!Config::has('email')) {
return; return;
+6
View File
@@ -6,6 +6,8 @@ namespace HttpServer\Events;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use Snowflake\Event;
use Snowflake\Snowflake;
/** /**
* Class OnWorkerExit * Class OnWorkerExit
@@ -21,6 +23,10 @@ class OnWorkerExit extends Callback
*/ */
public function onHandler($server, $worker_id) public function onHandler($server, $worker_id)
{ {
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_WORKER_EXIT);
$event->offName(Event::SERVER_WORKER_EXIT);
$this->clear($server, $worker_id, self::EVENT_EXIT); $this->clear($server, $worker_id, self::EVENT_EXIT);
} }
+1 -4
View File
@@ -13,6 +13,7 @@ use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Server; use Swoole\Server;
use Swoole\Timer;
/** /**
* Class OnWorkerStart * Class OnWorkerStart
@@ -26,7 +27,6 @@ class OnWorkerStart extends Callback
private int $signal = SIGUSR1 | SIGKILL | SIGKILL; private int $signal = SIGUSR1 | SIGKILL | SIGKILL;
/** /**
* @param Server $server * @param Server $server
* @param int $worker_id * @param int $worker_id
@@ -44,9 +44,7 @@ class OnWorkerStart extends Callback
} else { } else {
$this->onWorker($server, $worker_id); $this->onWorker($server, $worker_id);
} }
$this->debug(sprintf('%s #%d Pid:%d start.', ucfirst(env('environmental')), $worker_id, $server->worker_pid)); $this->debug(sprintf('%s #%d Pid:%d start.', ucfirst(env('environmental')), $worker_id, $server->worker_pid));
// Coroutine\go([$this, 'onSignal'], $server, $worker_id);
} }
@@ -85,7 +83,6 @@ class OnWorkerStart extends Callback
} }
/** /**
* @param $server * @param $server
* @param $worker_id * @param $worker_id
+6
View File
@@ -6,6 +6,8 @@ namespace HttpServer\Events;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use Snowflake\Event;
use Snowflake\Snowflake;
/** /**
* Class OnWorkerStop * Class OnWorkerStop
@@ -22,6 +24,10 @@ class OnWorkerStop extends Callback
*/ */
public function onHandler($server, $worker_id) public function onHandler($server, $worker_id)
{ {
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_WORKER_STOP);
$event->offName(Event::SERVER_WORKER_STOP);
$this->clear($server, $worker_id, self::EVENT_STOP); $this->clear($server, $worker_id, self::EVENT_STOP);
} }
+8
View File
@@ -9,6 +9,7 @@ use Exception;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Phalcon\Mvc\Model\Query\Lang; use Phalcon\Mvc\Model\Query\Lang;
use Snowflake\Event;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -145,11 +146,18 @@ abstract class Pool extends Component
/** /**
* @param $name * @param $name
* @param mixed $callback * @param mixed $callback
* @throws ComponentException
* @throws Exception
*/ */
private function createByCallback($name, mixed $callback) private function createByCallback($name, mixed $callback)
{ {
if ($this->creates === -1 && !is_callable($callback)) { if ($this->creates === -1 && !is_callable($callback)) {
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
$event = Snowflake::app()->getEvent();
$event->on(Event::SERVER_WORKER_STOP, function () {
Timer::clear($this->creates);
});
} }
if (!Context::hasContext('create::client::ing::' . $name)) { if (!Context::hasContext('create::client::ing::' . $name)) {
$this->push($name, $this->createClient($name, $callback)); $this->push($name, $this->createClient($name, $callback));