Browse Source

将波次的任务作为队列Job执行

LD 5 years ago
parent
commit
06ef86ef56

+ 2 - 1
app/Console/Commands/SyncBatchTask.php

@@ -2,6 +2,7 @@
 
 namespace App\Console\Commands;
 
+use App\Jobs\BatchTaskJob;
 use App\Order;
 use App\OrderBin;
 use App\Services\BatchService;
@@ -256,7 +257,7 @@ sql;
                 foreach ($batches as $batch){
                     app("OrderService")->update(["code"=>$map[$batch->code]],["batch_id"=>$batch->id]);
                 }
-                $this->batchService->assignTasks($batches);    //在这里为波次注册任务!
+                BatchTaskJob::dispatch($batches);    //在这里为波次注册队列任务!
             }
         }
         ValueStore::query()->where("name","wave_detail_last_sync_date")->update(["value"=>$details[count($details)-1]->edittime]);

+ 41 - 0
app/Jobs/BatchTaskJob.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Services\BatchService;
+use App\Traits\TestableInstant;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class BatchTaskJob implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+    use TestableInstant;
+
+    /** @var $batchService BatchService */
+    protected $batchService;
+    protected $batches;
+    /**
+     * Create a new job instance.
+     *
+     * @param $batches
+     */
+    public function __construct($batches)
+    {
+        $this->batches=$batches;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $this->instant($this->batchService, 'BatchService');
+        $this->batchService->assignTasks($this->batches);    //在这里为波次注册任务执行!
+    }
+}

+ 1 - 21
app/Traits/ServiceAppAop.php

@@ -12,27 +12,7 @@ use Illuminate\Support\Facades\Cache;
 trait ServiceAppAop
 {
 
-
-    /**
-     * 实例化一个service的入口,为了测试方便做出来的,这样测试时可以代入假的柱件替换service的实现
-     * 应当在一般性用app的地方换成本方法才可以做到使用柱件
-     * @param $targetService
-     * @param $abstractClassName
-     * @return Application|mixed|string
-     */
-    public function instant(&$targetService, $abstractClassName){
-        if(!is_string($targetService)){
-            if(empty($targetService))
-                $targetService=app($abstractClassName);
-            return $targetService;
-        }
-        if(!is_string($abstractClassName)){
-            $this->$targetService=$abstractClassName;
-        }else{
-            $this->$targetService=app($abstractClassName);
-        }
-        return $targetService;
-    }
+    use TestableInstant;
     /**
      * 缓存过了
      * @param array $kvPairs

+ 36 - 0
app/Traits/TestableInstant.php

@@ -0,0 +1,36 @@
+<?php
+
+
+namespace App\Traits;
+
+
+use App\Services\LogService;
+use Illuminate\Contracts\Foundation\Application;
+use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\Cache;
+
+trait TestableInstant
+{
+
+
+    /**
+     * 实例化一个service的入口,为了测试方便做出来的,这样测试时可以代入假的柱件替换service的实现
+     * 应当在一般性用app的地方换成本方法才可以做到使用柱件
+     * @param $targetService
+     * @param $abstractClassName
+     * @return Application|mixed|string
+     */
+    public function instant(&$targetService, $abstractClassName){
+        if(!is_string($targetService)){
+            if(empty($targetService))
+                $targetService=app($abstractClassName);
+            return $targetService;
+        }
+        if(!is_string($abstractClassName)){
+            $this->$targetService=$abstractClassName;
+        }else{
+            $this->$targetService=app($abstractClassName);
+        }
+        return $targetService;
+    }
+}