This commit is contained in:
2023-04-22 02:46:58 +08:00
parent d501e85153
commit e8c4a729a5
+10 -4
View File
@@ -65,19 +65,25 @@ class Task implements TaskInterface
/** /**
* @param array $handler * @param array|string|object $handler
* @param int|null $workerId * @param int|null $workerId
* @return void * @return void
* @throws * @throws ReflectionException
*/ */
public function dispatch(array $handler, ?int $workerId = null): void public function dispatch(array|string|object $handler, ?int $workerId = null): void
{ {
/** @var Server $server */ /** @var Server $server */
$server = \Kiri::service()->get('server'); $server = \Kiri::service()->get('server');
if (is_null($workerId)) { if (is_null($workerId)) {
$workerId = rand(0, $server->setting[Constant::OPTION_TASK_WORKER_NUM] - 1); $workerId = rand(0, $server->setting[Constant::OPTION_TASK_WORKER_NUM] - 1);
} }
$server->task(json_encode($handler), $workerId); if (is_string($handler)) {
$server->task(serialize([di($handler), 'handle']), $workerId);
} else if (is_string($handler[0])) {
$server->task(serialize($handler), $workerId);
} else {
$server->task(serialize([$handler, 'handle']), $workerId);
}
} }