队列任务加日志

This commit is contained in:
chengxl
2025-07-02 15:09:38 +08:00
parent 6b85ac0f98
commit 6947bde304
11 changed files with 368 additions and 340 deletions

View File

@@ -9,6 +9,10 @@ class WechatMaterial
public function fire(Job $job, $data)
{
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/WechatMaterial_'.date('Ymd').'.log';
file_put_contents($sLogPath,'-----------Queue job started-----------' . json_encode($data)."\n",FILE_APPEND);
//获取任务ID
$iLogId = 0;
try {
@@ -16,26 +20,26 @@ class WechatMaterial
//实例化
$oQueueJob = new QueueJob;
$sMsg = '上传素材成功';
if(!empty($job->getRawBody())){
$aJob = json_decode($job->getRawBody(), true);
$aParam = [
'job_id' => empty($aJob['id']) ? '' : $aJob['id'],
'job_class' => get_class($this),
'status' => 0,
'create_time' => time(),
'params' => json_encode($aJob, JSON_UNESCAPED_UNICODE)
];
$iLogId = $oQueueJob->addLog($aParam);
// 步骤1上传素材图片
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
if (!empty($iArticleId)) {
//上传素材
$oAiarticle = new Aiarticle;
$aResult = json_decode($oAiarticle->uploadMaterial($data),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '上传素材失败' : $aResult['msg'];
}
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true);
$aParam = [
'job_id' => empty($aJob['id']) ? 'WechatMaterial'.rand(100, 999) : $aJob['id'],
'job_class' => get_class($this),
'status' => 0,
'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE)
];
$iLogId = $oQueueJob->addLog($aParam);
// 步骤1上传素材图片
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
if (!empty($iArticleId)) {
//上传素材
$oAiarticle = new Aiarticle;
$aResult = json_decode($oAiarticle->uploadMaterial($data),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '上传素材失败' : $aResult['msg'];
}
$job->delete();
//更新任务状态
@@ -43,7 +47,7 @@ class WechatMaterial
$oQueueJob->updateLog($aParam);
// // 记录日志
// \think\Log::info("WechatMaterial-延迟任务执行成功: ".json_encode($data));
file_put_contents($sLogPath,'-----------Queue job end-----------' . json_encode($data)."\n\n\n",FILE_APPEND);
} catch (\Exception $e) {
//实例化
@@ -54,8 +58,6 @@ class WechatMaterial
$oQueueJob->updateLog($aParam);
$job->delete();
// // 记录错误日志
// \think\Log::error("WechatMaterial-延迟任务失败: ".$e->getMessage());
}finally {
gc_collect_cycles(); // 强制垃圾回收
}