作者 lyh

变更数据

@@ -10,7 +10,9 @@ @@ -10,7 +10,9 @@
10 namespace App\Console\Commands\SyncFilesImage; 10 namespace App\Console\Commands\SyncFilesImage;
11 11
12 use App\Models\File\ErrorFile; 12 use App\Models\File\ErrorFile;
  13 +use App\Models\Geo\GeoQuestion;
13 use Illuminate\Console\Command; 14 use Illuminate\Console\Command;
  15 +use Illuminate\Support\Facades\Redis;
14 16
15 class SyncFile extends Command 17 class SyncFile extends Command
16 { 18 {
@@ -30,21 +32,47 @@ class SyncFile extends Command @@ -30,21 +32,47 @@ class SyncFile extends Command
30 32
31 33
32 public function handle(){ 34 public function handle(){
  35 + while (true){
  36 + $task_id = $this->getTaskId();
  37 + if(empty($task_id)){
  38 + sleep(100);
  39 + continue;
  40 + }
33 $errorFileModel = new ErrorFile(); 41 $errorFileModel = new ErrorFile();
34 - $lists = $errorFileModel->list(['status'=>0],'id',['id','path'],'asc',1000);//未同步成功的图片及文件  
35 - foreach ($lists as $k => $v){  
36 - if(strpos($v['path'], '/181/') !== false ){  
37 - $code = $this->synchronizationFiles($v['path']); 42 + $info = $errorFileModel->read(['id'=>$task_id],['id','path']);
  43 + if(strpos($info['path'], '/181/') !== false ){
  44 + $code = $this->synchronizationFiles($info['path']);
38 }else{ 45 }else{
39 - $code = $this->synchronizationFile($v['path']); 46 + $code = $this->synchronizationFile($info['path']);
40 } 47 }
41 if(200 == (int)$code){ 48 if(200 == (int)$code){
42 - echo date('Y-m-d H:i:s') . '编辑的path为:'. $v['path'] .',主键id:'. $v['id'] . PHP_EOL; 49 + echo date('Y-m-d H:i:s') . '编辑的path为:'. $info['path'] .',主键id:'. $info['id'] . PHP_EOL;
  50 + $errorFileModel->edit(['status'=>2],['id'=>$info['id']]);
  51 + }
  52 + echo date('Y-m-d H:i:s') . '编辑的end为:' . PHP_EOL;
  53 + }
  54 + }
  55 +
  56 + public function getTaskId(){
  57 + $key = 'sync_file_task';
  58 + $task_id = Redis::rpop($key);
  59 + if(empty($task_id)){
  60 + $lock_key = 'sync_file_lock';
  61 + $lock_ttl = 60; // 锁时间大于当前 锁功能执行时间
  62 + // 尝试获取锁,非阻塞方式
  63 + $lock = Redis::set($lock_key, 1, 'EX', $lock_ttl, 'NX');
  64 + if (empty($lock)){
  65 + return $task_id;
  66 + }
  67 + $errorFileModel = new ErrorFile();
  68 + $lists = $errorFileModel->list(['status'=>0],'id',['id','path'],'asc',1000);//未同步成功的图片及文件
  69 + foreach ($lists as $v){
  70 + Redis::lpush($key, $v['id']);
43 $errorFileModel->edit(['status'=>1],['id'=>$v['id']]); 71 $errorFileModel->edit(['status'=>1],['id'=>$v['id']]);
44 } 72 }
  73 + $task_id = Redis::rpop($key);
45 } 74 }
46 - echo date('Y-m-d H:i:s') . '编辑的end为:' . PHP_EOL;  
47 - return true; 75 + return $task_id;
48 } 76 }
49 77
50 /** 78 /**