Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e399837dad | |||
| 7b8a43f94f | |||
| c0f8133926 | |||
| f533d3262b | |||
| c767a1745a | |||
| 251e05dbf7 | |||
| 5defd5fb30 | |||
| 95f52b58fd | |||
| 0a97c9b29a | |||
| 3336573b4d | |||
| 44187c2354 | |||
| a058f3ed99 | |||
| faff485006 | |||
| e2b012126a | |||
| b36e21d8ee | |||
| d191296ecc | |||
| 03825dcb09 |
@@ -149,9 +149,7 @@ class HotReload extends AbstractProcess
|
|||||||
public function readFile(string $directory): void
|
public function readFile(string $directory): void
|
||||||
{
|
{
|
||||||
if (str_ends_with($directory, '.php') === true) {
|
if (str_ends_with($directory, '.php') === true) {
|
||||||
$wd = inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
|
inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
|
||||||
|
|
||||||
$this->watches[] = $wd;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Kiri\Server\Handler;
|
namespace Kiri\Server\Handler;
|
||||||
|
|
||||||
|
use Kiri;
|
||||||
use Kiri\Server\Abstracts\Server;
|
use Kiri\Server\Abstracts\Server;
|
||||||
use Kiri\Server\Contract\OnPipeMessageInterface;
|
use Kiri\Server\Contract\OnPipeMessageInterface;
|
||||||
|
|
||||||
@@ -20,13 +21,17 @@ class OnPipeMessage extends Server
|
|||||||
*/
|
*/
|
||||||
public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message): void
|
public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message): void
|
||||||
{
|
{
|
||||||
if (is_string($message)) {
|
try {
|
||||||
$message = unserialize($message);
|
if (is_string($message)) {
|
||||||
|
$message = unserialize($message);
|
||||||
|
}
|
||||||
|
if (!is_object($message) || !($message instanceof OnPipeMessageInterface)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
call_user_func([$message, 'process'], $server, $src_worker_id);
|
||||||
|
} catch (\Throwable $throwable) {
|
||||||
|
Kiri::getLogger()->error(throwable($throwable));
|
||||||
}
|
}
|
||||||
if (!is_object($message) || !($message instanceof OnPipeMessageInterface)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
call_user_func([$message, 'process'], $server, $src_worker_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ namespace Kiri\Server\Processes;
|
|||||||
|
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
use Swoole\Process;
|
use Swoole\Process;
|
||||||
|
use const SIGHUP;
|
||||||
|
use const SIGTERM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -125,26 +127,29 @@ abstract class AbstractProcess implements OnProcessInterface
|
|||||||
{
|
{
|
||||||
$this->process = $process;
|
$this->process = $process;
|
||||||
if ($this->enable_coroutine) {
|
if ($this->enable_coroutine) {
|
||||||
Coroutine::set([
|
$array['enable_deadlock_check'] = false;
|
||||||
'enable_deadlock_check' => false,
|
$array['deadlock_check_disable_trace'] = false;
|
||||||
'deadlock_check_disable_trace' => false,
|
$array['exit_condition'] = [$this, 'exit_condition'];
|
||||||
'exit_condition' => function () {
|
Coroutine::set($array);
|
||||||
return Coroutine::stats()['coroutine_num'] === 0;
|
Coroutine::create(fn() => $this->coroutineWaitSignal());
|
||||||
}
|
|
||||||
]);
|
|
||||||
Coroutine::create(fn () => $this->coroutineWaitSignal());
|
|
||||||
} else {
|
} else {
|
||||||
pcntl_signal(SIGTERM, [$this, 'pointWaitSignal']);
|
$process::signal(SIGTERM | SIGINT | SIGUSR1, [$this, 'pointWaitSignal']);
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function exit_condition(): bool
|
||||||
|
{
|
||||||
|
return Coroutine::stats()['coroutine_num'] === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $data
|
* @param $signal
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function pointWaitSignal($data): void
|
public function pointWaitSignal($signal): void
|
||||||
{
|
{
|
||||||
$this->stop = true;
|
$this->stop = true;
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ class OnTaskFinish
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $task_id
|
* @param int $task_id
|
||||||
* @param mixed $data
|
* @param mixed $data
|
||||||
*/
|
*/
|
||||||
public function __construct(int $task_id, mixed $data)
|
public function __construct(public int $task_id, public mixed $data)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+12
-8
@@ -67,20 +67,24 @@ 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
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$data = json_decode($data, true);
|
[$handler, $params] = [$data[0], $data[1]];
|
||||||
if (is_null($data)) {
|
|
||||||
return null;
|
$handler = Kiri::getDi()->make($handler, $params);
|
||||||
|
if (!($handler instanceof OnTaskInterface)) {
|
||||||
|
throw new \Exception('Task process must implements ' . OnTaskInterface::class);
|
||||||
}
|
}
|
||||||
$data[0] = Kiri::getDi()->get($data[0]);
|
|
||||||
return call_user_func($data, $task_id, $src_worker_id);
|
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+30
-4
@@ -2,22 +2,50 @@
|
|||||||
|
|
||||||
namespace Kiri\Server\Task;
|
namespace Kiri\Server\Task;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Kiri\Server\ServerInterface;
|
use Kiri\Server\ServerInterface;
|
||||||
|
|
||||||
class TaskExecute implements TaskInterface
|
class TaskExecute implements TaskInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param string $handler
|
||||||
|
* @param mixed $data
|
||||||
|
* @param int $dstWorkerId
|
||||||
|
* @param callable|null $finishFinishCallback
|
||||||
|
* @return void
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function task(string $handler, mixed $data, int $dstWorkerId = -1, ?callable $finishFinishCallback = null): void
|
||||||
|
{
|
||||||
|
$array = class_implements($handler, true);
|
||||||
|
if (!in_array(OnTaskInterface::class, $array, true)) {
|
||||||
|
throw new Exception('Task is not implement OnTaskInterface');
|
||||||
|
}
|
||||||
|
$server = \Kiri::getDi()->get(ServerInterface::class);
|
||||||
|
|
||||||
|
$server->task([$handler, $data], $dstWorkerId, $finishFinishCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $handler
|
||||||
* @param mixed $data
|
* @param mixed $data
|
||||||
* @param float $timeout
|
* @param float $timeout
|
||||||
* @param int $dstWorkerId
|
* @param int $dstWorkerId
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function taskWait(mixed $data, float $timeout = 0.5, int $dstWorkerId = -1): mixed
|
public function taskWait(string $handler, mixed $data, float $timeout = 0.5, int $dstWorkerId = -1): mixed
|
||||||
{
|
{
|
||||||
|
$array = class_implements($handler, true);
|
||||||
|
if (!in_array(OnTaskInterface::class, $array, true)) {
|
||||||
|
throw new Exception('Task is not implement OnTaskInterface');
|
||||||
|
}
|
||||||
$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 +73,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 string $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(string $handler, mixed $data, int $dstWorkerId = -1, ?callable $finishFinishCallback = null): void;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $handler
|
||||||
|
* @param mixed $data
|
||||||
|
* @param float $timeout
|
||||||
|
* @param int $dstWorkerId
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function taskWait(string $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