| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace app\job;
- use think\Db;
- use think\queue\Job;
- use think\Queue;
- class ImageArrJob
- {
- public function fire(Job $job, $data)
- {
- //队列id
- $task_id = $data['task_id'];
- //队列数据
- $images = $data['data'];
- foreach ($images as $value) {
- $value['task_id'] = $task_id;
- // 1. 清理 sourceDir,去掉末尾重复的 Preview
- $sourceDir = rtrim($value["sourceDir"], '/\\');
- $dirParts = explode('/', str_replace('\\', '/', $sourceDir));
- if (end($dirParts) === 'Preview') {
- array_pop($dirParts); // 删除末尾 Preview
- }
- $cleanedSourceDir = implode('/', $dirParts);
- // 2. 获取纯文件名
- $fileName = basename($value['file_name']);
- // 3. 拼接最终路径
- $fullPath = $cleanedSourceDir . '/' . $fileName;
- // 4. 写入日志记录
- $log_id = Db::name('image_task_log')->insertGetId([
- 'task_id' => $task_id,
- 'file_name' => $fullPath,
- 'model_name' => $value['type'] ?? '',
- 'status' => 0,
- 'log' => '队列中',
- 'create_time' => date('Y-m-d H:i:s')
- ]);
- $value['log_id'] = $log_id;
- // 6. 若有链式任务,传递下去
- $chain_next = $value['chain_next'] ?? [];
- switch (trim($value['type'])) {
- case '图生文':
- Queue::push('app\job\ImageJob', array_merge($value, ['chain_next' => $chain_next]), 'imgtotxt');
- break;
- case '文生文':
- Queue::push('app\job\TextToTextJob', array_merge($value, ['chain_next' => $chain_next]), 'txttotxt');
- break;
- case '文生图':
- Queue::push('app\job\TextToImageJob', array_merge($value, ['chain_next' => $chain_next]), 'txttoimg');
- break;
- default:
- \think\Log::warning("未识别的任务类型:" . json_encode($value, JSON_UNESCAPED_UNICODE));
- break;
- }
- }
- // 6. 更新任务状态为已启动
- Db::name('queue_logs')->where('id', $task_id)->update(['status' => '已启动队列']);
- echo date('Y-m-d H:i:s') . " 队列已启动\n";
- // 7. 删除当前队列任务
- $job->delete();
- }
- public function failed($data)
- {
- \think\Log::error("ImageArrJob 任务失败:" . json_encode($data, JSON_UNESCAPED_UNICODE));
- }
- }
|