visuddhinanda 11 miesięcy temu
rodzic
commit
1ffad5f993

+ 61 - 0
api-v8/app/Console/Commands/MqEmpty.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use App\Http\Api\Mq;
+
+class MqEmpty extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'mq:empty';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        if (\App\Tools\Tools::isStop()) {
+            return 0;
+        }
+        $exchange = 'router';
+        $queue = 'ai_translate';
+        $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
+        Log::debug("mq worker {$queue} start.");
+        Mq::worker(
+            $exchange,
+            $queue,
+            function ($message) {
+                $this->info('new message');
+                sleep(3);
+                return 0;
+            }
+        );
+
+        return 0;
+    }
+}

+ 46 - 0
api-v8/app/Console/Commands/TestMqExit.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Http\Api\Mq;
+
+class TestMqExit extends Command
+{
+    /**
+     * The name and signature of the console command.
+     * php artisan test:mq.exit
+     * @var string
+     */
+    protected $signature = 'test:mq.exit';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        for ($i = 0; $i < 10; $i++) {
+            Mq::publish('ai_translate', ['hello world']);
+        }
+        return 0;
+    }
+}