自动化队列

This commit is contained in:
chengxl
2025-06-03 09:25:19 +08:00
parent 22e4986329
commit 1c7bc19ae2
2 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
<?php
namespace app\api\job;
use think\queue\Job;
use app\api\controller\Aiarticle;
use app\common\QueueJob;
class ArticleAiCreateContent
{
// 文章AI内容生成
public function fire(Job $job, $data)
{
//获取任务ID
$iLogId = 0;
try {
//实例化
$oQueueJob = new QueueJob;
$sMsg = '文章AI内容生成成功';
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->create($data),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '文章AI内容声场失败' : $aResult['msg'];
}
}
$job->delete();
//更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam);
// // 记录日志
// \think\Log::info("ArticleAiCreateContent延迟任务执行成功: ".json_encode($data));
} catch (\Exception $e) {
//实例化
$oQueueJob = new QueueJob;
//更新任务状态
$sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage();
$aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam);
$job->delete();
// // 记录错误日志
// \think\Log::error("ArticleAiCreateContent延迟任务失败: ".$e->getMessage());
}finally {
gc_collect_cycles(); // 强制垃圾回收
}
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace app\api\job;
use think\queue\Job;
use app\api\controller\Aiarticle;
use app\common\QueueJob;
class WechatDraftPublish
{
// 发布草稿箱任务入口
public function fire(Job $job, $data)
{
//获取任务ID
$iLogId = 0;
try {
//实例化
$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->publishDraft($data),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '草稿箱发布失败' : $aResult['msg'];
}
}
$job->delete();
//更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam);
// // 记录日志
// \think\Log::info("WechatDraft-延迟任务执行成功: ".json_encode($data));
} catch (\Exception $e) {
//实例化
$oQueueJob = new QueueJob;
//更新任务状态
$sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage();
$aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam);
$job->delete();
// // 记录错误日志
// \think\Log::error("WechatDraft-延迟任务失败: ".$e->getMessage());
}finally {
gc_collect_cycles(); // 强制垃圾回收
}
}
}