This commit is contained in:
2021-04-26 17:34:33 +08:00
parent 76f9688ea6
commit 52357f8283
7 changed files with 577 additions and 487 deletions
+10 -10
View File
@@ -47,21 +47,21 @@ class OnPipeMessage extends Callback
throw new Exception('unknown handler'); throw new Exception('unknown handler');
} }
/** @var Crontab $handler */ /** @var Crontab $handler */
$handler = $message['handler']; $handler = swoole_unserialize($message['handler']);
defer(function () use ($handler) { defer(function () use ($handler) {
$return = $handler->isRecover(); if ($handler->isRecover() !== 999) {
if ($return === 999) { return;
$name = $handler->getName(); }
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
if ($redis->exists('stop:crontab:' . $name)) {
$redis->del('crontab:' . $name); $name = $handler->getName();
$redis->del('stop:crontab:' . $name); if (!$redis->exists('stop:crontab:' . $name)) {
} else {
$redis->set('crontab:' . $name, swoole_serialize($handler)); $redis->set('crontab:' . $name, swoole_serialize($handler));
$tickTime = time() + $handler->getTickTime(); $tickTime = time() + $handler->getTickTime();
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name); $redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name);
} } else {
$redis->del('crontab:' . $name);
$redis->del('stop:crontab:' . $name);
} }
}); });
$handler->increment()->execute(); $handler->increment()->execute();
+32 -3
View File
@@ -10,7 +10,8 @@ use RdKafka\ConsumerTopic;
use RdKafka\Exception; use RdKafka\Exception;
use RdKafka\KafkaConsumer; use RdKafka\KafkaConsumer;
use RdKafka\TopicConf; use RdKafka\TopicConf;
use Snowflake\Core\Json; use Snowflake\Abstracts\Config;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
use Swoole\Coroutine\System; use Swoole\Coroutine\System;
@@ -29,19 +30,47 @@ class Kafka extends \Snowflake\Process\Process
private int $maxLength = 5000; private int $maxLength = 5000;
/**
* @var array
*/
private array $kafkaConfig = [];
/**
* @return string
* @throws ConfigException
*/
public function getProcessName(): string
{
$this->kafkaConfig = swoole_unserialize($this->read());
$name = Config::get('id', 'system') . '[' . $this->pid . ']';
return $name . '.' . 'Kafka Consumer ' . $this->kafkaConfig['topic'];
}
/** /**
* @param Process $process * @param Process $process
* @throws \Exception * @throws \Exception
*/ */
public function onHandler(Process $process): void public function before(Process $process): void
{ {
$content = System::readFile(storage('runtime.php')); $content = System::readFile(storage('runtime.php'));
$annotation = Snowflake::app()->getAnnotation(); $annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize($content)); $annotation->setLoader(unserialize($content));
$annotation->runtime(KAFKA_PATH); $annotation->runtime(KAFKA_PATH);
}
$this->waite(swoole_unserialize($process->read()));
/**
* @param Process $process
* @throws \Exception
*/
public function onHandler(Process $process): void
{
$this->waite($this->kafkaConfig);
} }
+5 -3
View File
@@ -8,6 +8,7 @@ use Exception;
use HttpServer\Server; use HttpServer\Server;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Cache\Redis; use Snowflake\Cache\Redis;
use Snowflake\Exception\ConfigException;
use Snowflake\Process\Process; use Snowflake\Process\Process;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine; use Swoole\Coroutine;
@@ -28,7 +29,8 @@ class Zookeeper extends Process
/** /**
* @param \Swoole\Process $process * @return string
* @throws ConfigException
*/ */
public function getProcessName(): string public function getProcessName(): string
{ {
@@ -85,7 +87,7 @@ class Zookeeper extends Process
if (empty($handler = $redis->get('crontab:' . $value))) { if (empty($handler = $redis->get('crontab:' . $value))) {
return; return;
} }
$params['handler'] = unserialize($handler); $params['handler'] = $handler;
$this->server->sendMessage($params, $this->getWorker()); $this->server->sendMessage($params, $this->getWorker());
} catch (Throwable $exception) { } catch (Throwable $exception) {
@@ -105,8 +107,8 @@ class Zookeeper extends Process
/** /**
* @param Redis|\Redis $redis
* @return array * @return array
* @throws Exception
*/ */
private function loadCarobTask(Redis|\Redis $redis): array private function loadCarobTask(Redis|\Redis $redis): array
{ {
+13
View File
@@ -21,6 +21,19 @@ class LoggerProcess extends Process
{ {
public function before(\Swoole\Process $process): void
{
// TODO: Implement before() method.
}
public function getProcessName(): string
{
// TODO: Implement getProcessName() method.
}
/** /**
* @param \Swoole\Process $process * @param \Swoole\Process $process
* @throws ComponentException * @throws ComponentException
+20
View File
@@ -5,6 +5,7 @@ namespace Snowflake\Process;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Timer; use Swoole\Timer;
@@ -16,6 +17,25 @@ class Biomonitoring extends Process
{ {
/**
* @param \Swoole\Process $process
*/
public function before(\Swoole\Process $process): void
{
// TODO: Implement before() method.
}
/**
* @return string
*/
#[Pure] public function getProcessName(): string
{
// TODO: Implement getProcessName() method.
return get_called_class();
}
/** /**
* @param \Swoole\Process $process * @param \Swoole\Process $process
* @throws Exception * @throws Exception
+12 -4
View File
@@ -8,10 +8,18 @@ interface SProcess
{ {
// public function getProcessName(): string; /**
// * @return string
// *
// public function before(\Swoole\Process $process): void; * return process name
*/
public function getProcessName(): string;
/**
* @param \Swoole\Process $process
*/
public function before(\Swoole\Process $process): void;
/** /**
* @param \Swoole\Process $process * @param \Swoole\Process $process
+22 -4
View File
@@ -12,10 +12,8 @@ namespace Snowflake\Process;
use Exception; use Exception;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Event; use Swoole\Event;
use Swoole\Timer; use Swoole\Timer;
@@ -37,16 +35,36 @@ class ServerInotify extends Process
/** /**
* @param \Swoole\Process $process * @param \Swoole\Process $process
* @throws Exception * @throws ConfigException
*/ */
public function onHandler(\Swoole\Process $process): void public function before(\Swoole\Process $process): void
{ {
// TODO: Implement before() method.
set_error_handler([$this, 'onErrorHandler']); set_error_handler([$this, 'onErrorHandler']);
$this->dirs = Config::get('inotify', [APP_PATH]); $this->dirs = Config::get('inotify', [APP_PATH]);
if (extension_loaded('inotify')) { if (extension_loaded('inotify')) {
$this->inotify = inotify_init(); $this->inotify = inotify_init();
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE; $this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
}
}
/**
* @return string
*/
public function getProcessName(): string
{
return 'file change process';
}
/**
* @param \Swoole\Process $process
* @throws Exception
*/
public function onHandler(\Swoole\Process $process): void
{
if (extension_loaded('inotify')) {
foreach ($this->dirs as $dir) { foreach ($this->dirs as $dir) {
if (!is_dir($dir)) continue; if (!is_dir($dir)) continue;
$this->watch($dir); $this->watch($dir);