ImageArrJob.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $job->delete();
  28. die;
  29. }else if (empty($data['status_val']) || $data['status_val'] == '图生文') {
  30. Queue::push('app\job\ImageJob', $data, 'imgtotxt');
  31. echo date('Y-m-d H:i:s') . " 队列已启动\n";
  32. $job->delete();
  33. die;
  34. } else {
  35. $task_id = $data['task_id'];
  36. $images = $data['data'];
  37. foreach ($images as $value) {
  38. $value['task_id'] = $task_id;
  39. // 清理路径:去除末尾 Preview 目录
  40. $sourceDir = rtrim($value["sourceDir"], '/\\');
  41. $dirParts = explode('/', str_replace('\\', '/', $sourceDir));
  42. if (end($dirParts) === 'Preview') {
  43. array_pop($dirParts);
  44. }
  45. $cleanedSourceDir = implode('/', $dirParts);
  46. // 获取文件名并拼接最终路径
  47. $fileName = basename($value['file_name']);
  48. $fullPath = $cleanedSourceDir . '/' . $fileName;
  49. // 写入 image_task_log 队列明细日志记录
  50. $log_id = Db::name('image_task_log')->insertGetId([
  51. 'task_id' => $task_id,
  52. 'file_name' => $fullPath,
  53. 'model_name' => $value['type'] ?? '',
  54. 'status' => 0,
  55. 'log' => '队列中',
  56. 'create_time' => date('Y-m-d H:i:s')
  57. ]);
  58. $value['log_id'] = $log_id;
  59. //若有链式任务,传递下去
  60. $chain_next = $value['chain_next'] ?? [];
  61. switch (trim($value['type'])) {
  62. case '图生文':
  63. Queue::push('app\job\ImageJob', array_merge($value, ['chain_next' => $chain_next]), 'imgtotxt');
  64. break;
  65. case '文生文':
  66. Queue::push('app\job\TextToTextJob', array_merge($value, ['chain_next' => $chain_next]), 'txttotxt');
  67. break;
  68. case '文生图':
  69. Queue::push('app\job\TextToImageJob', array_merge($value, ['chain_next' => $chain_next]), 'txttoimg');
  70. break;
  71. case '图生图':
  72. Queue::push('app\job\ImageToImageJob', array_merge($value, ['chain_next' => $chain_next]), 'imgtoimg');
  73. break;
  74. case '高清放大':
  75. Queue::push('app\job\ImageToSingleJob', array_merge($value, ['chain_next' => $chain_next]), 'single');
  76. break;
  77. default:
  78. \think\Log::warning("未识别的任务类型:" . json_encode($value, JSON_UNESCAPED_UNICODE));
  79. break;
  80. }
  81. }
  82. //更新任务状态为已启动
  83. Db::name('queue_logs')->where('id', $task_id)->update(['status' => '已启动队列']);
  84. echo date('Y-m-d H:i:s') . " 队列已启动\n";
  85. }
  86. //推送后删除当前队列任务
  87. $job->delete();
  88. }
  89. /**
  90. * 任务执行失败回调
  91. *
  92. * @param array $data 原始任务数据
  93. */
  94. public function failed($data)
  95. {
  96. \think\Log::error("ImageArrJob 任务失败:" . json_encode($data, JSON_UNESCAPED_UNICODE));
  97. }
  98. }