浏览代码

asn同步,commodity同步添加任务起始时间

haozi 5 年之前
父节点
当前提交
f9025046f6

+ 5 - 5
app/Console/Commands/SyncWmsCommoditiesInformation.php

@@ -12,16 +12,16 @@ use Illuminate\Support\Facades\Cache;
 class SyncWmsCommoditiesInformation extends Command
 {
 
-    private $task_start_at;
-    private $task_end_at;
-    private $restart;
-    private $is_enabled;
     /**
      * The name and signature of the console command.
      *
      * @var string
      */
     protected $signature = 'SyncWmsCommoditiesInformation';
+    private $task_start_at;
+    private $task_end_at;
+    private $restart;
+    private $is_enabled;
 
     /**
      * The console command description.
@@ -53,7 +53,7 @@ class SyncWmsCommoditiesInformation extends Command
     public function handle()
     {
         $this->init();
-        if($this->is_enabled=='false')return;
+        if($this->is_enabled==false)return;
         sleep(rand(2,3));
         $start_time =  Cache::remember($this->task_start_at,null,function (){
             return ValueStore::query()->firstOrCreate(['name'=>$this->task_start_at])->first()->value;

+ 41 - 3
app/Console/Commands/WasSyncWmsAsnInformation.php

@@ -4,7 +4,10 @@ namespace App\Console\Commands;
 
 
 use App\Services\StoreService;
+use App\ValueStore;
 use Illuminate\Console\Command;
+use Illuminate\Support\Carbon;
+use Illuminate\Support\Facades\Cache;
 
 class WasSyncWmsAsnInformation extends Command
 {
@@ -15,7 +18,10 @@ class WasSyncWmsAsnInformation extends Command
      * @var string
      */
     protected $signature = 'WasSyncWmsAsnInformation';
-
+    private $task_start_at;
+    private $task_end_at;
+    private $restart;
+    private $is_enabled;
     /**
      * The console command description.
      *
@@ -33,14 +39,46 @@ class WasSyncWmsAsnInformation extends Command
         parent::__construct();
     }
 
+    public function init()
+    {
+        $this->task_start_at = config('sync.asn_sync.task_start_at');
+        $this->task_end_at = config('sync.asn_sync.task_end_at');
+        $this->restart = config('sync.asn_sync.restart');
+        $this->is_enabled= config('sync.asn_sync.enabled');
+    }
     /**
      * Execute the console command.
-     *
-     * @return int
+     * @return void
      */
     public function handle()
     {
+        $this->init();
+        if($this->is_enabled==false)return;
+        sleep(rand(2,3));
+        $start_time =  Cache::remember($this->task_start_at,null,function (){
+            return ValueStore::query()->firstOrCreate(['name'=>$this->task_start_at])->first()->value;
+        });
+        $end_time =  Cache::remember($this->task_end_at,null,function (){
+            return  ValueStore::query()->firstOrCreate(['name'=>$this->task_end_at])->first()->value;
+        });
+
+        $start = Carbon::now();
+        // 判断上一次任务异常了
+        //1.第一次就异常了
+        if(isset($start_time) && empty($end_time) && $start->diffInMinutes(Carbon::parse($start_time)) < $this->restart)return;
+        //2.中间某次异常了
+        if(isset($start_time) && isset($end_time)
+            && Carbon::parse($end_time)->lt(Carbon::parse($start_time))
+            && $start->diffInMinutes(Carbon::parse($start_time)) < $this->restart)
+            return;
+
+        $start = (string)$start;
+        Cache::put($this->task_start_at,$start);
+        ValueStore::query()->where('name',$this->task_start_at)->update(['value'=>$start]);
         $this->WasSyncWmsAsn();
+        $end = (string)Carbon::now();
+        Cache::put($this->task_end_at,$end);
+        ValueStore::query()->where('name',$this->task_end_at)->update(['value'=>$end]);
     }
 
     public function WasSyncWmsAsn(){

+ 3 - 4
app/Http/Controllers/TestController.php

@@ -11,6 +11,7 @@ use App\CommodityBarcode;
 use App\Console\Commands\CreateOwnerAreaReport;
 use App\Console\Commands\CreateOwnerBillReport;
 use App\Console\Commands\CreateOwnerReport;
+use App\Console\Commands\SyncWmsCommoditiesInformation;
 use App\Console\Commands\SyncWMSOrderTask;
 use App\Events\CancelOrder;
 use App\Http\Requests\TestAaRequest;
@@ -1234,10 +1235,8 @@ where (commodities.owner_id,commodity_barcodes.code) in (select commodities.owne
     public function testSyncCommodity()
     {
         dump('start' . (string)Carbon::now());
-        /**@var  CommodityService $commodityService */
-        $commodityService = app(CommodityService::class);
-        $commodityService->syncCommodityCreated();
-        $commodityService->syncCommodityUpdated();
+        $syncCommodityTask=new SyncWmsCommoditiesInformation();
+        $syncCommodityTask->handle();
         dump('end' . (string)Carbon::now());
     }
 

+ 5 - 0
config/sync.php

@@ -5,6 +5,11 @@ return [
         'start_at'=> '',    // 开始同步WMS的时间  时间格式: yyyy-MM-dd HH:mm:ss 如 2020-09-05 13:15:16
     ],
     'asn_sync' => [
+        'enabled' => true,
+        'restart' => 10,  //重新启动时间 分钟为单位
+        'task_start_at' => 'last_asn_sync_task_start_at',
+        'task_end_at' => 'last_asn_sync_task_end_at',
+        'task_err_at' => 'last_asn_sync_task_error_at',
         'created_at'=>'asn_last_created_sync_at',
         'updated_at'=>'asn_last_updated_sync_at',
         'cache_prefix'=>[