123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
-
- namespace Illuminate\Queue;
-
- use Exception;
- use ReflectionClass;
- use Illuminate\Contracts\Queue\Job;
- use Illuminate\Contracts\Bus\Dispatcher;
- use Illuminate\Database\Eloquent\ModelNotFoundException;
-
- class CallQueuedHandler
- {
- /**
- * The bus dispatcher implementation.
- *
- * @var \Illuminate\Contracts\Bus\Dispatcher
- */
- protected $dispatcher;
-
- /**
- * Create a new handler instance.
- *
- * @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
- * @return void
- */
- public function __construct(Dispatcher $dispatcher)
- {
- $this->dispatcher = $dispatcher;
- }
-
- /**
- * Handle the queued job.
- *
- * @param \Illuminate\Contracts\Queue\Job $job
- * @param array $data
- * @return void
- */
- public function call(Job $job, array $data)
- {
- try {
- $command = $this->setJobInstanceIfNecessary(
- $job, unserialize($data['command'])
- );
- } catch (ModelNotFoundException $e) {
- return $this->handleModelNotFound($job, $e);
- }
-
- $this->dispatcher->dispatchNow(
- $command, $this->resolveHandler($job, $command)
- );
-
- if (! $job->hasFailed() && ! $job->isReleased()) {
- $this->ensureNextJobInChainIsDispatched($command);
- }
-
- if (! $job->isDeletedOrReleased()) {
- $job->delete();
- }
- }
-
- /**
- * Resolve the handler for the given command.
- *
- * @param \Illuminate\Contracts\Queue\Job $job
- * @param mixed $command
- * @return mixed
- */
- protected function resolveHandler($job, $command)
- {
- $handler = $this->dispatcher->getCommandHandler($command) ?: null;
-
- if ($handler) {
- $this->setJobInstanceIfNecessary($job, $handler);
- }
-
- return $handler;
- }
-
- /**
- * Set the job instance of the given class if necessary.
- *
- * @param \Illuminate\Contracts\Queue\Job $job
- * @param mixed $instance
- * @return mixed
- */
- protected function setJobInstanceIfNecessary(Job $job, $instance)
- {
- if (in_array(InteractsWithQueue::class, class_uses_recursive(get_class($instance)))) {
- $instance->setJob($job);
- }
-
- return $instance;
- }
-
- /**
- * Ensure the next job in the chain is dispatched if applicable.
- *
- * @param mixed $command
- * @return void
- */
- protected function ensureNextJobInChainIsDispatched($command)
- {
- if (method_exists($command, 'dispatchNextJobInChain')) {
- $command->dispatchNextJobInChain();
- }
- }
-
- /**
- * Handle a model not found exception.
- *
- * @param \Illuminate\Contracts\Queue\Job $job
- * @param \Exception $e
- * @return void
- */
- protected function handleModelNotFound(Job $job, $e)
- {
- $class = $job->resolveName();
-
- try {
- $shouldDelete = (new ReflectionClass($class))
- ->getDefaultProperties()['deleteWhenMissingModels'] ?? false;
- } catch (Exception $e) {
- $shouldDelete = false;
- }
-
- if ($shouldDelete) {
- return $job->delete();
- }
-
- return FailingJob::handle(
- $job->getConnectionName(), $job, $e
- );
- }
-
- /**
- * Call the failed method on the job instance.
- *
- * The exception that caused the failure will be passed.
- *
- * @param array $data
- * @param \Exception $e
- * @return void
- */
- public function failed(array $data, $e)
- {
- $command = unserialize($data['command']);
-
- if (method_exists($command, 'failed')) {
- $command->failed($e);
- }
- }
- }
|