BusServiceProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Illuminate\Bus;
  3. use Aws\DynamoDb\DynamoDbClient;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Contracts\Bus\Dispatcher as DispatcherContract;
  6. use Illuminate\Contracts\Bus\QueueingDispatcher as QueueingDispatcherContract;
  7. use Illuminate\Contracts\Queue\Factory as QueueFactoryContract;
  8. use Illuminate\Contracts\Support\DeferrableProvider;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\ServiceProvider;
  11. class BusServiceProvider extends ServiceProvider implements DeferrableProvider
  12. {
  13. /**
  14. * Register the service provider.
  15. *
  16. * @return void
  17. */
  18. public function register()
  19. {
  20. $this->app->singleton(Dispatcher::class, function ($app) {
  21. return new Dispatcher($app, function ($connection = null) {
  22. return Container::getInstance()->make(QueueFactoryContract::class)->connection($connection);
  23. });
  24. });
  25. $this->registerBatchServices();
  26. $this->app->alias(
  27. Dispatcher::class, DispatcherContract::class
  28. );
  29. $this->app->alias(
  30. Dispatcher::class, QueueingDispatcherContract::class
  31. );
  32. }
  33. /**
  34. * Register the batch handling services.
  35. *
  36. * @return void
  37. */
  38. protected function registerBatchServices()
  39. {
  40. $this->app->singleton(BatchRepository::class, function ($app) {
  41. $driver = $app->config->get('queue.batching.driver', 'database');
  42. return $driver === 'dynamodb'
  43. ? $app->make(DynamoBatchRepository::class)
  44. : $app->make(DatabaseBatchRepository::class);
  45. });
  46. $this->app->singleton(DatabaseBatchRepository::class, function ($app) {
  47. return new DatabaseBatchRepository(
  48. $app->make(BatchFactory::class),
  49. $app->make('db')->connection($app->config->get('queue.batching.database')),
  50. $app->config->get('queue.batching.table', 'job_batches')
  51. );
  52. });
  53. $this->app->singleton(DynamoBatchRepository::class, function ($app) {
  54. $config = $app->config->get('queue.batching');
  55. $dynamoConfig = [
  56. 'region' => $config['region'],
  57. 'version' => 'latest',
  58. 'endpoint' => $config['endpoint'] ?? null,
  59. ];
  60. if (! empty($config['key']) && ! empty($config['secret'])) {
  61. $dynamoConfig['credentials'] = Arr::only($config, ['key', 'secret']);
  62. if (! empty($config['token'])) {
  63. $dynamoConfig['credentials']['token'] = $config['token'];
  64. }
  65. }
  66. return new DynamoBatchRepository(
  67. $app->make(BatchFactory::class),
  68. new DynamoDbClient($dynamoConfig),
  69. $app->config->get('app.name'),
  70. $app->config->get('queue.batching.table', 'job_batches'),
  71. ttl: $app->config->get('queue.batching.ttl', null),
  72. ttlAttribute: $app->config->get('queue.batching.ttl_attribute', 'ttl'),
  73. );
  74. });
  75. }
  76. /**
  77. * Get the services provided by the provider.
  78. *
  79. * @return array
  80. */
  81. public function provides()
  82. {
  83. return [
  84. Dispatcher::class,
  85. DispatcherContract::class,
  86. QueueingDispatcherContract::class,
  87. BatchRepository::class,
  88. DatabaseBatchRepository::class,
  89. ];
  90. }
  91. }