This commit is contained in:
2021-07-21 11:25:31 +08:00
parent 3b05c856ef
commit 8f6079b040
9 changed files with 201 additions and 112 deletions
+26 -1
View File
@@ -3,8 +3,10 @@
namespace Server;
use Closure;
use Exception;
use ReflectionException;
use Server\SInterface\CustomProcess;
use Server\SInterface\TaskExecute;
use Server\Task\ServerTask;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
@@ -173,7 +175,7 @@ class ServerManager extends Abstracts\Server
* @param array $config
* @throws NotFindClassException
* @throws ReflectionException
* @throws \Exception
* @throws Exception
*/
private function startListenerHandler(ServerManager $context, array $config)
{
@@ -355,6 +357,29 @@ class ServerManager extends Abstracts\Server
}
/**
* @param TaskExecute|string $handler
* @param array $params
* @param int $workerId
* @throws Exception
*/
public function task(TaskExecute|string $handler, array $params = [], int $workerId = 0)
{
if ($workerId === null || $workerId <= $this->server->setting['worker_num']) {
$workerId = random_int($this->server->setting['worker_num'] + 1,
$this->server->setting['worker_num'] + 1 + $this->server->setting['task_worker_num']);
}
if (is_string($handler)) {
$implements = Snowflake::getDi()->getReflect($handler);
if (!in_array(TaskExecute::class, $implements->getInterfaceNames())) {
throw new Exception('Task must instance ' . TaskExecute::class);
}
$handler = $implements->newInstanceArgs($params);
}
$this->server->task(serialize($handler), $workerId);
}
/**
* @param array $events
* @throws NotFindClassException