diff --git a/Annotation/Asynchronous.php b/Annotation/Asynchronous.php new file mode 100644 index 00000000..723dbe64 --- /dev/null +++ b/Annotation/Asynchronous.php @@ -0,0 +1,42 @@ +getAsync(); + $async->addAsync($this->name, current($handler)); + return true; + } + +} diff --git a/System/Async.php b/System/Async.php index 399f99ad..eb5cd7dc 100644 --- a/System/Async.php +++ b/System/Async.php @@ -17,21 +17,37 @@ class Async extends Component { + private array $_absences = []; + + /** - * @param string $class + * @param string $name + * @param Task $handler + */ + public function addAsync(string $name, Task $handler) + { + $this->_absences[$name] = $handler; + } + + + /** + * @param string $name * @param array $params * @throws Exception - * @throws ReflectionException */ - public function dispatch(string $class, array $params) + public function dispatch(string $name, array $params) { $server = Snowflake::app()->getSwoole(); - if (!isset($server->setting['task_worker_num']) || !class_exists($class)) { + if (!isset($server->setting['task_worker_num'])) { + return; + } + + if (!isset($this->_absences[$name])) { return; } /** @var Task $class */ - $class = Snowflake::createObject($class); + $class = $this->_absences[$name]; $class->setParams($params); $randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1);