|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DesignMyNight\Laravel\Horizon\Connectors; |
| 4 | + |
| 5 | +use DesignMyNight\Laravel\Horizon\RabbitMQQueue; |
| 6 | +use Enqueue\AmqpTools\DelayStrategyAware; |
| 7 | +use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy; |
| 8 | +use Illuminate\Contracts\Events\Dispatcher; |
| 9 | +use Illuminate\Contracts\Queue\Queue; |
| 10 | +use Illuminate\Queue\Events\WorkerStopping; |
| 11 | +use Interop\Amqp\AmqpConnectionFactory as InteropAmqpConnectionFactory; |
| 12 | +use Interop\Amqp\AmqpConnectionFactory; |
| 13 | +use Interop\Amqp\AmqpContext; |
| 14 | +use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector as BaseConnector; |
| 15 | + |
| 16 | +class RabbitMQConnector extends BaseConnector |
| 17 | +{ |
| 18 | + protected $dispatcher; |
| 19 | + |
| 20 | + public function __construct(Dispatcher $dispatcher) |
| 21 | + { |
| 22 | + $this->dispatcher = $dispatcher; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @inheritDoc |
| 27 | + */ |
| 28 | + public function connect(array $config): Queue |
| 29 | + { |
| 30 | + if (false === array_key_exists('factory_class', $config)) { |
| 31 | + throw new \LogicException('The factory_class option is missing though it is required.'); |
| 32 | + } |
| 33 | + |
| 34 | + $factoryClass = $config['factory_class']; |
| 35 | + if (false === class_exists($factoryClass) || false === (new \ReflectionClass($factoryClass))->implementsInterface(InteropAmqpConnectionFactory::class)) { |
| 36 | + throw new \LogicException(sprintf('The factory_class option has to be valid class that implements "%s"', InteropAmqpConnectionFactory::class)); |
| 37 | + } |
| 38 | + |
| 39 | + /** @var AmqpConnectionFactory $factory */ |
| 40 | + $factory = new $factoryClass([ |
| 41 | + 'dsn' => $config['dsn'], |
| 42 | + 'host' => $config['host'], |
| 43 | + 'port' => $config['port'], |
| 44 | + 'user' => $config['login'], |
| 45 | + 'pass' => $config['password'], |
| 46 | + 'vhost' => $config['vhost'], |
| 47 | + 'ssl_on' => $config['ssl_params']['ssl_on'], |
| 48 | + 'ssl_verify' => $config['ssl_params']['verify_peer'], |
| 49 | + 'ssl_cacert' => $config['ssl_params']['cafile'], |
| 50 | + 'ssl_cert' => $config['ssl_params']['local_cert'], |
| 51 | + 'ssl_key' => $config['ssl_params']['local_key'], |
| 52 | + 'ssl_passphrase' => $config['ssl_params']['passphrase'], |
| 53 | + ]); |
| 54 | + |
| 55 | + if ($factory instanceof DelayStrategyAware) { |
| 56 | + $factory->setDelayStrategy(new RabbitMqDlxDelayStrategy()); |
| 57 | + } |
| 58 | + |
| 59 | + /** @var AmqpContext $context */ |
| 60 | + $context = $factory->createContext(); |
| 61 | + |
| 62 | + $this->dispatcher->listen(WorkerStopping::class, function () use ($context) { |
| 63 | + $context->close(); |
| 64 | + }); |
| 65 | + |
| 66 | + return new RabbitMQQueue($context, $config); |
| 67 | + } |
| 68 | +} |
0 commit comments