作者 赵彬吉

update

<?php
namespace App\Console\Commands;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Com\NoticeLog;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/**
* 初始化项目
* Class InitProject
* @package App\Console\Commands
* @author zbj
* @date 2023/10/8
*/
class InitProject extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'init_project';
/**
* The console command description.
*
* @var string
*/
protected $description = '初始化项目';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* @return bool
*/
public function handle()
{
while (true){
$list = NoticeLog::where('type', NoticeLog::TYPE_INIT_PROJECT)->where('status', NoticeLog::STATUS_PENDING)->get();
foreach ($list as $item){
echo 'start:' . $item['id'] . PHP_EOL;
try {
$project = Project::find($item['data']['project_id']);
$project_logic = new ProjectLogic();
//初始化数据库
if(!empty($project['mysql_id'])){
$project_logic->initializationMysql($project['id']);
}
//初始账号
if(!empty($project['mobile'])){
$project_logic->createUser($project['mobile'],$project['id'],$project['lead_name']);
}
//更改服务器状态
if(!empty($project['serve_id'])){
$project_logic->updateServe($project['serve_id']);
}
$item->status = NoticeLog::STATUS_SUCCESS;
$item->save();
echo 'success:' . $item['id'] . PHP_EOL;
}catch (\Exception $e){
echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL;
errorLog('项目初始化失败', $item, $e);
$this->retry($item, $e->getMessage());
}
}
sleep(2);
}
}
/**
* @param NoticeLog $log
*/
public function retry($log, $remark){
if($log->retry >= 3){
$log->status = NoticeLog::STATUS_FAIL;
$log->remark = mb_substr($remark, 0, 250);
}else{
$log->retry = $log->retry + 1;
}
$log->save();
}
}
... ...
... ... @@ -54,14 +54,20 @@ class RankDataTask extends Command
{
$list = NoticeLog::where('type', NoticeLog::TYPE_RANK_DATA)->where('status', NoticeLog::STATUS_PENDING)->get();
foreach ($list as $item){
echo 'start:' . $item['id'] . PHP_EOL;
try {
(new RankDataLogic())->syncRankData($item['data']['api_no']);
$api = new QuanqiusouApi();
$site_res = $api->getSiteRes();
(new RankDataLogic())->syncRankData($item['data']['api_no'], $site_res);
$item->status = NoticeLog::STATUS_SUCCESS;
$item->save();
echo 'success:' . $item['id'] . PHP_EOL;
}catch (\Exception $e){
echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL;
errorLog('排名数据更新失败', $item, $e);
$this->retry($item);
$this->retry($item, $e->getMessage());
}
}
}
... ... @@ -69,9 +75,10 @@ class RankDataTask extends Command
/**
* @param NoticeLog $log
*/
public function retry($log){
public function retry($log, $remark){
if($log->retry >= 3){
$log->status = NoticeLog::STATUS_FAIL;
$log->remark = mb_substr($remark, 0, 250);
}else{
$log->retry = $log->retry + 1;
}
... ...
... ... @@ -76,7 +76,7 @@ class SyncProject extends Command
}
if(!$data || empty($data['data'])){
LogUtils::error('OaGlobalsoApi order_info error', $data);
$this->retry($item);
$this->retry($item, '未获取到订单信息');
}
if($data['data']['order_type'] == '首次'){
$this->sync($data['data'],$is_update);
... ... @@ -99,7 +99,7 @@ class SyncProject extends Command
}catch (\Exception $e){
echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL;
errorLog('项目同步失败', $item, $e);
$this->retry($item);
$this->retry($item, $e->getMessage());
}
}
sleep(2);
... ... @@ -110,9 +110,10 @@ class SyncProject extends Command
/**
* @param NoticeLog $log
*/
public function retry($log){
public function retry($log, $remark){
if($log->retry >= 3){
$log->status = NoticeLog::STATUS_FAIL;
$log->remark = mb_substr($remark, 0, 250);
}else{
$log->retry = $log->retry + 1;
}
... ...
... ... @@ -248,18 +248,20 @@ class ProjectLogic extends BaseLogic
}
//创建默认数据库
if($param['type'] == Project::TYPE_ONE){
//初始化数据库
if(isset($param['mysql_id']) && !empty($param['mysql_id'])){
$this->initializationMysql($param['id']);
}
//初始账号
if(isset($param['mobile']) && !empty($param['mobile'])){
$this->createUser($param['mobile'],$param['id'],$param['lead_name']);
}
//更改服务器状态
if(isset($param['serve_id']) && !empty($param['serve_id'])){
$this->updateServe($param['serve_id']);
}
//改为异步
NoticeLog::createLog(NoticeLog::TYPE_INIT_PROJECT, ['project_id' => $param['id']]);
// //初始化数据库
// if(isset($param['mysql_id']) && !empty($param['mysql_id'])){
// $this->initializationMysql($param['id']);
// }
// //初始账号
// if(isset($param['mobile']) && !empty($param['mobile'])){
// $this->createUser($param['mobile'],$param['id'],$param['lead_name']);
// }
// //更改服务器状态
// if(isset($param['serve_id']) && !empty($param['serve_id'])){
// $this->updateServe($param['serve_id']);
// }
}
return $this->success();
}
... ...
... ... @@ -11,6 +11,7 @@ class NoticeLog extends Model
const TYPE_PROJECT = 'project';
const TYPE_RANK_DATA = 'rank_data';
const TYPE_INIT_PROJECT = 'init_project';
const STATUS_PENDING = 0;
const STATUS_SUCCESS = 1;
... ...
... ... @@ -78,6 +78,8 @@ class Project extends Base
7 => '定制建站(PS订制)',
8 => '星链网站(1年版)',
9 => '星链网站(2年版)',
11 => '俄语标准版',
12 => '俄语商务版',
];
}
... ...
... ... @@ -37,6 +37,9 @@ class ProjectServer extends BaseService
config(['database.connections.custom_mysql.database' => $project->databaseName()]);
config(['database.connections.custom_mysql.username' => $project->mysqlConfig->user]);
config(['database.connections.custom_mysql.password' => $project->mysqlConfig->password]);
//重连
DB::connection('custom_mysql')->reconnect();
// 设置 redis 配置
return $project;
}
... ...