作者 刘锟

import_task

@@ -10,6 +10,7 @@ use App\Models\Mail\Mail; @@ -10,6 +10,7 @@ use App\Models\Mail\Mail;
10 use App\Services\ProjectServer; 10 use App\Services\ProjectServer;
11 use Illuminate\Console\Command; 11 use Illuminate\Console\Command;
12 use Illuminate\Support\Facades\DB; 12 use Illuminate\Support\Facades\DB;
  13 +use Illuminate\Support\Facades\Redis;
13 14
14 /** 15 /**
15 * Class GoogleRank 16 * Class GoogleRank
@@ -43,7 +44,13 @@ class ProjectImport extends Command @@ -43,7 +44,13 @@ class ProjectImport extends Command
43 44
44 protected function start_import() 45 protected function start_import()
45 { 46 {
46 - $task = ImportTask::where('status', ImportTask::STATUS_UN)->first(); 47 + $task_id = $this->get_task();
  48 + if (!$task_id) {
  49 + sleep(2);
  50 + return true;
  51 + }
  52 +
  53 + $task = ImportTask::where('id', $task_id)->where('status', ImportTask::STATUS_UN)->first();
47 if (!$task) { 54 if (!$task) {
48 sleep(2); 55 sleep(2);
49 return true; 56 return true;
@@ -51,6 +58,9 @@ class ProjectImport extends Command @@ -51,6 +58,9 @@ class ProjectImport extends Command
51 58
52 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', import start' . PHP_EOL; 59 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', import start' . PHP_EOL;
53 60
  61 + $task->status = ImportTask::STATUS_ING;//导入中
  62 + $task->save();
  63 +
54 $is_gbk = 0; 64 $is_gbk = 0;
55 $file_code_type = $this->get_code_type($task->file_url); 65 $file_code_type = $this->get_code_type($task->file_url);
56 if ($file_code_type === false) { 66 if ($file_code_type === false) {
@@ -86,8 +96,6 @@ class ProjectImport extends Command @@ -86,8 +96,6 @@ class ProjectImport extends Command
86 $success_count = 0; //成功导入条数 96 $success_count = 0; //成功导入条数
87 $repeat_count = 0; //过滤已存在条数 97 $repeat_count = 0; //过滤已存在条数
88 if (count($line_of_text) > 1) { 98 if (count($line_of_text) > 1) {
89 - $task->status = ImportTask::STATUS_ING;//导入中  
90 - $task->save();  
91 99
92 //设置数据库 100 //设置数据库
93 $project = ProjectServer::useProject($task->project_id); 101 $project = ProjectServer::useProject($task->project_id);
@@ -144,6 +152,28 @@ class ProjectImport extends Command @@ -144,6 +152,28 @@ class ProjectImport extends Command
144 sleep(2); 152 sleep(2);
145 } 153 }
146 154
  155 + //获取任务
  156 + protected function get_task()
  157 + {
  158 + $key = 'console_import_task';
  159 + $task_id = Redis::rpop($key);
  160 + if ($task_id) {
  161 + return $task_id;
  162 + }
  163 +
  164 + $task_list = ImportTask::where('status', ImportTask::STATUS_UN)->limit(20)->get();
  165 + if ($task_list->count() == 0) {
  166 + return false;
  167 + }
  168 +
  169 + foreach ($task_list as $value) {
  170 + Redis::lpush($key, $value->id);
  171 + }
  172 +
  173 + $task_id = Redis::rpop($key);
  174 + return $task_id;
  175 + }
  176 +
147 //判断编码格式 177 //判断编码格式
148 protected function get_code_type($file) 178 protected function get_code_type($file)
149 { 179 {
@@ -158,7 +188,7 @@ class ProjectImport extends Command @@ -158,7 +188,7 @@ class ProjectImport extends Command
158 return false; 188 return false;
159 } 189 }
160 190
161 - //发站内通知 191 + //发站内通知
162 protected function send_mail($user_list, $time, $type, $success_count, $repeat_count, $reason) 192 protected function send_mail($user_list, $time, $type, $success_count, $repeat_count, $reason)
163 { 193 {
164 if ($type == ImportTask::TYPE_NEWS) { 194 if ($type == ImportTask::TYPE_NEWS) {