Explorar o código

Merge pull request #2372 from visuddhinanda/development

Development
visuddhinanda hai 4 días
pai
achega
1c3d7330aa

+ 29 - 0
api-v13/app/Console/Commands/CreateQueue.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Attributes\Description;
+use Illuminate\Console\Attributes\Signature;
+use Illuminate\Console\Command;
+
+use App\Services\RabbitMQService;
+
+#[Signature('app:create-queue')]
+#[Description('create queues')]
+class CreateQueue extends Command
+{
+    /**
+     * Execute the console command.
+     */
+    public function handle()
+    {
+        //
+        $this->info("queues create start");
+        $created = app(RabbitMQService::class)->createQueue();
+        $total = count($created);
+        $this->info("[{$total}] queues created");
+        foreach ($created as $key => $queue) {
+            $this->line($queue);
+        }
+    }
+}

+ 8 - 0
api-v13/app/Console/Commands/PostInstall.php

@@ -28,8 +28,16 @@ class PostInstall extends Command
         //
         $this->info('post install start');
 
+        #openSearch
         $this->call('create:opensearch.index');
+
+        #rabbitmq
+        $this->call('app:create-queue');
+
+        # system channel
         $this->call('init:system.channel');
+
+        # system dictionary
         $this->call('init:system.dict');
 
         $this->info('post install done');

+ 1 - 1
api-v13/app/Http/Api/Mq.php

@@ -45,7 +45,7 @@ class Mq
             }
             $connection = new AMQPStreamConnection($host, $port, $user, $password, $vhost);
             $channel = $connection->channel();
-            $channel->queue_declare($queue, false, true, false, false);
+            //$channel->queue_declare($queue, false, true, false, false);
 
             $msgId = Str::uuid();
             Log::info("mq push message queue={$queue} id={$msgId}");

+ 44 - 1
api-v13/app/Services/RabbitMQService.php

@@ -14,11 +14,13 @@ class RabbitMQService
 {
     private $connection;
     private $channel;
-    private $config;
+    private array $config;
+    private array $queues;
 
     public function __construct()
     {
         $this->config = config('queue.connections.rabbitmq');
+        $this->queues = config('mint.rabbitmq.queues');
         $this->connect();
     }
 
@@ -44,6 +46,47 @@ class RabbitMQService
         return $this->channel;
     }
 
+    public function createQueue(): array
+    {
+        foreach ($this->queues as $name => $queue) {
+
+            $args = [];
+
+            // TTL
+            if (!empty($queue['ttl'])) {
+                $args['x-message-ttl'] = (int) $queue['ttl'];
+            }
+
+            // max length
+            if (!empty($queue['max_length'])) {
+                $args['x-max-length'] = (int) $queue['max_length'];
+            }
+
+            // dead letter exchange
+            if (!empty($queue['dead_letter_exchange'])) {
+                $args['x-dead-letter-exchange'] = $queue['dead_letter_exchange'];
+            }
+
+            // dead letter routing key(可选但建议)
+            if (!empty($queue['dead_letter_queue'])) {
+                $args['x-dead-letter-routing-key'] = $queue['dead_letter_queue'];
+            }
+
+            $this->channel->queue_declare(
+                $name,                 // queue 名称
+                false,                 // passive:检查是否存在(false=不存在则创建)
+                true,                  // durable:是否持久化(重启 RabbitMQ 后仍存在)
+                false,                 // exclusive:独占模式
+                false,                 // auto_delete:是否在无消费者时自动删除
+                false,                 // nowait:是否不等待服务器响应(false=阻塞等待确认)
+                new AMQPTable($args)   // arguments:附加参数(TTL / DLX / max-length 等)
+            );
+        }
+
+        return array_keys($this->queues);
+    }
+
+
     public function setupQueue(string $queueName): void
     {
         $queueConfig = config("mint.rabbitmq.queues.{$queueName}");

+ 8 - 0
api-v13/config/mint.php

@@ -133,6 +133,14 @@ return [
     ],
     'rabbitmq' => [
         'queues' => [
+            'task' => ['retry_times' => env('RABBITMQ_AI_RETRY_TIMES', 3)],
+            'discussion' => ['retry_times' => env('RABBITMQ_AI_RETRY_TIMES', 3)],
+            'export_pali_chapter' => ['retry_times' => env('RABBITMQ_AI_RETRY_TIMES', 3)],
+            'export_article' => ['retry_times' => env('RABBITMQ_AI_RETRY_TIMES', 3)],
+            'progress' => ['retry_times' => env('RABBITMQ_AI_RETRY_TIMES', 3)],
+            'content' => ['retry_times' => env('RABBITMQ_AI_RETRY_TIMES', 3)],
+            'suggestion' => ['retry_times' => env('RABBITMQ_AI_RETRY_TIMES', 3)],
+            'wbw-analyses' => ['retry_times' => env('RABBITMQ_AI_RETRY_TIMES', 3)],
             'ai_translate_v2' => [
                 'retry_times' => env('RABBITMQ_AI_RETRY_TIMES', 3),
                 'max_loop_count' => env('RABBITMQ_AI_MAX_LOOP', 10),