RetryCommand.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Illuminate\Queue\Console;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Console\Command;
  5. class RetryCommand extends Command
  6. {
  7. /**
  8. * The console command signature.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'queue:retry {id* : The ID of the failed job or "all" to retry all jobs.}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Retry a failed queue job';
  19. /**
  20. * Execute the console command.
  21. *
  22. * @return void
  23. */
  24. public function handle()
  25. {
  26. foreach ($this->getJobIds() as $id) {
  27. $job = $this->laravel['queue.failer']->find($id);
  28. if (is_null($job)) {
  29. $this->error("Unable to find failed job with ID [{$id}].");
  30. } else {
  31. $this->retryJob($job);
  32. $this->info("The failed job [{$id}] has been pushed back onto the queue!");
  33. $this->laravel['queue.failer']->forget($id);
  34. }
  35. }
  36. }
  37. /**
  38. * Get the job IDs to be retried.
  39. *
  40. * @return array
  41. */
  42. protected function getJobIds()
  43. {
  44. $ids = (array) $this->argument('id');
  45. if (count($ids) === 1 && $ids[0] === 'all') {
  46. $ids = Arr::pluck($this->laravel['queue.failer']->all(), 'id');
  47. }
  48. return $ids;
  49. }
  50. /**
  51. * Retry the queue job.
  52. *
  53. * @param \stdClass $job
  54. * @return void
  55. */
  56. protected function retryJob($job)
  57. {
  58. $this->laravel['queue']->connection($job->connection)->pushRaw(
  59. $this->resetAttempts($job->payload), $job->queue
  60. );
  61. }
  62. /**
  63. * Reset the payload attempts.
  64. *
  65. * Applicable to Redis jobs which store attempts in their payload.
  66. *
  67. * @param string $payload
  68. * @return string
  69. */
  70. protected function resetAttempts($payload)
  71. {
  72. $payload = json_decode($payload, true);
  73. if (isset($payload['attempts'])) {
  74. $payload['attempts'] = 0;
  75. }
  76. return json_encode($payload);
  77. }
  78. }