This commit is contained in:
as2252258@163.com
2021-02-28 14:46:17 +08:00
parent ffc02cc33a
commit 5a0c35453d
2 changed files with 209 additions and 206 deletions
+15 -12
View File
@@ -6,6 +6,7 @@ namespace HttpServer\Events;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use Snowflake\Event;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Server; use Swoole\Server;
@@ -15,17 +16,19 @@ use Swoole\Server;
*/ */
class OnFinish extends Callback class OnFinish extends Callback
{ {
/** /**
* @param Server $server * @param Server $server
* @param $task_id * @param $task_id
* @param $data * @param $data
* @throws Exception * @throws Exception
*/ */
public function onHandler(Server $server, $task_id, $data) public function onHandler(Server $server, $task_id, $data)
{ {
$data = json_decode($data, true); try {
$data['work_id'] = $task_id; fire(Event::TASK_FINISH, [$task_id, $data]);
$this->write(var_export($data, true), 'Task'); } catch (\Throwable $exception) {
} $this->addError($exception, 'task');
}
}
} }
+194 -194
View File
@@ -17,229 +17,229 @@ use Snowflake\Core\ArrayAccess;
class Event extends BaseObject class Event extends BaseObject
{ {
public bool $isVide = true; public bool $isVide = true;
private array $_events = []; private array $_events = [];
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';
const PIPE_MESSAGE = 'SERVER:PIPE:MESSAGE'; const PIPE_MESSAGE = 'SERVER:PIPE:MESSAGE';
const TASK_FINISH = 'SERVER:TASK::FINISH';
const EVENT_AFTER_REQUEST = 'SERVER:REQUEST:AFTER:START'; const EVENT_AFTER_REQUEST = 'SERVER:REQUEST:AFTER:START';
const EVENT_BEFORE_REQUEST = 'SERVER:REQUEST:BEFORE:START'; const EVENT_BEFORE_REQUEST = 'SERVER:REQUEST:BEFORE:START';
const RECEIVE_CONNECTION = 'SERVER:RECEIVE:CONNECTION'; const RECEIVE_CONNECTION = 'SERVER:RECEIVE:CONNECTION';
const SYSTEM_RESOURCE_RELEASES = 'SYSTEM::RESOURCE::RELEASES'; const SYSTEM_RESOURCE_RELEASES = 'SYSTEM::RESOURCE::RELEASES';
const SYSTEM_RESOURCE_CLEAN = 'SYSTEM::RESOURCE::CLEAN'; const SYSTEM_RESOURCE_CLEAN = 'SYSTEM::RESOURCE::CLEAN';
const PROCESS_WORKER_STOP = 'SERVER:PROCESS:WORKER:STOP'; const PROCESS_WORKER_STOP = 'SERVER:PROCESS:WORKER:STOP';
const SERVER_AFTER_RELOAD = 'SERVER:AFTER:RELOAD'; const SERVER_AFTER_RELOAD = 'SERVER:AFTER:RELOAD';
const SERVER_BEFORE_RELOAD = 'SERVER:BEFORE:RELOAD'; const SERVER_BEFORE_RELOAD = 'SERVER:BEFORE:RELOAD';
const SERVER_EVENT_START = 'SERVER:EVENT:START'; const SERVER_EVENT_START = 'SERVER:EVENT:START';
const SERVER_MANAGER_START = 'SERVER:EVENT:MANAGER:START'; const SERVER_MANAGER_START = 'SERVER:EVENT:MANAGER:START';
const SERVER_MANAGER_STOP = 'SERVER:EVENT:MANAGER:START'; const SERVER_MANAGER_STOP = 'SERVER:EVENT:MANAGER:START';
const SERVER_WORKER_STOP = 'SERVER:EVENT:WORKER:STOP'; const SERVER_WORKER_STOP = 'SERVER:EVENT:WORKER:STOP';
const SERVER_WORKER_START = 'SERVER:EVENT:WORKER:START'; const SERVER_WORKER_START = 'SERVER:EVENT:WORKER:START';
const SERVER_AFTER_WORKER_START = 'SERVER:EVENT:AFTER:WORKER:START'; const SERVER_AFTER_WORKER_START = 'SERVER:EVENT:AFTER:WORKER:START';
const SERVER_BEFORE_START = 'SERVER:EVENT:BEFORE:START'; const SERVER_BEFORE_START = 'SERVER:EVENT:BEFORE:START';
const SERVER_TASK_START = 'SERVER:EVENT:TASK:START'; const SERVER_TASK_START = 'SERVER:EVENT:TASK:START';
const SERVER_WORKER_EXIT = 'SERVER:EVENT:WORKER:EXIT'; const SERVER_WORKER_EXIT = 'SERVER:EVENT:WORKER:EXIT';
const SERVER_WORKER_ERROR = 'SERVER:EVENT:WORKER:ERROR'; const SERVER_WORKER_ERROR = 'SERVER:EVENT:WORKER:ERROR';
const SERVER_SHUTDOWN = 'SERVER:EVENT:SHUTDOWN'; const SERVER_SHUTDOWN = 'SERVER:EVENT:SHUTDOWN';
const SERVER_HANDSHAKE = 'on handshake'; const SERVER_HANDSHAKE = 'on handshake';
const SERVER_MESSAGE = 'on message'; const SERVER_MESSAGE = 'on message';
const SERVER_CLOSE = 'on close'; const SERVER_CLOSE = 'on close';
/**
/** * @param $name
* @param $name * @param $callback
* @param $callback * @param array $parameter
* @param array $parameter * @param bool $isAppend
* @param bool $isAppend * @throws Exception
* @throws Exception */
*/ public function on($name, $callback, $parameter = [], $isAppend = true)
public function on($name, $callback, $parameter = [], $isAppend = true) {
{ if (!isset($this->_events[$name])) {
if (!isset($this->_events[$name])) { $this->_events[$name] = [];
$this->_events[$name] = []; }
} if ($callback instanceof \Closure) {
if ($callback instanceof \Closure) { $callback = \Closure::bind($callback, Snowflake::app());
$callback = \Closure::bind($callback, Snowflake::app()); } else if (is_array($callback) && is_string($callback[0])) {
} else if (is_array($callback) && is_string($callback[0])) { if (!class_exists($callback[0])) {
if (!class_exists($callback[0])) { throw new Exception('Undefined callback class.');
throw new Exception('Undefined callback class.'); }
} $callback[0] = Snowflake::createObject($callback[0]);
$callback[0] = Snowflake::createObject($callback[0]); }
} if ($this->exists($name, $callback)) {
if ($this->exists($name, $callback)) { return;
return; }
} if (!empty($this->_events[$name]) && $isAppend === true) {
if (!empty($this->_events[$name]) && $isAppend === true) { array_unshift($this->_events[$name], [$callback, $parameter]);
array_unshift($this->_events[$name], [$callback, $parameter]); } else {
} else { $this->_events[$name][] = [$callback, $parameter];
$this->_events[$name][] = [$callback, $parameter]; }
} }
}
/** /**
* @param $name * @param $name
* @param $callback * @param $callback
*/ */
public function of($name, $callback): void public function of($name, $callback): void
{ {
if (!isset($this->_events[$name])) { if (!isset($this->_events[$name])) {
return; return;
} }
foreach ($this->_events[$name] as $index => $event) { foreach ($this->_events[$name] as $index => $event) {
[$handler, $parameter] = $event; [$handler, $parameter] = $event;
if ($handler !== $callback) { if ($handler !== $callback) {
continue; continue;
} }
unset($this->_events[$name][$index]); unset($this->_events[$name][$index]);
} }
} }
/** /**
* @param $name * @param $name
* @return bool * @return bool
*/ */
public function offName($name): bool public function offName($name): bool
{ {
if (!$this->exists($name)) { if (!$this->exists($name)) {
return true; return true;
} }
unset($this->_events[$name]); unset($this->_events[$name]);
return $this->exists($name); return $this->exists($name);
} }
/** /**
* @param $name * @param $name
* @param null $callback * @param null $callback
* @return bool * @return bool
*/ */
public function exists($name, $callback = null): bool public function exists($name, $callback = null): bool
{ {
if (!isset($this->_events[$name])) { if (!isset($this->_events[$name])) {
return false; return false;
} }
if ($callback === null) { if ($callback === null) {
return true; return true;
} }
foreach ($this->_events[$name] as $event) { foreach ($this->_events[$name] as $event) {
[$handler, $parameter] = $event; [$handler, $parameter] = $event;
if ($handler === $callback) { if ($handler === $callback) {
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* @param $name * @param $name
* @param $handler * @param $handler
* @return mixed * @return mixed
*/ */
public function get($name, $handler): mixed public function get($name, $handler): mixed
{ {
if (!$this->exists($name)) { if (!$this->exists($name)) {
return null; return null;
} }
foreach ($this->_events[$name] as $event) { foreach ($this->_events[$name] as $event) {
[$callback, $parameter] = $event; [$callback, $parameter] = $event;
if ($callback === $handler) { if ($callback === $handler) {
return $event; return $event;
} }
} }
return null; return null;
} }
public function clean() public function clean()
{ {
$this->_events = []; $this->_events = [];
} }
/** /**
* @param $name * @param $name
* @param array $params * @param array $params
* @param null $scope * @param null $scope
* @return bool * @return bool
*/ */
public function dispatch($name, $params = [], $scope = null): bool public function dispatch($name, $params = [], $scope = null): bool
{ {
return $this->trigger($name, $params, $scope); return $this->trigger($name, $params, $scope);
} }
/** /**
* @param $name * @param $name
* @param null $parameter * @param null $parameter
* @param null $handler * @param null $handler
* @param false $is_remove * @param false $is_remove
* @return bool * @return bool
*/ */
public function trigger($name, $parameter = null, $handler = null, $is_remove = false): bool public function trigger($name, $parameter = null, $handler = null, $is_remove = false): bool
{ {
try { try {
if (!$this->exists($name)) { if (!$this->exists($name)) {
return true; return true;
} }
if (!empty($handler) && $this->exists($name, $handler)) { if (!empty($handler) && $this->exists($name, $handler)) {
$events = [$this->get($name, $handler)]; $events = [$this->get($name, $handler)];
} else { } else {
$events = $this->_events[$name]; $events = $this->_events[$name];
} }
foreach ($events as $index => $event) { foreach ($events as $index => $event) {
$meta = $this->mergeParams($event[1], $parameter); $meta = $this->mergeParams($event[1], $parameter);
if (call_user_func($event[0], ...$meta) === false) { if (call_user_func($event[0], ...$meta) === false) {
return false; return false;
} }
} }
if ($is_remove) { if ($is_remove) {
$this->offName($name); $this->offName($name);
} }
return true; return true;
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
return false; return false;
} }
} }
/** /**
* @param $defaultParameter * @param $defaultParameter
* @param $parameter * @param $parameter
* @return array * @return array
*/ */
#[Pure] private function mergeParams($defaultParameter, $parameter = []): array #[Pure] private function mergeParams($defaultParameter, $parameter = []): array
{ {
if (empty($defaultParameter)) { if (empty($defaultParameter)) {
$defaultParameter = $parameter; $defaultParameter = $parameter;
} else { } else {
if (!is_array($parameter)) { if (!is_array($parameter)) {
$parameter = []; $parameter = [];
} }
foreach ($parameter as $key => $value) { foreach ($parameter as $key => $value) {
$defaultParameter[] = $value; $defaultParameter[] = $value;
} }
} }
if (!is_array($defaultParameter)) { if (!is_array($defaultParameter)) {
$defaultParameter = [$defaultParameter]; $defaultParameter = [$defaultParameter];
} }
return $defaultParameter; return $defaultParameter;
} }
} }