visuddhinanda 2 anni fa
parent
commit
ca8c4a06b6
2 ha cambiato i file con 87 aggiunte e 0 eliminazioni
  1. 69 0
      app/Console/Commands/MqProgress.php
  2. 18 0
      app/Http/Api/Mq.php

+ 69 - 0
app/Console/Commands/MqProgress.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use PhpAmqpLib\Connection\AMQPStreamConnection;
+
+class MqProgress extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'mq:progress';
+
+    /**
+     * 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()
+    {
+		$connection = new AMQPStreamConnection(env("MQ_HOST"), env("MQ_PORT"), env("MQ_USERNAME"), env("MQ_PASSWORD"));
+		$channel = $connection->channel();
+
+		$channel->queue_declare('progress', false, true, false, false);
+
+		$this->info(" [*] Waiting for messages. To exit press CTRL+C");
+
+		$callback = function ($msg) {
+            $message = json_decode($msg->body);
+
+            $ok = $this->call('upgrade:progress',['--book'=>$message->book,
+                                            '--para'=>$message->para,
+                                            '--channel'=>$message->channel,
+                                            ]);
+            $ok2 = $this->call('upgrade:progress.chapter',['--book'=>$message->book,
+                                                '--para'=>$message->para,
+                                                '--channel'=>$message->channel,
+                                                ]);
+            $this->info("Received book=".$message->book.' progress='.$ok.' chapter='.$ok2);
+		};
+
+		$channel->basic_consume('progress', '', false, true, false, false, $callback);
+
+		while ($channel->is_open()) {
+			  $channel->wait();
+		  }
+        return 0;
+    }
+}

+ 18 - 0
app/Http/Api/Mq.php

@@ -0,0 +1,18 @@
+<?php
+namespace App\Http\Api;
+use PhpAmqpLib\Connection\AMQPStreamConnection;
+use PhpAmqpLib\Message\AMQPMessage;
+class Mq{
+    public static function send(string $channelName, $message){
+                //一对一
+		$connection = new AMQPStreamConnection(env("MQ_HOST"), env("MQ_PORT"), env("MQ_USERNAME"), env("MQ_PASSWORD"));
+		$channel = $connection->channel();
+		$channel->queue_declare($channelName, false, true, false, false);
+
+		$msg = new AMQPMessage(json_encode($message,JSON_UNESCAPED_UNICODE));
+		$channel->basic_publish($msg, '', $channelName);
+
+		$channel->close();
+		$connection->close();
+    }
+}