QueueServiceProvider.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace Illuminate\Queue;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Queue\Connectors\SqsConnector;
  5. use Illuminate\Queue\Connectors\NullConnector;
  6. use Illuminate\Queue\Connectors\SyncConnector;
  7. use Illuminate\Queue\Connectors\RedisConnector;
  8. use Illuminate\Contracts\Debug\ExceptionHandler;
  9. use Illuminate\Queue\Connectors\DatabaseConnector;
  10. use Illuminate\Queue\Failed\NullFailedJobProvider;
  11. use Illuminate\Queue\Connectors\BeanstalkdConnector;
  12. use Illuminate\Queue\Failed\DatabaseFailedJobProvider;
  13. class QueueServiceProvider extends ServiceProvider
  14. {
  15. /**
  16. * Indicates if loading of the provider is deferred.
  17. *
  18. * @var bool
  19. */
  20. protected $defer = true;
  21. /**
  22. * Register the service provider.
  23. *
  24. * @return void
  25. */
  26. public function register()
  27. {
  28. $this->registerManager();
  29. $this->registerConnection();
  30. $this->registerWorker();
  31. $this->registerListener();
  32. $this->registerFailedJobServices();
  33. }
  34. /**
  35. * Register the queue manager.
  36. *
  37. * @return void
  38. */
  39. protected function registerManager()
  40. {
  41. $this->app->singleton('queue', function ($app) {
  42. // Once we have an instance of the queue manager, we will register the various
  43. // resolvers for the queue connectors. These connectors are responsible for
  44. // creating the classes that accept queue configs and instantiate queues.
  45. return tap(new QueueManager($app), function ($manager) {
  46. $this->registerConnectors($manager);
  47. });
  48. });
  49. }
  50. /**
  51. * Register the default queue connection binding.
  52. *
  53. * @return void
  54. */
  55. protected function registerConnection()
  56. {
  57. $this->app->singleton('queue.connection', function ($app) {
  58. return $app['queue']->connection();
  59. });
  60. }
  61. /**
  62. * Register the connectors on the queue manager.
  63. *
  64. * @param \Illuminate\Queue\QueueManager $manager
  65. * @return void
  66. */
  67. public function registerConnectors($manager)
  68. {
  69. foreach (['Null', 'Sync', 'Database', 'Redis', 'Beanstalkd', 'Sqs'] as $connector) {
  70. $this->{"register{$connector}Connector"}($manager);
  71. }
  72. }
  73. /**
  74. * Register the Null queue connector.
  75. *
  76. * @param \Illuminate\Queue\QueueManager $manager
  77. * @return void
  78. */
  79. protected function registerNullConnector($manager)
  80. {
  81. $manager->addConnector('null', function () {
  82. return new NullConnector;
  83. });
  84. }
  85. /**
  86. * Register the Sync queue connector.
  87. *
  88. * @param \Illuminate\Queue\QueueManager $manager
  89. * @return void
  90. */
  91. protected function registerSyncConnector($manager)
  92. {
  93. $manager->addConnector('sync', function () {
  94. return new SyncConnector;
  95. });
  96. }
  97. /**
  98. * Register the database queue connector.
  99. *
  100. * @param \Illuminate\Queue\QueueManager $manager
  101. * @return void
  102. */
  103. protected function registerDatabaseConnector($manager)
  104. {
  105. $manager->addConnector('database', function () {
  106. return new DatabaseConnector($this->app['db']);
  107. });
  108. }
  109. /**
  110. * Register the Redis queue connector.
  111. *
  112. * @param \Illuminate\Queue\QueueManager $manager
  113. * @return void
  114. */
  115. protected function registerRedisConnector($manager)
  116. {
  117. $manager->addConnector('redis', function () {
  118. return new RedisConnector($this->app['redis']);
  119. });
  120. }
  121. /**
  122. * Register the Beanstalkd queue connector.
  123. *
  124. * @param \Illuminate\Queue\QueueManager $manager
  125. * @return void
  126. */
  127. protected function registerBeanstalkdConnector($manager)
  128. {
  129. $manager->addConnector('beanstalkd', function () {
  130. return new BeanstalkdConnector;
  131. });
  132. }
  133. /**
  134. * Register the Amazon SQS queue connector.
  135. *
  136. * @param \Illuminate\Queue\QueueManager $manager
  137. * @return void
  138. */
  139. protected function registerSqsConnector($manager)
  140. {
  141. $manager->addConnector('sqs', function () {
  142. return new SqsConnector;
  143. });
  144. }
  145. /**
  146. * Register the queue worker.
  147. *
  148. * @return void
  149. */
  150. protected function registerWorker()
  151. {
  152. $this->app->singleton('queue.worker', function () {
  153. return new Worker(
  154. $this->app['queue'], $this->app['events'], $this->app[ExceptionHandler::class]
  155. );
  156. });
  157. }
  158. /**
  159. * Register the queue listener.
  160. *
  161. * @return void
  162. */
  163. protected function registerListener()
  164. {
  165. $this->app->singleton('queue.listener', function () {
  166. return new Listener($this->app->basePath());
  167. });
  168. }
  169. /**
  170. * Register the failed job services.
  171. *
  172. * @return void
  173. */
  174. protected function registerFailedJobServices()
  175. {
  176. $this->app->singleton('queue.failer', function () {
  177. $config = $this->app['config']['queue.failed'];
  178. return isset($config['table'])
  179. ? $this->databaseFailedJobProvider($config)
  180. : new NullFailedJobProvider;
  181. });
  182. }
  183. /**
  184. * Create a new database failed job provider.
  185. *
  186. * @param array $config
  187. * @return \Illuminate\Queue\Failed\DatabaseFailedJobProvider
  188. */
  189. protected function databaseFailedJobProvider($config)
  190. {
  191. return new DatabaseFailedJobProvider(
  192. $this->app['db'], $config['database'], $config['table']
  193. );
  194. }
  195. /**
  196. * Get the services provided by the provider.
  197. *
  198. * @return array
  199. */
  200. public function provides()
  201. {
  202. return [
  203. 'queue', 'queue.worker', 'queue.listener',
  204. 'queue.failer', 'queue.connection',
  205. ];
  206. }
  207. }