Job.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace Illuminate\Queue\Jobs;
  3. use Illuminate\Support\InteractsWithTime;
  4. abstract class Job
  5. {
  6. use InteractsWithTime;
  7. /**
  8. * The job handler instance.
  9. *
  10. * @var mixed
  11. */
  12. protected $instance;
  13. /**
  14. * The IoC container instance.
  15. *
  16. * @var \Illuminate\Container\Container
  17. */
  18. protected $container;
  19. /**
  20. * Indicates if the job has been deleted.
  21. *
  22. * @var bool
  23. */
  24. protected $deleted = false;
  25. /**
  26. * Indicates if the job has been released.
  27. *
  28. * @var bool
  29. */
  30. protected $released = false;
  31. /**
  32. * Indicates if the job has failed.
  33. *
  34. * @var bool
  35. */
  36. protected $failed = false;
  37. /**
  38. * The name of the connection the job belongs to.
  39. */
  40. protected $connectionName;
  41. /**
  42. * The name of the queue the job belongs to.
  43. *
  44. * @var string
  45. */
  46. protected $queue;
  47. /**
  48. * Get the job identifier.
  49. *
  50. * @return string
  51. */
  52. abstract public function getJobId();
  53. /**
  54. * Get the raw body of the job.
  55. *
  56. * @return string
  57. */
  58. abstract public function getRawBody();
  59. /**
  60. * Fire the job.
  61. *
  62. * @return void
  63. */
  64. public function fire()
  65. {
  66. $payload = $this->payload();
  67. list($class, $method) = JobName::parse($payload['job']);
  68. ($this->instance = $this->resolve($class))->{$method}($this, $payload['data']);
  69. }
  70. /**
  71. * Delete the job from the queue.
  72. *
  73. * @return void
  74. */
  75. public function delete()
  76. {
  77. $this->deleted = true;
  78. }
  79. /**
  80. * Determine if the job has been deleted.
  81. *
  82. * @return bool
  83. */
  84. public function isDeleted()
  85. {
  86. return $this->deleted;
  87. }
  88. /**
  89. * Release the job back into the queue.
  90. *
  91. * @param int $delay
  92. * @return void
  93. */
  94. public function release($delay = 0)
  95. {
  96. $this->released = true;
  97. }
  98. /**
  99. * Determine if the job was released back into the queue.
  100. *
  101. * @return bool
  102. */
  103. public function isReleased()
  104. {
  105. return $this->released;
  106. }
  107. /**
  108. * Determine if the job has been deleted or released.
  109. *
  110. * @return bool
  111. */
  112. public function isDeletedOrReleased()
  113. {
  114. return $this->isDeleted() || $this->isReleased();
  115. }
  116. /**
  117. * Determine if the job has been marked as a failure.
  118. *
  119. * @return bool
  120. */
  121. public function hasFailed()
  122. {
  123. return $this->failed;
  124. }
  125. /**
  126. * Mark the job as "failed".
  127. *
  128. * @return void
  129. */
  130. public function markAsFailed()
  131. {
  132. $this->failed = true;
  133. }
  134. /**
  135. * Process an exception that caused the job to fail.
  136. *
  137. * @param \Exception $e
  138. * @return void
  139. */
  140. public function failed($e)
  141. {
  142. $this->markAsFailed();
  143. $payload = $this->payload();
  144. list($class, $method) = JobName::parse($payload['job']);
  145. if (method_exists($this->instance = $this->resolve($class), 'failed')) {
  146. $this->instance->failed($payload['data'], $e);
  147. }
  148. }
  149. /**
  150. * Resolve the given class.
  151. *
  152. * @param string $class
  153. * @return mixed
  154. */
  155. protected function resolve($class)
  156. {
  157. return $this->container->make($class);
  158. }
  159. /**
  160. * Get the decoded body of the job.
  161. *
  162. * @return array
  163. */
  164. public function payload()
  165. {
  166. return json_decode($this->getRawBody(), true);
  167. }
  168. /**
  169. * Get the number of times to attempt a job.
  170. *
  171. * @return int|null
  172. */
  173. public function maxTries()
  174. {
  175. return $this->payload()['maxTries'] ?? null;
  176. }
  177. /**
  178. * Get the number of seconds the job can run.
  179. *
  180. * @return int|null
  181. */
  182. public function timeout()
  183. {
  184. return $this->payload()['timeout'] ?? null;
  185. }
  186. /**
  187. * Get the timestamp indicating when the job should timeout.
  188. *
  189. * @return int|null
  190. */
  191. public function timeoutAt()
  192. {
  193. return $this->payload()['timeoutAt'] ?? null;
  194. }
  195. /**
  196. * Get the name of the queued job class.
  197. *
  198. * @return string
  199. */
  200. public function getName()
  201. {
  202. return $this->payload()['job'];
  203. }
  204. /**
  205. * Get the resolved name of the queued job class.
  206. *
  207. * Resolves the name of "wrapped" jobs such as class-based handlers.
  208. *
  209. * @return string
  210. */
  211. public function resolveName()
  212. {
  213. return JobName::resolve($this->getName(), $this->payload());
  214. }
  215. /**
  216. * Get the name of the connection the job belongs to.
  217. *
  218. * @return string
  219. */
  220. public function getConnectionName()
  221. {
  222. return $this->connectionName;
  223. }
  224. /**
  225. * Get the name of the queue the job belongs to.
  226. *
  227. * @return string
  228. */
  229. public function getQueue()
  230. {
  231. return $this->queue;
  232. }
  233. /**
  234. * Get the service container instance.
  235. *
  236. * @return \Illuminate\Container\Container
  237. */
  238. public function getContainer()
  239. {
  240. return $this->container;
  241. }
  242. }