FailingJob.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Illuminate\Queue;
  3. use Illuminate\Container\Container;
  4. use Illuminate\Queue\Events\JobFailed;
  5. use Illuminate\Contracts\Events\Dispatcher;
  6. class FailingJob
  7. {
  8. /**
  9. * Delete the job, call the "failed" method, and raise the failed job event.
  10. *
  11. * @param string $connectionName
  12. * @param \Illuminate\Queue\Jobs\Job $job
  13. * @param \Exception $e
  14. * @return void
  15. */
  16. public static function handle($connectionName, $job, $e = null)
  17. {
  18. $job->markAsFailed();
  19. if ($job->isDeleted()) {
  20. return;
  21. }
  22. try {
  23. // If the job has failed, we will delete it, call the "failed" method and then call
  24. // an event indicating the job has failed so it can be logged if needed. This is
  25. // to allow every developer to better keep monitor of their failed queue jobs.
  26. $job->delete();
  27. $job->failed($e);
  28. } finally {
  29. static::events()->dispatch(new JobFailed(
  30. $connectionName, $job, $e ?: new ManuallyFailedException
  31. ));
  32. }
  33. }
  34. /**
  35. * Get the event dispatcher instance.
  36. *
  37. * @return \Illuminate\Contracts\Events\Dispatcher
  38. */
  39. protected static function events()
  40. {
  41. return Container::getInstance()->make(Dispatcher::class);
  42. }
  43. }