This commit is contained in:
as2252258@163.com
2021-07-20 01:56:04 +08:00
parent d5cc4c2d64
commit 46bcbb4f4b
2 changed files with 180 additions and 197 deletions
+12 -27
View File
@@ -10,6 +10,7 @@ use RdKafka\ConsumerTopic;
use RdKafka\Exception;
use RdKafka\KafkaConsumer;
use RdKafka\TopicConf;
use Server\SInterface\CustomProcess;
use Snowflake\Abstracts\Config;
use Snowflake\Exception\ConfigException;
use Snowflake\Runtime;
@@ -23,55 +24,41 @@ use Throwable;
* Class Queue
* @package Queue
*/
class Kafka extends \Snowflake\Process\Process
class Kafka implements CustomProcess
{
protected Channel $channel;
private int $maxLength = 5000;
/**
* @var array
* Kafka constructor.
* @param array $kafkaConfig
*/
private array $kafkaConfig = [];
public function __construct(public array $kafkaConfig)
{
}
/**
* @return string
* @throws ConfigException
*/
public function getProcessName(): string
public function getProcessName(Process $process): string
{
$this->kafkaConfig = swoole_unserialize($this->read());
$name = Config::get('id', 'system') . '[' . $this->pid . ']';
$name = Config::get('id', 'system') . '[' . $process->pid . ']';
return $name . '.' . 'Kafka Consumer ' . $this->kafkaConfig['topic'];
}
/**
* @param Process $process
* @throws \Exception
*/
public function before(Process $process): void
{
$content = System::readFile(storage(Runtime::CACHE_NAME));
$annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize($content));
$annotation->runtime(KAFKA_PATH);
}
/**
* @param Process $process
* @throws \Exception
*/
public function onHandler(Process $process): void
{
$this->waite($this->kafkaConfig);
$this->waite($process, $this->kafkaConfig);
}
@@ -79,11 +66,9 @@ class Kafka extends \Snowflake\Process\Process
* @param array $kafkaServer
* @throws \Exception
*/
private function waite(array $kafkaServer)
private function waite(Process $process, array $kafkaServer)
{
try {
name($this->pid, 'Kafka Consumer ' . $kafkaServer['topic']);
[$config, $topic, $conf] = $this->kafkaConfig($kafkaServer);
if (empty($config) && empty($topic) && empty($conf)) {
return;
+2 -4
View File
@@ -35,14 +35,12 @@ class KafkaProviders extends Providers
if (!extension_loaded('rdkafka')) {
return;
}
$kafkaServers = Config::get('kafka.consumers', []);
if (empty($kafkaServers)) {
return;
}
foreach ($kafkaServers as $index => $kafkaServer) {
$server->addProcess('kafka_' . $index, Kafka::class, $kafkaServer);
foreach ($kafkaServers as $kafkaServer) {
$server->addProcess(new Kafka($kafkaServer));
}
}