作者 赵彬吉

update

  1 +<?php
  2 +
  3 +namespace App\Console\Commands;
  4 +
  5 +use App\Http\Logic\Aside\Project\ProjectLogic;
  6 +use App\Models\Com\NoticeLog;
  7 +use App\Models\Project\Project;
  8 +use App\Services\ProjectServer;
  9 +use Illuminate\Console\Command;
  10 +use Illuminate\Support\Facades\DB;
  11 +
  12 +/**
  13 + * 初始化项目
  14 + * Class InitProject
  15 + * @package App\Console\Commands
  16 + * @author zbj
  17 + * @date 2023/10/8
  18 + */
  19 +class InitProject extends Command
  20 +{
  21 + /**
  22 + * The name and signature of the console command.
  23 + *
  24 + * @var string
  25 + */
  26 + protected $signature = 'init_project';
  27 +
  28 + /**
  29 + * The console command description.
  30 + *
  31 + * @var string
  32 + */
  33 + protected $description = '初始化项目';
  34 +
  35 + /**
  36 + * Create a new command instance.
  37 + *
  38 + * @return void
  39 + */
  40 + public function __construct()
  41 + {
  42 + parent::__construct();
  43 + }
  44 +
  45 + /**
  46 + * @return bool
  47 + */
  48 + public function handle()
  49 + {
  50 + while (true){
  51 + $list = NoticeLog::where('type', NoticeLog::TYPE_INIT_PROJECT)->where('status', NoticeLog::STATUS_PENDING)->get();
  52 + foreach ($list as $item){
  53 + echo 'start:' . $item['id'] . PHP_EOL;
  54 + try {
  55 + $project = Project::find($item['data']['project_id']);
  56 +
  57 + $project_logic = new ProjectLogic();
  58 + //初始化数据库
  59 + if(!empty($project['mysql_id'])){
  60 + $project_logic->initializationMysql($project['id']);
  61 + }
  62 + //初始账号
  63 + if(!empty($project['mobile'])){
  64 + $project_logic->createUser($project['mobile'],$project['id'],$project['lead_name']);
  65 + }
  66 + //更改服务器状态
  67 + if(!empty($project['serve_id'])){
  68 + $project_logic->updateServe($project['serve_id']);
  69 + }
  70 +
  71 + $item->status = NoticeLog::STATUS_SUCCESS;
  72 + $item->save();
  73 + echo 'success:' . $item['id'] . PHP_EOL;
  74 + }catch (\Exception $e){
  75 + echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL;
  76 + errorLog('项目初始化失败', $item, $e);
  77 + $this->retry($item, $e->getMessage());
  78 + }
  79 + }
  80 + sleep(2);
  81 + }
  82 + }
  83 +
  84 + /**
  85 + * @param NoticeLog $log
  86 + */
  87 + public function retry($log, $remark){
  88 + if($log->retry >= 3){
  89 + $log->status = NoticeLog::STATUS_FAIL;
  90 + $log->remark = mb_substr($remark, 0, 250);
  91 + }else{
  92 + $log->retry = $log->retry + 1;
  93 + }
  94 + $log->save();
  95 + }
  96 +}
