作者 周海龙

合并分支 'zhl' 到 'master'

Zhl



查看合并请求 !1755
... ... @@ -40,8 +40,7 @@ class AiBlogTask extends Command
protected $signature = 'save_ai_blog';
public $updateProject = [];//需更新的列表
public $projectSetting = [];
/**
* The console command description.
... ... @@ -50,10 +49,14 @@ class AiBlogTask extends Command
*/
protected $description = '查询ai_blog是否已经生成';
/**
* @return bool
* @throws \Exception
*/
public function handle(){
while (true){
//获取任务id
$task_id = $this->getTaskId(2);
$task_id = $this->getTaskId();
if(empty($task_id)){
sleep(300);
continue;
... ... @@ -64,16 +67,15 @@ class AiBlogTask extends Command
}
/**
* @remark :请求方法
* @name :sendRequest
* @author :lyh
* @method :post
* @time :2025/3/19 16:48
* 请求方法
* @param $task_id
* @return bool
* @throws \Exception
*/
public function sendRequest($task_id){
$aiBlogTaskModel = new AiBlogTaskModel();
$item = $aiBlogTaskModel->read(['id'=>$task_id]);
echo date('Y-m-d H:i:s') . '开始->任务id:' . $item['task_id'] . PHP_EOL;
$this->output('start:project ID: ' . $item['project_id'] . ', task ID: ' . $task_id);
//获取配置
$aiSettingInfo = $this->getSetting($item['project_id']);
$aiBlogService = new AiBlogService();
... ... @@ -82,7 +84,9 @@ class AiBlogTask extends Command
$aiBlogService->task_id = $item['task_id'];
//拉取文章数据
$result = $aiBlogService->getDetail();
if(!isset($result['status']) || ($result['status'] != 200)){
// 获取失败
if(empty($result['status']) || $result['status'] != 200){
if($item['sort'] < 5){
$aiBlogTaskModel->edit(['sort'=>$item['sort'] + 1],['id'=>$item['id']]);
}else{
... ... @@ -90,15 +94,16 @@ class AiBlogTask extends Command
// 钉钉通知
$dingService = new DingService();
$body = [
'keyword' => 'AI_BLOG生成错误',
'msg' => '任务id:'.$item['task_id'].'拉取数据失败'.PHP_EOL.'返回信息:'.json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
'keyword' => 'AI_BLOG获取失败',
'msg' => '任务ID:' . $item['task_id'] . PHP_EOL . '返回信息:' . json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
'isAtAll' => false, // 是否@所有人
];
$dingService->handle($body);
}
echo date('Y-m-d H:i:s'). '错误信息:'.json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).PHP_EOL;
$this->output('error: 数据获取失败,status:' . $result['status'] . ', message: ' . ($result['message'] ?? 'null'));
return false;
}
//保存当前项目ai_blog数据
ProjectServer::useProject($item['project_id']);
$aiBlogModel = new AiBlog();
... ... @@ -108,11 +113,12 @@ class AiBlogTask extends Command
$dingService = new DingService();
$body = [
'keyword' => 'AI_BLOG生成错误',
'msg' => '子任务不存在:'.$item['task_id'],
'msg' => '任务ID:' . $item['task_id'] . ', 子库获取数据失败, 检查子库数据是否被删除!',
'isAtAll' => false, // 是否@所有人
];
$dingService->handle($body);
echo '任务id不存在:'.$item['task_id'].PHP_EOL;
$this->output('error: 子库获取数据失败, task id: ' . $task_id);
$aiBlogTaskModel->edit(['status'=>9],['id'=>$item['id']]);
DB::disconnect('custom_mysql');
return false;
... ... @@ -128,19 +134,17 @@ class AiBlogTask extends Command
$aiBlogModel->edit(['new_title'=>$result['data']['title'], 'image'=>$result['data']['thumb'], 'text'=>$result['data']['section'], 'author_id'=>$result['data']['author_id'],'seo_title'=>$result['data']['title'],'seo_keyword'=>$result['data']['keyword'],'seo_description'=>$result['data']['description'], 'route'=>$route ,'status'=>$aiBlogModel::STATUS_FINISH], ['task_id'=>$item['task_id']]);
DB::disconnect('custom_mysql');
$aiBlogTaskModel->edit(['status'=>$aiBlogModel::STATUS_FINISH],['id'=>$item['id']]);
echo date('Y-m-d H:i:s').'结束->任务id:' . $item['task_id'] . PHP_EOL;
$this->output('success: task id: ' . $task_id);
return true;
}
/**
* @remark :获取任务id
* @name :getTaskId
* @author :lyh
* @method :post
* @time :2025/3/19 16:16
* @param :
* 获取任务id
* @param int $finish_at
* @return mixed
*/
public function getTaskId($finish_at = 1)
public function getTaskId($finish_at = 2)
{
$task_id = Redis::rpop('ai_blog_task');
if (empty($task_id)) {
... ... @@ -150,7 +154,7 @@ class AiBlogTask extends Command
}
$aiBlogTaskModel = new AiBlogTaskModel();
$finish_at = date('Y-m-d H:i:s', strtotime('-' . $finish_at . ' hour'));
$ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING,'type'=>$aiBlogTaskModel::TYPE_BLOG,'updated_at'=>['<=',$finish_at]])->pluck('id');
$ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING, 'type'=>$aiBlogTaskModel::TYPE_BLOG, 'updated_at'=>['<=',$finish_at]])->pluck('id');
if(!empty($ids)){
foreach ($ids as $id) {
Redis::lpush('ai_blog_task', $id);
... ... @@ -162,49 +166,57 @@ class AiBlogTask extends Command
}
/**
* @remark :更新项目作者页面及列表页
* @name :updateProject
* @author :lyh
* @method :post
* @time :2025/3/4 10:25
* 更新项目作者页面及列表页
* @param $updateProject
* @return bool
*/
public function updateProject($updateProject){
if(empty($updateProject)){
return true;
}
foreach ($updateProject as $project_id => $author){
ProjectServer::useProject($project_id);
$aiSettingInfo = $this->getSetting($project_id);
$this->output('sync: list start, project_id: ' . $project_id);
$this->updateBlogList($aiSettingInfo);
$this->output('sync: list end');
//更新作者
$this->output('sync: author start, project_id: ' . $project_id);
foreach ($author as $val){
$this->updateAiBlogAuthor($aiSettingInfo,$val);
}
$this->output('sync: author end');
DB::disconnect('custom_mysql');
$this->curlDelRoute($project_id);
}
return true;
}
/**
* @remark :获取项目配置
* @name :getSetting
* @author :lyh
* @method :post
* @time :2025/2/14 11:27
* 获取项目配置
* @param $project_id
* @return bool
*/
public function getSetting($project_id){
public function getSetting($project_id)
{
$project_setting = $this->projectSetting;
if (FALSE == empty($project_setting[$project_id]))
return $project_setting[$project_id];
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
$this->projectSetting[$project_id] = $aiSettingInfo;
return $aiSettingInfo;
}
/**
* @remark :更新作者的页面
* @name :updateAiBlogAuthor
* @author :lyh
* @method :post
* @time :2025/2/21 11:53
* 更新作者的页面
* @param $aiSettingInfo
* @param $author_id
* @return bool
*/
public function updateAiBlogAuthor($aiSettingInfo,$author_id){
if(empty($author_id)){
... ... @@ -226,11 +238,9 @@ class AiBlogTask extends Command
}
/**
* @remark :更新列表页
* @name :updateBlogList
* @author :lyh
* @method :post
* @time :2025/2/26 15:42
* 更新列表页
* @param $aiSettingInfo
* @return bool
*/
public function updateBlogList($aiSettingInfo){
$aiBlogService = new AiBlogService();
... ... @@ -268,55 +278,69 @@ class AiBlogTask extends Command
}
/**
* @remark :通知C端生成界面
* @name :sendNotice
* @author :lyh
* @method :post
* @time :2025/3/6 11:51
* 通知C端生成界面
* @param $project_id
* @return bool
*/
public function curlDelRoute($project_id){
$domainModel = new DomainInfo();
//获取项目域名
$domain = $domainModel->getProjectIdDomain($project_id);
if(!empty($domain)){
//判断是否是自建站服务器,如果是,不请求C端接口,数据直接入库
$project_model = new Project();
$project_info = $project_model->read(['id'=>$project_id],['serve_id']);
if(!$project_info){
echo '项目不存在:' . $project_id . PHP_EOL . date('Y-m-d H:i:s');
return true;
}
$serve_ip_model = new ServersIp();
$serve_ip_info = $serve_ip_model->read(['id'=>$project_info['serve_id']],['servers_id']);
$servers_id = $serve_ip_info ? $serve_ip_info['servers_id'] : 0;
if($servers_id == ServerConfig::SELF_SITE_ID){
//判断是否已有更新进行中
$notify_model = new Notify();
$data = [
'project_id' => $project_id,
'type' => Notify::TYPE_MASTER,
'route' => Notify::ROUTE_AI_BLOG,
'server_id' => ServerConfig::SELF_SITE_ID,
'status' => ['!=',Notify::STATUS_FINISH_SITEMAP]
];
$notify = $notify_model->read($data,['id']);
if (empty($domain)) {
$this->output('send: 域名不存在, project id: ' . $project_id);
return true;
}
if(!$notify){
$domain_array = parse_url($domain);
$data['data'] = Arr::a2s(['domain'=>$domain_array['host'],'url'=>[],'language'=>[]]);
$data['status'] = Notify::STATUS_INIT;
$data['sort'] = 2;
$notify_model->add($data);
}
echo '自建站项目:'.$project_id.'更新';
}else{
$url = $domain.'api/update_page/?project_id='.$project_id.'&route=7';
$res = http_get($url);
echo '通知C端:'.json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).PHP_EOL;
//判断是否是自建站服务器,如果是,不请求C端接口,数据直接入库
$project_model = new Project();
$project_info = $project_model->read(['id'=>$project_id],['serve_id']);
if(!$project_info){
$this->output('send: 项目不存在, project id: ' . $project_id);
return true;
}
$serve_ip_model = new ServersIp();
$serve_ip_info = $serve_ip_model->read(['id'=>$project_info['serve_id']],['servers_id']);
$servers_id = $serve_ip_info ? $serve_ip_info['servers_id'] : 0;
if($servers_id == ServerConfig::SELF_SITE_ID){
//判断是否已有更新进行中
$notify_model = new Notify();
$data = [
'project_id' => $project_id,
'type' => Notify::TYPE_MASTER,
'route' => Notify::ROUTE_AI_BLOG,
'server_id' => ServerConfig::SELF_SITE_ID,
'status' => ['!=',Notify::STATUS_FINISH_SITEMAP]
];
$notify = $notify_model->read($data,['id']);
if(!$notify){
$domain_array = parse_url($domain);
$data['data'] = Arr::a2s(['domain'=>$domain_array['host'],'url'=>[],'language'=>[]]);
$data['status'] = Notify::STATUS_INIT;
$data['sort'] = 2;
$notify_model->add($data);
}
$this->output('send: 自建站项目, project id: ' . $project_id);
}else{
echo '域名不存在:' . $project_id . PHP_EOL . date('Y-m-d H:i:s');
$url = $domain . 'api/update_page/?project_id='.$project_id.'&route=7';
$res = http_get($url);
$this->output('notify: project id: ' . $project_id . ', result: ' . json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
return true;
}
/**
* 输入日志
* @param $message
* @return bool
*/
public function output($message)
{
$message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
echo $message;
file_put_contents(storage_path('logs/AiBlog/') . date('Ymd') . '.log', $message, FILE_APPEND);
return true;
}
}
... ...