From 9c20cf51bb29adb3c072dda1d2fd890b106b2bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 8 Sep 2020 15:30:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/IInterface/Task.php | 2 ++ System/Snowflake.php | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/HttpServer/IInterface/Task.php b/HttpServer/IInterface/Task.php index 78df366a..1285dc07 100644 --- a/HttpServer/IInterface/Task.php +++ b/HttpServer/IInterface/Task.php @@ -9,6 +9,8 @@ interface Task public function getParams(); + public function setParams(array $params); + public function handler(); } diff --git a/System/Snowflake.php b/System/Snowflake.php index 1709846c..39398b7d 100644 --- a/System/Snowflake.php +++ b/System/Snowflake.php @@ -5,6 +5,7 @@ namespace Snowflake; use Exception; +use HttpServer\IInterface\Task; use ReflectionException; use Snowflake\Abstracts\Config; use Snowflake\Di\Container; @@ -187,7 +188,29 @@ class Snowflake public static function clearProcessId($worker_pid) { + } + + /** + * @param string $class + * @param array $params + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + public static function async(string $class, array $params = []) + { + $server = static::app()->server->getServer(); + if (!isset($server->setting['task_worker_num'])) { + return; + } + $randWorkerId = random_int(0, $server->setting['task_worker_num']); + + /** @var Task $class */ + $class = static::createObject($class); + $class->setParams($params); + + $server->task(serialize($class), $randWorkerId); }