Browse Source

Merge pull request #1639 from visuddhinanda/laravel

mq discussion 添加log
visuddhinanda 2 years ago
parent
commit
608abf8922

+ 5 - 3
app/Console/Commands/ExportChapter.php

@@ -139,9 +139,11 @@ class ExportChapter extends Command
             }
         }
         $tex = array();
-        $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,'delimiters' => '[[ ]]','escape'=>function ($value){
-            return $value;
-        }));
+        $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
+                                        'delimiters' => '[[ ]]',
+                                        'escape'=>function ($value){
+                                            return $value;
+                                        }));
         $tpl = file_get_contents(resource_path("mustache/tex/main.tex"));
         $texContent = $m->render($tpl,$bookMeta);
         $tex[] = ['name'=>'main.tex',

+ 7 - 1
app/Console/Commands/MqDiscussion.php

@@ -50,6 +50,7 @@ class MqDiscussion extends Command
         $queue = 'discussion';
         $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
         Mq::worker($exchange,$queue,function ($message){
+            Log::info('mq discussion start {message}',['message'=>json_encode($message,JSON_UNESCAPED_UNICODE)]);
             $result = 0;
             switch ($message->res_type) {
                 case 'sentence':
@@ -91,7 +92,12 @@ class MqDiscussion extends Command
                     $tpl = Article::where('owner',$rootId)
                                   ->where('title',$articleTitle)
                                   ->value('content');
-                    $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES));
+                    if(empty($tpl)){
+                        Log::error('模版不能为空',['tpl_title'=>$articleTitle]);
+                        return 1;
+                    }
+                    $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
+                                                'delimiters' => '{% %}',));
                     $msgContent = $m->render($tpl,$msgParam);
 
                     $webhooks = WebHook::where('res_id',$sentence->channel_uid)

+ 3 - 0
app/Console/Commands/TestMdRender.php

@@ -5,6 +5,7 @@ namespace App\Console\Commands;
 use Illuminate\Console\Command;
 use App\Http\Api\MdRender;
 use Illuminate\Support\Str;
+use Illuminate\Support\Facades\Log;
 
 class TestMdRender extends Command
 {
@@ -39,6 +40,8 @@ class TestMdRender extends Command
      */
     public function handle()
     {
+        Log::info('md render start item='.$this->argument('item'));
+
         $data = array();
         $data['bold'] = <<<md
         **三十位** 经

+ 9 - 10
app/Console/Commands/TestMq.php

@@ -5,6 +5,9 @@ namespace App\Console\Commands;
 use Illuminate\Console\Command;
 
 use App\Http\Api\Mq;
+use App\Models\Discussion;
+use App\Http\Resources\DiscussionResource;
+use Illuminate\Support\Str;
 
 class TestMq extends Command
 {
@@ -13,7 +16,7 @@ class TestMq extends Command
      *
      * @var string
      */
-    protected $signature = 'test:mq';
+    protected $signature = 'test:mq {--discussion=}';
 
     /**
      * The console command description.
@@ -39,17 +42,13 @@ class TestMq extends Command
      */
     public function handle()
     {
-        //一对一
-		//Mq::publish('wbw-analyses',[13607982709477376]);
+
 		Mq::publish('hello',['hello world']);
+        $discussion = $this->option('discussion');
+        if($discussion && Str::isUuid($discussion)){
+            Mq::publish('discussion',new DiscussionResource(Discussion::find($discussion)));
+        }
 
-        //一对多
-        /*
-        $connection = new AMQPStreamConnection(MQ_HOST, MQ_PORT, MQ_USERNAME, MQ_PASSWORD);
-        $channel->exchange_declare('hello_exchange','fanout',false,true);
-        $channel->queue_declare('hello', false, true, false, false);
-        $channel->exchange_bind('hello','exchange',"");
-*/
         return 0;
     }
 }

+ 17 - 6
app/Http/Api/MdRender.php

@@ -290,17 +290,28 @@ class MdRender{
                 }
                 break;
             case 'text':
-                if(is_array($tplProps)){
-                    return '';
+                if(isset($tplProps)){
+                    if(is_array($tplProps)){
+                        return '';
+                    }else{
+                        return $tplProps;
+                    }
                 }else{
-                    return $tplProps;
+                    Log::error('tplProps undefine');
+                    return '';
                 }
+
                 break;
             case 'tex':
-                if(is_array($tplProps)){
-                    return '';
+                if(isset($tplProps)){
+                    if(is_array($tplProps)){
+                        return '';
+                    }else{
+                        return $tplProps;
+                    }
                 }else{
-                    return $tplProps;
+                    Log::error('tplProps undefine');
+                    return '';
                 }
                 break;
             default:

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

@@ -24,6 +24,7 @@ class Mq{
                 //一对一
 
         try{
+            Log::debug('mq start {channel} {message}',['channel'=>$channelName,'message'=>$message]);
             $host = config("queue.connections.rabbitmq.host");
             $port = config("queue.connections.rabbitmq.port");
             $user = config("queue.connections.rabbitmq.user");
@@ -114,6 +115,7 @@ class Mq{
                     }
                 }catch(\Exception $e){
                     // push to issues
+                    Log::error('mq worker exception',$e);
                     $channelName = 'issues';
                     $channelIssues = $connection->channel();
                     $channelIssues->queue_declare($channelName, false, true, false, false);

+ 3 - 1
app/Http/Controllers/DiscussionController.php

@@ -12,6 +12,7 @@ use App\Http\Resources\DiscussionResource;
 use App\Http\Api\MdRender;
 use App\Http\Api\AuthApi;
 use App\Http\Api\Mq;
+use Illuminate\Support\Facades\Log;
 
 class DiscussionController extends Controller
 {
@@ -133,7 +134,8 @@ class DiscussionController extends Controller
     {
         $user = AuthApi::current($request);
         if(!$user){
-            return $this->error(__('auth.failed'));
+            Log::error('discussion store auth failed {request}',['request'=>$request]);
+            return $this->error(__('auth.failed'),[401],401);
         }
         //
         // validate

+ 31 - 0
tests/Feature/MqDiscussionTest.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+
+class MqDiscussionTest extends TestCase
+{
+    protected $token = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJuYmYiOjE2NjgyMzE3MTksImV4cCI6MTY5OTc2NzcxOSwidWlkIjoiYmE1NDYzZjMtNzJkMS00NDEwLTg1OGUtZWFkZDEwODg0NzEzIiwiaWQiOiI0In0.LV4ItC5VCqXpbKIXT1zePcnfi-heCf3Df63w7qbXsT1i5KJtwJJC938CLgANjqwcQFa3lrR5TqvT1kkqD-Mmgg';
+
+    /**
+     * A basic feature test example.
+     *
+     * @return void
+     */
+    public function test_store()
+    {
+        $response = $this->withHeaders([
+            'Authorization' => $this->token,
+        ])->json('POST', '/api/v2/discussion',
+                    [
+                        'title'=>'test'.time(),
+                        'res_id'=>'1aebd3c0-61cc-4e5b-a56e-b98ffb7f1430',
+                        'res_type'=>'sentence',
+                    ]);
+
+        $response->assertOk();
+    }
+}