visuddhinanda 9 months ago
parent
commit
5e7ff60dee
2 changed files with 59 additions and 0 deletions
  1. 14 0
      api-v8/app/Exceptions/SectionTimeoutException.php
  2. 45 0
      api-v8/app/Jobs/AiTranslate.php

+ 14 - 0
api-v8/app/Exceptions/SectionTimeoutException.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Exceptions;
+
+use Exception;
+
+class SectionTimeoutException extends Exception
+{
+    //
+    public function __construct()
+    {
+        parent::__construct('section timeout');
+    }
+}

+ 45 - 0
api-v8/app/Jobs/AiTranslate.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Jobs;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeUnique;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use App\Services\AiTranslateService;
+
+class AiTranslate implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    public $tries = 3;       // 最大尝试次数(重试次数 = 3 - 1)
+    public $timeout = 60;    // 超时时间,单位:秒
+    private $aiService;
+    private $data;
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct($data)
+    {
+        //
+        $this->data = $data;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        //
+        echo 'ai worker start';
+        $data = json_decode(json_encode($this->data['payload']));
+        $this->aiService = app(AiTranslateService::class);
+        return $this->aiService->processTranslate($this->data['message_id'], $data, null);
+    }
+}