Factory method to create the appropriate queue implementation.
(self)
| 125 | logger.info("Queue manager initialized successfully") |
| 126 | |
| 127 | def _create_queue(self) -> QueueInterface: |
| 128 | """Factory method to create the appropriate queue implementation.""" |
| 129 | if config.QUEUE_TYPE == "kafka": |
| 130 | try: |
| 131 | from .kafka_queue import KafkaQueue |
| 132 | |
| 133 | kafka_kwargs = { |
| 134 | "bootstrap_servers": config.KAFKA_BOOTSTRAP_SERVERS, |
| 135 | "topic": config.KAFKA_TOPIC, |
| 136 | "group_id": config.KAFKA_GROUP_ID, |
| 137 | "serialization_format": config.KAFKA_SERIALIZATION_FORMAT, |
| 138 | "security_protocol": config.KAFKA_SECURITY_PROTOCOL, |
| 139 | "auto_offset_reset": config.KAFKA_AUTO_OFFSET_RESET, |
| 140 | "consumer_timeout_ms": config.KAFKA_CONSUMER_TIMEOUT_MS, |
| 141 | "max_poll_interval_ms": config.KAFKA_MAX_POLL_INTERVAL_MS, |
| 142 | "session_timeout_ms": config.KAFKA_SESSION_TIMEOUT_MS, |
| 143 | } |
| 144 | |
| 145 | if config.KAFKA_SSL_CAFILE: |
| 146 | kafka_kwargs["ssl_cafile"] = config.KAFKA_SSL_CAFILE |
| 147 | if config.KAFKA_SSL_CERTFILE: |
| 148 | kafka_kwargs["ssl_certfile"] = config.KAFKA_SSL_CERTFILE |
| 149 | if config.KAFKA_SSL_KEYFILE: |
| 150 | kafka_kwargs["ssl_keyfile"] = config.KAFKA_SSL_KEYFILE |
| 151 | |
| 152 | return KafkaQueue(**kafka_kwargs) |
| 153 | except ImportError as e: |
| 154 | raise ImportError( |
| 155 | f"Kafka queue requested but dependencies not installed: {e}\n" |
| 156 | "Install with: pip install queue-sample[kafka]" |
| 157 | ) from e |
| 158 | else: |
| 159 | if self._num_workers > 1: |
| 160 | mode = "round-robin" if self._round_robin else "hash" |
| 161 | logger.info( |
| 162 | "Using PartitionedMemoryQueue with %d partitions, mode=%s", |
| 163 | self._num_workers, |
| 164 | mode, |
| 165 | ) |
| 166 | return PartitionedMemoryQueue( |
| 167 | num_partitions=self._num_workers, |
| 168 | round_robin=self._round_robin, |
| 169 | ) |
| 170 | else: |
| 171 | return MemoryQueue() |
| 172 | |
| 173 | async def save(self, message: QueueMessage) -> None: |
| 174 | """ |
no test coverage detected