QueueManager.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace Illuminate\Queue;
  3. use Closure;
  4. use InvalidArgumentException;
  5. use Illuminate\Contracts\Queue\Factory as FactoryContract;
  6. use Illuminate\Contracts\Queue\Monitor as MonitorContract;
  7. /**
  8. * @mixin \Illuminate\Contracts\Queue\Queue
  9. */
  10. class QueueManager implements FactoryContract, MonitorContract
  11. {
  12. /**
  13. * The application instance.
  14. *
  15. * @var \Illuminate\Foundation\Application
  16. */
  17. protected $app;
  18. /**
  19. * The array of resolved queue connections.
  20. *
  21. * @var array
  22. */
  23. protected $connections = [];
  24. /**
  25. * The array of resolved queue connectors.
  26. *
  27. * @var array
  28. */
  29. protected $connectors = [];
  30. /**
  31. * Create a new queue manager instance.
  32. *
  33. * @param \Illuminate\Foundation\Application $app
  34. * @return void
  35. */
  36. public function __construct($app)
  37. {
  38. $this->app = $app;
  39. }
  40. /**
  41. * Register an event listener for the before job event.
  42. *
  43. * @param mixed $callback
  44. * @return void
  45. */
  46. public function before($callback)
  47. {
  48. $this->app['events']->listen(Events\JobProcessing::class, $callback);
  49. }
  50. /**
  51. * Register an event listener for the after job event.
  52. *
  53. * @param mixed $callback
  54. * @return void
  55. */
  56. public function after($callback)
  57. {
  58. $this->app['events']->listen(Events\JobProcessed::class, $callback);
  59. }
  60. /**
  61. * Register an event listener for the exception occurred job event.
  62. *
  63. * @param mixed $callback
  64. * @return void
  65. */
  66. public function exceptionOccurred($callback)
  67. {
  68. $this->app['events']->listen(Events\JobExceptionOccurred::class, $callback);
  69. }
  70. /**
  71. * Register an event listener for the daemon queue loop.
  72. *
  73. * @param mixed $callback
  74. * @return void
  75. */
  76. public function looping($callback)
  77. {
  78. $this->app['events']->listen(Events\Looping::class, $callback);
  79. }
  80. /**
  81. * Register an event listener for the failed job event.
  82. *
  83. * @param mixed $callback
  84. * @return void
  85. */
  86. public function failing($callback)
  87. {
  88. $this->app['events']->listen(Events\JobFailed::class, $callback);
  89. }
  90. /**
  91. * Register an event listener for the daemon queue stopping.
  92. *
  93. * @param mixed $callback
  94. * @return void
  95. */
  96. public function stopping($callback)
  97. {
  98. $this->app['events']->listen(Events\WorkerStopping::class, $callback);
  99. }
  100. /**
  101. * Determine if the driver is connected.
  102. *
  103. * @param string $name
  104. * @return bool
  105. */
  106. public function connected($name = null)
  107. {
  108. return isset($this->connections[$name ?: $this->getDefaultDriver()]);
  109. }
  110. /**
  111. * Resolve a queue connection instance.
  112. *
  113. * @param string $name
  114. * @return \Illuminate\Contracts\Queue\Queue
  115. */
  116. public function connection($name = null)
  117. {
  118. $name = $name ?: $this->getDefaultDriver();
  119. // If the connection has not been resolved yet we will resolve it now as all
  120. // of the connections are resolved when they are actually needed so we do
  121. // not make any unnecessary connection to the various queue end-points.
  122. if (! isset($this->connections[$name])) {
  123. $this->connections[$name] = $this->resolve($name);
  124. $this->connections[$name]->setContainer($this->app);
  125. }
  126. return $this->connections[$name];
  127. }
  128. /**
  129. * Resolve a queue connection.
  130. *
  131. * @param string $name
  132. * @return \Illuminate\Contracts\Queue\Queue
  133. */
  134. protected function resolve($name)
  135. {
  136. $config = $this->getConfig($name);
  137. return $this->getConnector($config['driver'])
  138. ->connect($config)
  139. ->setConnectionName($name);
  140. }
  141. /**
  142. * Get the connector for a given driver.
  143. *
  144. * @param string $driver
  145. * @return \Illuminate\Queue\Connectors\ConnectorInterface
  146. *
  147. * @throws \InvalidArgumentException
  148. */
  149. protected function getConnector($driver)
  150. {
  151. if (! isset($this->connectors[$driver])) {
  152. throw new InvalidArgumentException("No connector for [$driver]");
  153. }
  154. return call_user_func($this->connectors[$driver]);
  155. }
  156. /**
  157. * Add a queue connection resolver.
  158. *
  159. * @param string $driver
  160. * @param \Closure $resolver
  161. * @return void
  162. */
  163. public function extend($driver, Closure $resolver)
  164. {
  165. return $this->addConnector($driver, $resolver);
  166. }
  167. /**
  168. * Add a queue connection resolver.
  169. *
  170. * @param string $driver
  171. * @param \Closure $resolver
  172. * @return void
  173. */
  174. public function addConnector($driver, Closure $resolver)
  175. {
  176. $this->connectors[$driver] = $resolver;
  177. }
  178. /**
  179. * Get the queue connection configuration.
  180. *
  181. * @param string $name
  182. * @return array
  183. */
  184. protected function getConfig($name)
  185. {
  186. if (! is_null($name) && $name !== 'null') {
  187. return $this->app['config']["queue.connections.{$name}"];
  188. }
  189. return ['driver' => 'null'];
  190. }
  191. /**
  192. * Get the name of the default queue connection.
  193. *
  194. * @return string
  195. */
  196. public function getDefaultDriver()
  197. {
  198. return $this->app['config']['queue.default'];
  199. }
  200. /**
  201. * Set the name of the default queue connection.
  202. *
  203. * @param string $name
  204. * @return void
  205. */
  206. public function setDefaultDriver($name)
  207. {
  208. $this->app['config']['queue.default'] = $name;
  209. }
  210. /**
  211. * Get the full name for the given connection.
  212. *
  213. * @param string $connection
  214. * @return string
  215. */
  216. public function getName($connection = null)
  217. {
  218. return $connection ?: $this->getDefaultDriver();
  219. }
  220. /**
  221. * Determine if the application is in maintenance mode.
  222. *
  223. * @return bool
  224. */
  225. public function isDownForMaintenance()
  226. {
  227. return $this->app->isDownForMaintenance();
  228. }
  229. /**
  230. * Dynamically pass calls to the default connection.
  231. *
  232. * @param string $method
  233. * @param array $parameters
  234. * @return mixed
  235. */
  236. public function __call($method, $parameters)
  237. {
  238. return $this->connection()->$method(...$parameters);
  239. }
  240. }