This commit is contained in:
2021-04-12 11:05:57 +08:00
parent 996ff50e99
commit c62be31e6e
4 changed files with 43 additions and 38 deletions
+25 -24
View File
@@ -7,6 +7,7 @@ namespace HttpServer\Events;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use Kafka\Struct; use Kafka\Struct;
use Snowflake\Crontab\Crontab;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Swoole\Server; use Swoole\Server;
@@ -22,16 +23,15 @@ class OnPipeMessage extends Callback
* @param Server $server * @param Server $server
* @param int $src_worker_id * @param int $src_worker_id
* @param $message * @param $message
* @throws ComponentException
* @throws Exception * @throws Exception
*/ */
public function onHandler(Server $server, int $src_worker_id, $message) public function onHandler(Server $server, int $src_worker_id, $message)
{ {
try { try {
$swoole_unserialize = swoole_unserialize($message); $swollen_universalize = swoole_unserialize($message);
match ($swoole_unserialize['action'] ?? null) { match ($swollen_universalize['action'] ?? null) {
'kafka' => $this->onKafkaWorker($swoole_unserialize), 'kafka' => $this->onKafkaWorker($swollen_universalize),
'crontab' => $this->onCrontabWorker($swoole_unserialize), 'crontab' => $this->onCrontabWorker($swollen_universalize),
default => $this->onMessageWorker($server, $src_worker_id, $message) default => $this->onMessageWorker($server, $src_worker_id, $message)
}; };
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
@@ -42,26 +42,28 @@ class OnPipeMessage extends Callback
} }
/** /**
* @param array $message * @param array $message
* @return string * @return string
*/ * @throws Exception
private function onCrontabWorker(array $message) */
private function onCrontabWorker(array $message): string
{ {
/** @var \Snowflake\Crontab\Crontab $crontab */ /** @var Crontab $crontab */
$crontab = $message['handler'] ?? null; $crontab = $message['handler'] ?? null;
$crontab->increment()->execute(); $crontab->increment()->execute();
return 'success'; return 'success';
} }
/** /**
* @param $server * @param $server
* @param $src_worker_id * @param $src_worker_id
* @param $message * @param $message
* @throws \Exception * @return string
*/ * @throws Exception
private function onMessageWorker($server, $src_worker_id, $message) */
private function onMessageWorker($server, $src_worker_id, $message): string
{ {
fire(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]); fire(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]);
@@ -69,12 +71,11 @@ class OnPipeMessage extends Callback
} }
/** /**
* @param array $message * @param array $message
* @throws \ReflectionException * @return string
* @throws \Snowflake\Exception\NotFindClassException */
*/ private function onKafkaWorker(array $message): string
private function onKafkaWorker(array $message)
{ {
[$topic, $rdMessage] = $message['body']; [$topic, $rdMessage] = $message['body'];
+5 -4
View File
@@ -6,6 +6,7 @@ namespace Snowflake\Crontab;
use Closure; use Closure;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -64,9 +65,9 @@ class Crontab extends BaseObject
/** /**
* @return string * @return string
*/ */
public function getName(): string #[Pure] public function getName(): string
{ {
return $this->name; return md5($this->name);
} }
/** /**
@@ -203,7 +204,7 @@ class Crontab extends BaseObject
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$redis->set('crontab:' . ($name = md5($this->getName())), swoole_serialize($this)); $redis->set('crontab:' . ($name = $this->getName()), swoole_serialize($this));
$tickTime = time() + $this->getTickTime(); $tickTime = time() + $this->getTickTime();
@@ -219,7 +220,7 @@ class Crontab extends BaseObject
try { try {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$name_md5 = md5($this->getName()); $name_md5 = $this->getName();
$redis->hSet(self::WAIT_END, $name_md5, serialize($this)); $redis->hSet(self::WAIT_END, $name_md5, serialize($this));
$params = call_user_func($this->handler, $this->params, $this->name); $params = call_user_func($this->handler, $this->params, $this->name);
+1 -1
View File
@@ -27,7 +27,7 @@ class Producer extends Component
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$name = md5($crontab->getName()); $name = $crontab->getName();
$redis->set('crontab:' . $name, swoole_serialize($crontab)); $redis->set('crontab:' . $name, swoole_serialize($crontab));
+12 -9
View File
@@ -10,6 +10,7 @@ use Snowflake\Snowflake;
use Swoole\Coroutine\WaitGroup; use Swoole\Coroutine\WaitGroup;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
use Swoole\Timer; use Swoole\Timer;
use Snowflake\Cache\Redis;
/** /**
* Class Zookeeper * Class Zookeeper
@@ -43,7 +44,7 @@ class Zookeeper extends Process
name($this->pid, 'Crontab zookeeper.'); name($this->pid, 'Crontab zookeeper.');
} }
Timer::tick(1000, function () { Timer::tick(1000, function () {
[$range, $redis] = $this->loadCrotabTask(); [$range, $redis] = $this->loadCarobTask();
$server = Snowflake::app()->getSwoole(); $server = Snowflake::app()->getSwoole();
$setting = $server->setting['worker_num']; $setting = $server->setting['worker_num'];
@@ -55,12 +56,14 @@ class Zookeeper extends Process
} }
/** /**
* @param $server * @param $server
* @param $redis * @param Redis|\Redis $redis
* @throws \Exception * @param int $setting
*/ * @param $value
private function dispatch($server, $redis, $setting, $value) * @throws Exception
*/
private function dispatch($server, Redis|\Redis $redis, int $setting, $value)
{ {
$server->sendMessage(swoole_serialize([ $server->sendMessage(swoole_serialize([
'action' => 'crontab', 'handler' => swoole_unserialize($redis->get('crontab:' . $value)) 'action' => 'crontab', 'handler' => swoole_unserialize($redis->get('crontab:' . $value))
@@ -71,9 +74,9 @@ class Zookeeper extends Process
/** /**
* @return array * @return array
* @throws \Exception * @throws Exception
*/ */
private function loadCrotabTask() private function loadCarobTask(): array
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();