Psaijob.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace app\admin\command;
  3. use app\common\library\ps\AiRecognizer;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Option;
  7. use think\console\Output;
  8. /**
  9. * 后台/计划任务执行 AI 识版。
  10. */
  11. class Psaijob extends Command
  12. {
  13. protected function configure()
  14. {
  15. $this->setName('ps:ai-job')
  16. ->addOption('id', null, Option::VALUE_OPTIONAL, 'job id', 0)
  17. ->addOption('limit', null, Option::VALUE_OPTIONAL, 'pending batch size', 1)
  18. ->setDescription('Run AI template recognition jobs');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. $id = (int)$input->getOption('id');
  23. if ($id > 0) {
  24. $job = AiRecognizer::run($id);
  25. $output->writeln('job #' . $id . ' => ' . $job['status']);
  26. if (!empty($job['error_msg'])) {
  27. $output->writeln($job['error_msg']);
  28. }
  29. return;
  30. }
  31. $ids = AiRecognizer::pendingIds((int)$input->getOption('limit'));
  32. if (!$ids) {
  33. $output->writeln('no pending jobs');
  34. return;
  35. }
  36. foreach ($ids as $jobId) {
  37. $job = AiRecognizer::run($jobId);
  38. $output->writeln('job #' . $jobId . ' => ' . $job['status']);
  39. if (!empty($job['error_msg'])) {
  40. $output->writeln($job['error_msg']);
  41. }
  42. }
  43. }
  44. }