25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3KB

  1. <?php
  2. namespace Framework\Queue\Command;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Exception;
  9. class WorkCommand extends Command
  10. {
  11. protected static $defaultName = 'queue:work';
  12. protected function configure()
  13. {
  14. $this
  15. ->setDescription('Runs tasks that have been queued')
  16. ->setHelp('This command waits for and runs queued jobs');
  17. }
  18. protected function execute(InputInterface $input, OutputInterface $output)
  19. {
  20. $output->writeln('<info>Waiting for jobs.</info>');
  21. while(true) {
  22. if ($job = app('queue')->shift()) {
  23. try {
  24. $job->run();
  25. $output->writeln("<info>Completed {$job->id}</info>");
  26. $job->is_complete = true;
  27. $job->save();
  28. sleep(1);
  29. }
  30. catch (Exception $e) {
  31. $message = $e->getMessage();
  32. $output->writeln("<error>{$message}</error>");
  33. $job->attempts = $job->attempts + 1;
  34. $job->save();
  35. }
  36. }
  37. }
  38. }
  39. }

Powered by TurnKey Linux.