/** * @param EventManagerInterface $eventManager * @param StrategyPluginManager $listenerPluginManager * @param array $strategyConfig * @throws RuntimeException */ protected function attachWorkerListeners(EventManagerInterface $eventManager, StrategyPluginManager $listenerPluginManager, array $strategyConfig = []) { foreach ($strategyConfig as $strategy => $options) { // no options given, name stored as value if (is_numeric($strategy) && is_string($options)) { $strategy = $options; $options = []; } if (!is_string($strategy) || !is_array($options)) { continue; } $priority = null; if (isset($options['priority'])) { $priority = $options['priority']; unset($options['priority']); } $listener = $listenerPluginManager->get($strategy, $options); if (!is_null($priority)) { $eventManager->attachAggregate($listener, $priority); } else { $eventManager->attachAggregate($listener); } } if (!in_array(WorkerEvent::EVENT_BOOTSTRAP, $eventManager->getEvents())) { throw new RuntimeException(sprintf("No worker strategy has been registered to respond to the '%s' event.", WorkerEvent::EVENT_BOOTSTRAP)); } }
/** * {@inheritDoc} */ public function createService(ServiceLocatorInterface $serviceLocator) { $config = $serviceLocator->get('Config'); $config = $config['slm_queue']['strategy_manager']; $listenerPluginManager = new StrategyPluginManager(new Config($config)); $listenerPluginManager->setServiceLocator($serviceLocator); return $listenerPluginManager; }
/** * @param WorkerEvent $e * @throws \SlmQueue\Exception\RuntimeException */ public function attachQueueListeners(WorkerEvent $e) { /** @var AbstractWorker $worker */ $worker = $e->getTarget(); $name = $e->getQueue()->getName(); $eventManager = $worker->getEventManager(); $eventManager->detachAggregate($this); if (!isset($this->strategyConfig[$name])) { $name = 'default'; // We want to make sure the default process queue is always attached } $strategies = $this->strategyConfig[$name]; foreach ($strategies as $strategy => $options) { // no options given, name stored as value if (is_numeric($strategy) && is_string($options)) { $strategy = $options; $options = array(); } if (!is_string($strategy) || !is_array($options)) { continue; } $priority = null; if (isset($options['priority'])) { $priority = $options['priority']; unset($options['priority']); } $listener = $this->pluginManager->get($strategy, $options); if (!is_null($priority)) { $eventManager->attachAggregate($listener, $priority); } else { $eventManager->attachAggregate($listener); } } if (!in_array(WorkerEvent::EVENT_PROCESS_QUEUE, $eventManager->getEvents())) { throw new RuntimeException(sprintf( "No worker strategy has been registered to respond to the '%s' event.", WorkerEvent::EVENT_PROCESS_QUEUE )); } $e->stopPropagation(); $eventManager->trigger(WorkerEvent::EVENT_BOOTSTRAP, $e); }