PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php

https://gitlab.com/judielsm/Handora
PHP | 264 lines | 100 code | 37 blank | 127 comment | 7 complexity | 9628616d40da8a75b6249f1c4a7af506 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Queue\Jobs;
  3. use DateTime;
  4. use Illuminate\Support\Str;
  5. abstract class Job
  6. {
  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. * The name of the queue the job belongs to.
  21. *
  22. * @var string
  23. */
  24. protected $queue;
  25. /**
  26. * Indicates if the job has been deleted.
  27. *
  28. * @var bool
  29. */
  30. protected $deleted = false;
  31. /**
  32. * Indicates if the job has been released.
  33. *
  34. * @var bool
  35. */
  36. protected $released = false;
  37. /**
  38. * Fire the job.
  39. *
  40. * @return void
  41. */
  42. abstract public function fire();
  43. /**
  44. * Delete the job from the queue.
  45. *
  46. * @return void
  47. */
  48. public function delete()
  49. {
  50. $this->deleted = true;
  51. }
  52. /**
  53. * Determine if the job has been deleted.
  54. *
  55. * @return bool
  56. */
  57. public function isDeleted()
  58. {
  59. return $this->deleted;
  60. }
  61. /**
  62. * Release the job back into the queue.
  63. *
  64. * @param int $delay
  65. * @return void
  66. */
  67. public function release($delay = 0)
  68. {
  69. $this->released = true;
  70. }
  71. /**
  72. * Determine if the job was released back into the queue.
  73. *
  74. * @return bool
  75. */
  76. public function isReleased()
  77. {
  78. return $this->released;
  79. }
  80. /**
  81. * Determine if the job has been deleted or released.
  82. *
  83. * @return bool
  84. */
  85. public function isDeletedOrReleased()
  86. {
  87. return $this->isDeleted() || $this->isReleased();
  88. }
  89. /**
  90. * Get the number of times the job has been attempted.
  91. *
  92. * @return int
  93. */
  94. abstract public function attempts();
  95. /**
  96. * Get the raw body string for the job.
  97. *
  98. * @return string
  99. */
  100. abstract public function getRawBody();
  101. /**
  102. * Resolve and fire the job handler method.
  103. *
  104. * @param array $payload
  105. * @return void
  106. */
  107. protected function resolveAndFire(array $payload)
  108. {
  109. list($class, $method) = $this->parseJob($payload['job']);
  110. $this->instance = $this->resolve($class);
  111. $this->instance->{$method}($this, $this->resolveQueueableEntities($payload['data']));
  112. }
  113. /**
  114. * Parse the job declaration into class and method.
  115. *
  116. * @param string $job
  117. * @return array
  118. */
  119. protected function parseJob($job)
  120. {
  121. $segments = explode('@', $job);
  122. return count($segments) > 1 ? $segments : [$segments[0], 'fire'];
  123. }
  124. /**
  125. * Resolve the given job handler.
  126. *
  127. * @param string $class
  128. * @return mixed
  129. */
  130. protected function resolve($class)
  131. {
  132. return $this->container->make($class);
  133. }
  134. /**
  135. * Resolve all of the queueable entities in the given payload.
  136. *
  137. * @param mixed $data
  138. * @return mixed
  139. */
  140. protected function resolveQueueableEntities($data)
  141. {
  142. if (is_string($data)) {
  143. return $this->resolveQueueableEntity($data);
  144. }
  145. if (is_array($data)) {
  146. array_walk($data, function (&$d) { $d = $this->resolveQueueableEntity($d); });
  147. }
  148. return $data;
  149. }
  150. /**
  151. * Resolve a single queueable entity from the resolver.
  152. *
  153. * @param mixed $value
  154. * @return \Illuminate\Contracts\Queue\QueueableEntity
  155. */
  156. protected function resolveQueueableEntity($value)
  157. {
  158. if (is_string($value) && Str::startsWith($value, '::entity::')) {
  159. list($marker, $type, $id) = explode('|', $value, 3);
  160. return $this->getEntityResolver()->resolve($type, $id);
  161. }
  162. return $value;
  163. }
  164. /**
  165. * Call the failed method on the job instance.
  166. *
  167. * @return void
  168. */
  169. public function failed()
  170. {
  171. $payload = json_decode($this->getRawBody(), true);
  172. list($class, $method) = $this->parseJob($payload['job']);
  173. $this->instance = $this->resolve($class);
  174. if (method_exists($this->instance, 'failed')) {
  175. $this->instance->failed($this->resolveQueueableEntities($payload['data']));
  176. }
  177. }
  178. /**
  179. * Get an entity resolver instance.
  180. *
  181. * @return \Illuminate\Contracts\Queue\EntityResolver
  182. */
  183. protected function getEntityResolver()
  184. {
  185. return $this->container->make('Illuminate\Contracts\Queue\EntityResolver');
  186. }
  187. /**
  188. * Calculate the number of seconds with the given delay.
  189. *
  190. * @param \DateTime|int $delay
  191. * @return int
  192. */
  193. protected function getSeconds($delay)
  194. {
  195. if ($delay instanceof DateTime) {
  196. return max(0, $delay->getTimestamp() - $this->getTime());
  197. }
  198. return (int) $delay;
  199. }
  200. /**
  201. * Get the current system time.
  202. *
  203. * @return int
  204. */
  205. protected function getTime()
  206. {
  207. return time();
  208. }
  209. /**
  210. * Get the name of the queued job class.
  211. *
  212. * @return string
  213. */
  214. public function getName()
  215. {
  216. return json_decode($this->getRawBody(), true)['job'];
  217. }
  218. /**
  219. * Get the name of the queue the job belongs to.
  220. *
  221. * @return string
  222. */
  223. public function getQueue()
  224. {
  225. return $this->queue;
  226. }
  227. }