作者 刘锟

项目升级

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Update;
  4 +
  5 +use App\Models\Com\UpdateLog;
  6 +use App\Models\Product\Keyword;
  7 +use App\Models\RouteMap\RouteMap;
  8 +use App\Services\ProjectServer;
  9 +use Illuminate\Console\Command;
  10 +use Illuminate\Support\Facades\DB;
  11 +use Illuminate\Support\Facades\Redis;
  12 +
  13 +/**
  14 + * 4.0,5.0升级到6.0,内容同步
  15 + * Class ProjectImport
  16 + * @package App\Console\Commands
  17 + * @author Akun
  18 + * @date 2023/10/9 15:04
  19 + */
  20 +class ProjectUpdate extends Command
  21 +{
  22 + /**
  23 + * The name and signature of the console command.
  24 + *
  25 + * @var string
  26 + */
  27 + protected $signature = 'project_update';
  28 +
  29 + /**
  30 + * The console command description.
  31 + *
  32 + * @var string
  33 + */
  34 + protected $description = '执行项目升级任务';
  35 +
  36 +
  37 + public function handle()
  38 + {
  39 + while (true) {
  40 + $this->start_update();
  41 + }
  42 + }
  43 +
  44 + protected function start_update()
  45 + {
  46 + $task_id = $this->get_task();
  47 + if (!$task_id) {
  48 + sleep(2);
  49 + return true;
  50 + }
  51 +
  52 + $task = UpdateLog::where('id', $task_id)->where('status', UpdateLog::STATUS_UN)->first();
  53 + if (!$task) {
  54 + sleep(2);
  55 + return true;
  56 + }
  57 +
  58 + $project_id = $task->project_id;
  59 + $api_type = $task->api_type;
  60 + $api_url_arr = explode('?', $task->api_url);
  61 + $api_url = $api_url_arr[0];
  62 +
  63 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;
  64 +
  65 + $task->status = UpdateLog::STATUS_ING;//同步中
  66 + $task->save();
  67 +
  68 + //设置数据库
  69 + $project = ProjectServer::useProject($task->project_id);
  70 + if ($project) {
  71 + if ($api_type == 'website_info') {
  72 + $url = $api_url . '?' . http_build_query(['w' => 'website_info']);
  73 + $data = http_get($url, ['charset' => 'UTF-8']);
  74 + if (isset($data['code']) && $data['code'] == 200) {
  75 + $tags = $data['data']['tags'] ?? [];
  76 +
  77 + $model = new Keyword();
  78 + foreach ($tags as $tag) {
  79 + $keyword = $model->read(['title' => $tag], 'id');
  80 + if (!$keyword) {
  81 + $id = $model->addReturnId([
  82 + 'project_id' => $project_id,
  83 + 'title' => $tag
  84 + ]);
  85 + $route = RouteMap::setRoute($tag, RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id);
  86 + $model->edit(['url' => $route], ['id' => $id]);
  87 + }
  88 + }
  89 + }
  90 + }
  91 + }
  92 + //关闭数据库
  93 + DB::disconnect('custom_mysql');
  94 +
  95 + $task->status = UpdateLog::STATUS_COM;//同步完成
  96 + $task->save();
  97 +
  98 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;
  99 +
  100 + sleep(2);
  101 + }
  102 +
  103 + //获取任务
  104 + protected function get_task()
  105 + {
  106 + $key = 'console_update_task';
  107 + $task_id = Redis::rpop($key);
  108 + if ($task_id) {
  109 + return $task_id;
  110 + }
  111 +
  112 + $task_list = UpdateLog::where('status', UpdateLog::STATUS_UN)->limit(20)->get();
  113 + if ($task_list->count() == 0) {
  114 + return false;
  115 + }
  116 +
  117 + foreach ($task_list as $value) {
  118 + Redis::lpush($key, $value->id);
  119 + }
  120 +
  121 + $task_id = Redis::rpop($key);
  122 + return $task_id;
  123 + }
  124 +
  125 +}
@@ -9,9 +9,9 @@ class UpdateLog extends Model @@ -9,9 +9,9 @@ class UpdateLog extends Model
9 //设置关联表名 9 //设置关联表名
10 protected $table = 'gl_update_log'; 10 protected $table = 'gl_update_log';
11 11
12 - const STATUS_PENDING = 0;  
13 - const STATUS_SUCCESS = 1;  
14 - const STATUS_FAIL = 2; 12 + const STATUS_UN = 0;//未开始
  13 + const STATUS_ING = 1;//导入中
  14 + const STATUS_COM = 2;//导入完成
15 15
16 /** 16 /**
17 * 创建更新日志 17 * 创建更新日志