@@ -54,14 +54,20 @@ class RankDataTask extends Command @@ -54,14 +54,20 @@ class RankDataTask extends Command
54 { 54 {
55 $list = NoticeLog::where('type', NoticeLog::TYPE_RANK_DATA)->where('status', NoticeLog::STATUS_PENDING)->get(); 55 $list = NoticeLog::where('type', NoticeLog::TYPE_RANK_DATA)->where('status', NoticeLog::STATUS_PENDING)->get();
56 foreach ($list as $item){ 56 foreach ($list as $item){
  57 + echo 'start:' . $item['id'] . PHP_EOL;
57 try { 58 try {
58 - (new RankDataLogic())->syncRankData($item['data']['api_no']); 59 + $api = new QuanqiusouApi();
  60 + $site_res = $api->getSiteRes();
  61 +
  62 + (new RankDataLogic())->syncRankData($item['data']['api_no'], $site_res);
59 63
60 $item->status = NoticeLog::STATUS_SUCCESS; 64 $item->status = NoticeLog::STATUS_SUCCESS;
61 $item->save(); 65 $item->save();
  66 + echo 'success:' . $item['id'] . PHP_EOL;
62 }catch (\Exception $e){ 67 }catch (\Exception $e){
  68 + echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL;
63 errorLog('排名数据更新失败', $item, $e); 69 errorLog('排名数据更新失败', $item, $e);
64 - $this->retry($item); 70 + $this->retry($item, $e->getMessage());
65 } 71 }
66 } 72 }
67 } 73 }
@@ -69,9 +75,10 @@ class RankDataTask extends Command @@ -69,9 +75,10 @@ class RankDataTask extends Command
69 /** 75 /**
70 * @param NoticeLog $log 76 * @param NoticeLog $log
71 */ 77 */
72 - public function retry($log){ 78 + public function retry($log, $remark){
73 if($log->retry >= 3){ 79 if($log->retry >= 3){
74 $log->status = NoticeLog::STATUS_FAIL; 80 $log->status = NoticeLog::STATUS_FAIL;
  81 + $log->remark = mb_substr($remark, 0, 250);
75 }else{ 82 }else{
76 $log->retry = $log->retry + 1; 83 $log->retry = $log->retry + 1;
77 } 84 }
@@ -76,7 +76,7 @@ class SyncProject extends Command @@ -76,7 +76,7 @@ class SyncProject extends Command
76 } 76 }
77 if(!$data || empty($data['data'])){ 77 if(!$data || empty($data['data'])){
78 LogUtils::error('OaGlobalsoApi order_info error', $data); 78 LogUtils::error('OaGlobalsoApi order_info error', $data);
79 - $this->retry($item); 79 + $this->retry($item, '未获取到订单信息');
80 } 80 }
81 if($data['data']['order_type'] == '首次'){ 81 if($data['data']['order_type'] == '首次'){
82 $this->sync($data['data'],$is_update); 82 $this->sync($data['data'],$is_update);
@@ -99,7 +99,7 @@ class SyncProject extends Command @@ -99,7 +99,7 @@ class SyncProject extends Command
99 }catch (\Exception $e){ 99 }catch (\Exception $e){
100 echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL; 100 echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL;
101 errorLog('项目同步失败', $item, $e); 101 errorLog('项目同步失败', $item, $e);
102 - $this->retry($item); 102 + $this->retry($item, $e->getMessage());
103 } 103 }
104 } 104 }
105 sleep(2); 105 sleep(2);
@@ -110,9 +110,10 @@ class SyncProject extends Command @@ -110,9 +110,10 @@ class SyncProject extends Command
110 /** 110 /**
111 * @param NoticeLog $log 111 * @param NoticeLog $log
112 */ 112 */
113 - public function retry($log){ 113 + public function retry($log, $remark){
114 if($log->retry >= 3){ 114 if($log->retry >= 3){
115 $log->status = NoticeLog::STATUS_FAIL; 115 $log->status = NoticeLog::STATUS_FAIL;
  116 + $log->remark = mb_substr($remark, 0, 250);
116 }else{ 117 }else{
117 $log->retry = $log->retry + 1; 118 $log->retry = $log->retry + 1;
118 } 119 }
@@ -248,18 +248,20 @@ class ProjectLogic extends BaseLogic @@ -248,18 +248,20 @@ class ProjectLogic extends BaseLogic
248 } 248 }
249 //创建默认数据库 249 //创建默认数据库
250 if($param['type'] == Project::TYPE_ONE){ 250 if($param['type'] == Project::TYPE_ONE){
251 - //初始化数据库  
252 - if(isset($param['mysql_id']) && !empty($param['mysql_id'])){  
253 - $this->initializationMysql($param['id']);  
254 - }  
255 - //初始账号  
256 - if(isset($param['mobile']) && !empty($param['mobile'])){  
257 - $this->createUser($param['mobile'],$param['id'],$param['lead_name']);  
258 - }  
259 - //更改服务器状态  
260 - if(isset($param['serve_id']) && !empty($param['serve_id'])){  
261 - $this->updateServe($param['serve_id']);  
262 - } 251 + //改为异步
  252 + NoticeLog::createLog(NoticeLog::TYPE_INIT_PROJECT, ['project_id' => $param['id']]);
  253 +// //初始化数据库
  254 +// if(isset($param['mysql_id']) && !empty($param['mysql_id'])){
  255 +// $this->initializationMysql($param['id']);
  256 +// }
  257 +// //初始账号
  258 +// if(isset($param['mobile']) && !empty($param['mobile'])){
  259 +// $this->createUser($param['mobile'],$param['id'],$param['lead_name']);
  260 +// }
  261 +// //更改服务器状态
  262 +// if(isset($param['serve_id']) && !empty($param['serve_id'])){
  263 +// $this->updateServe($param['serve_id']);
  264 +// }
263 } 265 }
264 return $this->success(); 266 return $this->success();
265 } 267 }
@@ -11,6 +11,7 @@ class NoticeLog extends Model @@ -11,6 +11,7 @@ class NoticeLog extends Model
11 11
12 const TYPE_PROJECT = 'project'; 12 const TYPE_PROJECT = 'project';
13 const TYPE_RANK_DATA = 'rank_data'; 13 const TYPE_RANK_DATA = 'rank_data';
  14 + const TYPE_INIT_PROJECT = 'init_project';
14 15
15 const STATUS_PENDING = 0; 16 const STATUS_PENDING = 0;
16 const STATUS_SUCCESS = 1; 17 const STATUS_SUCCESS = 1;
@@ -78,6 +78,8 @@ class Project extends Base @@ -78,6 +78,8 @@ class Project extends Base
78 7 => '定制建站(PS订制)', 78 7 => '定制建站(PS订制)',
79 8 => '星链网站(1年版)', 79 8 => '星链网站(1年版)',
80 9 => '星链网站(2年版)', 80 9 => '星链网站(2年版)',
  81 + 11 => '俄语标准版',
  82 + 12 => '俄语商务版',
81 ]; 83 ];
82 } 84 }
83 85
@@ -37,6 +37,9 @@ class ProjectServer extends BaseService @@ -37,6 +37,9 @@ class ProjectServer extends BaseService
37 config(['database.connections.custom_mysql.database' => $project->databaseName()]); 37 config(['database.connections.custom_mysql.database' => $project->databaseName()]);
38 config(['database.connections.custom_mysql.username' => $project->mysqlConfig->user]); 38 config(['database.connections.custom_mysql.username' => $project->mysqlConfig->user]);
39 config(['database.connections.custom_mysql.password' => $project->mysqlConfig->password]); 39 config(['database.connections.custom_mysql.password' => $project->mysqlConfig->password]);
  40 + //重连
  41 + DB::connection('custom_mysql')->reconnect();
  42 +
40 // 设置 redis 配置 43 // 设置 redis 配置
41 return $project; 44 return $project;
42 } 45 }