SyncQueue.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Illuminate\Queue;
  3. use Exception;
  4. use Throwable;
  5. use Illuminate\Queue\Jobs\SyncJob;
  6. use Illuminate\Contracts\Queue\Job;
  7. use Illuminate\Contracts\Queue\Queue as QueueContract;
  8. use Symfony\Component\Debug\Exception\FatalThrowableError;
  9. class SyncQueue extends Queue implements QueueContract
  10. {
  11. /**
  12. * Get the size of the queue.
  13. *
  14. * @param string $queue
  15. * @return int
  16. */
  17. public function size($queue = null)
  18. {
  19. return 0;
  20. }
  21. /**
  22. * Push a new job onto the queue.
  23. *
  24. * @param string $job
  25. * @param mixed $data
  26. * @param string $queue
  27. * @return mixed
  28. *
  29. * @throws \Exception|\Throwable
  30. */
  31. public function push($job, $data = '', $queue = null)
  32. {
  33. $queueJob = $this->resolveJob($this->createPayload($job, $data), $queue);
  34. try {
  35. $this->raiseBeforeJobEvent($queueJob);
  36. $queueJob->fire();
  37. $this->raiseAfterJobEvent($queueJob);
  38. } catch (Exception $e) {
  39. $this->handleException($queueJob, $e);
  40. } catch (Throwable $e) {
  41. $this->handleException($queueJob, new FatalThrowableError($e));
  42. }
  43. return 0;
  44. }
  45. /**
  46. * Resolve a Sync job instance.
  47. *
  48. * @param string $payload
  49. * @param string $queue
  50. * @return \Illuminate\Queue\Jobs\SyncJob
  51. */
  52. protected function resolveJob($payload, $queue)
  53. {
  54. return new SyncJob($this->container, $payload, $this->connectionName, $queue);
  55. }
  56. /**
  57. * Raise the before queue job event.
  58. *
  59. * @param \Illuminate\Contracts\Queue\Job $job
  60. * @return void
  61. */
  62. protected function raiseBeforeJobEvent(Job $job)
  63. {
  64. if ($this->container->bound('events')) {
  65. $this->container['events']->dispatch(new Events\JobProcessing($this->connectionName, $job));
  66. }
  67. }
  68. /**
  69. * Raise the after queue job event.
  70. *
  71. * @param \Illuminate\Contracts\Queue\Job $job
  72. * @return void
  73. */
  74. protected function raiseAfterJobEvent(Job $job)
  75. {
  76. if ($this->container->bound('events')) {
  77. $this->container['events']->dispatch(new Events\JobProcessed($this->connectionName, $job));
  78. }
  79. }
  80. /**
  81. * Raise the exception occurred queue job event.
  82. *
  83. * @param \Illuminate\Contracts\Queue\Job $job
  84. * @param \Exception $e
  85. * @return void
  86. */
  87. protected function raiseExceptionOccurredJobEvent(Job $job, $e)
  88. {
  89. if ($this->container->bound('events')) {
  90. $this->container['events']->dispatch(new Events\JobExceptionOccurred($this->connectionName, $job, $e));
  91. }
  92. }
  93. /**
  94. * Handle an exception that occurred while processing a job.
  95. *
  96. * @param \Illuminate\Queue\Jobs\Job $queueJob
  97. * @param \Exception $e
  98. * @return void
  99. *
  100. * @throws \Exception
  101. */
  102. protected function handleException($queueJob, $e)
  103. {
  104. $this->raiseExceptionOccurredJobEvent($queueJob, $e);
  105. FailingJob::handle($this->connectionName, $queueJob, $e);
  106. throw $e;
  107. }
  108. /**
  109. * Push a raw payload onto the queue.
  110. *
  111. * @param string $payload
  112. * @param string $queue
  113. * @param array $options
  114. * @return mixed
  115. */
  116. public function pushRaw($payload, $queue = null, array $options = [])
  117. {
  118. //
  119. }
  120. /**
  121. * Push a new job onto the queue after a delay.
  122. *
  123. * @param \DateTimeInterface|\DateInterval|int $delay
  124. * @param string $job
  125. * @param mixed $data
  126. * @param string $queue
  127. * @return mixed
  128. */
  129. public function later($delay, $job, $data = '', $queue = null)
  130. {
  131. return $this->push($job, $data, $queue);
  132. }
  133. /**
  134. * Pop the next job off of the queue.
  135. *
  136. * @param string $queue
  137. * @return \Illuminate\Contracts\Queue\Job|null
  138. */
  139. public function pop($queue = null)
  140. {
  141. //
  142. }
  143. }