eee
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server\Task;
|
||||||
|
|
||||||
|
interface OnTaskInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $task_id
|
||||||
|
* @param int $src_worker_id
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function process(int $task_id, int $src_worker_id): mixed;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+15
-9
@@ -67,22 +67,28 @@ class Task
|
|||||||
* @param int $task_id
|
* @param int $task_id
|
||||||
* @param int $src_worker_id
|
* @param int $src_worker_id
|
||||||
* @param mixed $data
|
* @param mixed $data
|
||||||
* @return mixed
|
* @return void
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data): mixed
|
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data): void
|
||||||
{
|
{
|
||||||
|
$response = 'task data format failed.';
|
||||||
try {
|
try {
|
||||||
$data = json_decode($data, true);
|
$data = json_decode($data, true);
|
||||||
if (is_null($data)) {
|
if (!is_null($data)) {
|
||||||
return null;
|
[$handler, $params] = [$data[0], $data[1]];
|
||||||
}
|
|
||||||
[$handler, $params] = [$data[0], $data[1]];
|
|
||||||
|
|
||||||
$handler[0] = Kiri::getDi()->get($handler[0]);
|
$handler = Kiri::getDi()->make($handler, $params);
|
||||||
return call_user_func($handler, $task_id, $src_worker_id, $params);
|
if (!($handler instanceof OnTaskInterface)) {
|
||||||
|
throw new \Exception('Task process must implements ' . OnTaskInterface::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = call_user_func([$handler, 'process'], $task_id, $src_worker_id);
|
||||||
|
}
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
return $this->exception->emit($throwable, response());
|
$response = throwable($throwable);
|
||||||
|
} finally {
|
||||||
|
$server->finish($response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+20
-5
@@ -7,17 +7,34 @@ use Kiri\Server\ServerInterface;
|
|||||||
class TaskExecute implements TaskInterface
|
class TaskExecute implements TaskInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param OnTaskInterface $handler
|
||||||
|
* @param mixed $data
|
||||||
|
* @param int $dstWorkerId
|
||||||
|
* @param callable|null $finishFinishCallback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function task(OnTaskInterface $handler, mixed $data, int $dstWorkerId = -1, ?callable $finishFinishCallback = null): void
|
||||||
|
{
|
||||||
|
$server = \Kiri::getDi()->get(ServerInterface::class);
|
||||||
|
|
||||||
|
$server->task([$handler, $data], $dstWorkerId, $finishFinishCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param OnTaskInterface $handler
|
||||||
* @param mixed $data
|
* @param mixed $data
|
||||||
* @param float $timeout
|
* @param float $timeout
|
||||||
* @param int $dstWorkerId
|
* @param int $dstWorkerId
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function taskWait(mixed $data, float $timeout = 0.5, int $dstWorkerId = -1): mixed
|
public function taskWait(OnTaskInterface $handler, mixed $data, float $timeout = 0.5, int $dstWorkerId = -1): mixed
|
||||||
{
|
{
|
||||||
$server = \Kiri::getDi()->get(ServerInterface::class);
|
$server = \Kiri::getDi()->get(ServerInterface::class);
|
||||||
|
|
||||||
return $server->taskwait($data, $timeout, $dstWorkerId);
|
return $server->taskwait([$handler, $data], $timeout, $dstWorkerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -45,6 +62,4 @@ class TaskExecute implements TaskInterface
|
|||||||
|
|
||||||
return $server->taskWaitMulti($tasks, $timeout);
|
return $server->taskWaitMulti($tasks, $timeout);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
+21
-8
@@ -7,11 +7,24 @@ interface TaskInterface
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $tasks
|
* @param OnTaskInterface $handler
|
||||||
* @param float $timeout
|
* @param mixed $data
|
||||||
* @return false|array
|
* @param int $dstWorkerId
|
||||||
|
* @param callable|null $finishFinishCallback
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function taskWaitMulti(array $tasks, float $timeout = 0.5): false|array;
|
public function task(OnTaskInterface $handler, mixed $data, int $dstWorkerId = -1, ?callable $finishFinishCallback = null): void;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param OnTaskInterface $handler
|
||||||
|
* @param mixed $data
|
||||||
|
* @param float $timeout
|
||||||
|
* @param int $dstWorkerId
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function taskWait(OnTaskInterface $handler, mixed $data, float $timeout = 0.5, int $dstWorkerId = -1): mixed;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $tasks
|
* @param array $tasks
|
||||||
@@ -20,12 +33,12 @@ interface TaskInterface
|
|||||||
*/
|
*/
|
||||||
public function taskCo(array $tasks, float $timeout = 0.5): false|array;
|
public function taskCo(array $tasks, float $timeout = 0.5): false|array;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $data
|
* @param array $tasks
|
||||||
* @param float $timeout
|
* @param float $timeout
|
||||||
* @param int $dstWorkerId
|
* @return false|array
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public function taskWait(mixed $data, float $timeout = 0.5, int $dstWorkerId = -1): mixed;
|
public function taskWaitMulti(array $tasks, float $timeout = 0.5): false|array;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server\Task;
|
||||||
|
|
||||||
|
class TestTask implements OnTaskInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct($user, $friend)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $task_id
|
||||||
|
* @param int $src_worker_id
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function process(int $task_id, int $src_worker_id): int
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user