This commit is contained in:
2021-01-04 18:21:37 +08:00
parent 4c8e81f417
commit aa7b663967
3 changed files with 72 additions and 25 deletions
+13
View File
@@ -23,6 +23,7 @@ use HttpServer\Service\Receive;
use HttpServer\Service\Websocket;
use Kafka\Producer;
use Annotation\Annotation as SAnnotation;
use Snowflake\Async;
use Snowflake\Cache\Redis;
use Snowflake\Di\Service;
use Snowflake\Error\ErrorHandler;
@@ -46,6 +47,7 @@ use Database\DatabasesProviders;
* @property \Redis|Redis $redis
* @property Server $server
* @property DatabasesProviders $db
* @property Async $async
* @property Connection $connections
* @property Logger $logger
* @property Jwt $jwt
@@ -391,6 +393,16 @@ abstract class BaseApplication extends Service
}
/**
* @return Async
* @throws ComponentException
*/
public function getAsync(): Async
{
return $this->get('async');
}
/**
* @throws Exception
*/
@@ -410,6 +422,7 @@ abstract class BaseApplication extends Service
'router' => ['class' => Router::class],
'redis' => ['class' => Redis::class],
'jwt' => ['class' => Jwt::class],
'async' => ['class' => Async::class],
'goto' => ['class' => BaseGoto::class],
'http2' => ['class' => Http2::class],
]);
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace Snowflake;
use Exception;
use HttpServer\IInterface\Task;
use Snowflake\Abstracts\Component;
/**
* Class Async
* @package Snowflake
*/
class Async extends Component
{
/**
* @param Task $class
* @throws Exception
*/
public function dispatch(Task $class)
{
$server = Snowflake::app()->server->getServer();
if (!isset($server->setting['task_worker_num']) || !class_exists($class)) {
return;
}
$randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1);
$server->task(serialize($class), $randWorkerId);
}
}