CallQueuedHandler.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Illuminate\Queue;
  3. use Exception;
  4. use ReflectionClass;
  5. use Illuminate\Contracts\Queue\Job;
  6. use Illuminate\Contracts\Bus\Dispatcher;
  7. use Illuminate\Database\Eloquent\ModelNotFoundException;
  8. class CallQueuedHandler
  9. {
  10. /**
  11. * The bus dispatcher implementation.
  12. *
  13. * @var \Illuminate\Contracts\Bus\Dispatcher
  14. */
  15. protected $dispatcher;
  16. /**
  17. * Create a new handler instance.
  18. *
  19. * @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
  20. * @return void
  21. */
  22. public function __construct(Dispatcher $dispatcher)
  23. {
  24. $this->dispatcher = $dispatcher;
  25. }
  26. /**
  27. * Handle the queued job.
  28. *
  29. * @param \Illuminate\Contracts\Queue\Job $job
  30. * @param array $data
  31. * @return void
  32. */
  33. public function call(Job $job, array $data)
  34. {
  35. try {
  36. $command = $this->setJobInstanceIfNecessary(
  37. $job, unserialize($data['command'])
  38. );
  39. } catch (ModelNotFoundException $e) {
  40. return $this->handleModelNotFound($job, $e);
  41. }
  42. $this->dispatcher->dispatchNow(
  43. $command, $this->resolveHandler($job, $command)
  44. );
  45. if (! $job->hasFailed() && ! $job->isReleased()) {
  46. $this->ensureNextJobInChainIsDispatched($command);
  47. }
  48. if (! $job->isDeletedOrReleased()) {
  49. $job->delete();
  50. }
  51. }
  52. /**
  53. * Resolve the handler for the given command.
  54. *
  55. * @param \Illuminate\Contracts\Queue\Job $job
  56. * @param mixed $command
  57. * @return mixed
  58. */
  59. protected function resolveHandler($job, $command)
  60. {
  61. $handler = $this->dispatcher->getCommandHandler($command) ?: null;
  62. if ($handler) {
  63. $this->setJobInstanceIfNecessary($job, $handler);
  64. }
  65. return $handler;
  66. }
  67. /**
  68. * Set the job instance of the given class if necessary.
  69. *
  70. * @param \Illuminate\Contracts\Queue\Job $job
  71. * @param mixed $instance
  72. * @return mixed
  73. */
  74. protected function setJobInstanceIfNecessary(Job $job, $instance)
  75. {
  76. if (in_array(InteractsWithQueue::class, class_uses_recursive(get_class($instance)))) {
  77. $instance->setJob($job);
  78. }
  79. return $instance;
  80. }
  81. /**
  82. * Ensure the next job in the chain is dispatched if applicable.
  83. *
  84. * @param mixed $command
  85. * @return void
  86. */
  87. protected function ensureNextJobInChainIsDispatched($command)
  88. {
  89. if (method_exists($command, 'dispatchNextJobInChain')) {
  90. $command->dispatchNextJobInChain();
  91. }
  92. }
  93. /**
  94. * Handle a model not found exception.
  95. *
  96. * @param \Illuminate\Contracts\Queue\Job $job
  97. * @param \Exception $e
  98. * @return void
  99. */
  100. protected function handleModelNotFound(Job $job, $e)
  101. {
  102. $class = $job->resolveName();
  103. try {
  104. $shouldDelete = (new ReflectionClass($class))
  105. ->getDefaultProperties()['deleteWhenMissingModels'] ?? false;
  106. } catch (Exception $e) {
  107. $shouldDelete = false;
  108. }
  109. if ($shouldDelete) {
  110. return $job->delete();
  111. }
  112. return FailingJob::handle(
  113. $job->getConnectionName(), $job, $e
  114. );
  115. }
  116. /**
  117. * Call the failed method on the job instance.
  118. *
  119. * The exception that caused the failure will be passed.
  120. *
  121. * @param array $data
  122. * @param \Exception $e
  123. * @return void
  124. */
  125. public function failed(array $data, $e)
  126. {
  127. $command = unserialize($data['command']);
  128. if (method_exists($command, 'failed')) {
  129. $command->failed($e);
  130. }
  131. }
  132. }