ImageArrJob.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\job;
  3. use think\Db;
  4. use think\queue\Job;
  5. use think\Queue;
  6. /**
  7. * ImageArrJob 图像任务初始化队列
  8. * 用于将批量图像任务按类型分发至下游子队列(图生文/文生文/文生图)
  9. */
  10. class ImageArrJob
  11. {
  12. /**
  13. * 队列任务执行函数
  14. *
  15. * @param Job $job 当前队列任务对象
  16. * @param array $data 传入的数据,包含 task_id 和图像列表 data
  17. * task_id:队列id
  18. * data:队列数据
  19. */
  20. public function fire(Job $job, $data)
  21. {
  22. // 将 $data 强制转换为数组(兼容对象/字符串等类型)
  23. $data = (array)$data;
  24. if (empty($data['status_val']) || $data['status_val'] == '文生图') {
  25. Queue::push('app\job\TextToImageJob', $data, 'txttoimg');
  26. echo date('Y-m-d H:i:s') . " 队列已启动\n";
  27. }else if (empty($data['status_val']) || $data['status_val'] == '图生文') {
  28. Queue::push('app\job\ImageJob', $data, 'imgtotxt');
  29. echo date('Y-m-d H:i:s') . " 队列已启动\n";
  30. } else {
  31. $task_id = $data['task_id'];
  32. $images = $data['data'];
  33. foreach ($images as $value) {
  34. $value['task_id'] = $task_id;
  35. // 清理路径:去除末尾 Preview 目录
  36. $sourceDir = rtrim($value["sourceDir"], '/\\');
  37. $dirParts = explode('/', str_replace('\\', '/', $sourceDir));
  38. if (end($dirParts) === 'Preview') {
  39. array_pop($dirParts);
  40. }
  41. $cleanedSourceDir = implode('/', $dirParts);
  42. // 获取文件名并拼接最终路径
  43. $fileName = basename($value['file_name']);
  44. $fullPath = $cleanedSourceDir . '/' . $fileName;
  45. // 写入 image_task_log 队列明细日志记录
  46. $log_id = Db::name('image_task_log')->insertGetId([
  47. 'task_id' => $task_id,
  48. 'file_name' => $fullPath,
  49. 'model_name' => $value['type'] ?? '',
  50. 'status' => 0,
  51. 'log' => '队列中',
  52. 'create_time' => date('Y-m-d H:i:s')
  53. ]);
  54. $value['log_id'] = $log_id;
  55. //若有链式任务,传递下去
  56. $chain_next = $value['chain_next'] ?? [];
  57. switch (trim($value['type'])) {
  58. case '图生文':
  59. Queue::push('app\job\ImageJob', array_merge($value, ['chain_next' => $chain_next]), 'imgtotxt');
  60. break;
  61. case '文生文':
  62. Queue::push('app\job\TextToTextJob', array_merge($value, ['chain_next' => $chain_next]), 'txttotxt');
  63. break;
  64. case '文生图':
  65. Queue::push('app\job\TextToImageJob', array_merge($value, ['chain_next' => $chain_next]), 'txttoimg');
  66. break;
  67. case '图生图':
  68. Queue::push('app\job\ImageToImageJob', array_merge($value, ['chain_next' => $chain_next]), 'imgtoimg');
  69. break;
  70. case '高清放大':
  71. Queue::push('app\job\ImageToSingleJob', array_merge($value, ['chain_next' => $chain_next]), 'single');
  72. break;
  73. default:
  74. \think\Log::warning("未识别的任务类型:" . json_encode($value, JSON_UNESCAPED_UNICODE));
  75. break;
  76. }
  77. }
  78. //更新任务状态为已启动
  79. Db::name('queue_logs')->where('id', $task_id)->update(['status' => '已启动队列']);
  80. echo date('Y-m-d H:i:s') . " 队列已启动\n";
  81. }
  82. //推送后删除当前队列任务
  83. $job->delete();
  84. }
  85. /**
  86. * 任务执行失败回调
  87. *
  88. * @param array $data 原始任务数据
  89. */
  90. public function failed($data)
  91. {
  92. \think\Log::error("ImageArrJob 任务失败:" . json_encode($data, JSON_UNESCAPED_UNICODE));
  93. }
  94. }