作者 刘锟

合并分支 'akun' 到 'master'

Akun



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