|
3 | 3 | import logging |
4 | 4 | import pika |
5 | 5 | import config |
6 | | -from typing import List |
| 6 | +import traceback |
| 7 | +import signal |
| 8 | +from typing import List, Optional |
7 | 9 | from emf.common.config_parser import parse_app_properties |
8 | 10 | from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor |
9 | 11 |
|
@@ -158,6 +160,190 @@ def __del__(self): |
158 | 160 | self.close() |
159 | 161 |
|
160 | 162 |
|
| 163 | +class SingleMessageConsumer: |
| 164 | + def __init__(self, |
| 165 | + host: str = RMQ_SERVER, |
| 166 | + port: int = int(RMQ_PORT), |
| 167 | + vhost: str = RMQ_VHOST, |
| 168 | + queue: str | None = None, |
| 169 | + username: str = RMQ_USERNAME, |
| 170 | + password: str = RMQ_PASSWORD, |
| 171 | + forward: Optional[str] = None, |
| 172 | + message_handlers: Optional[List[object]] = None, |
| 173 | + message_converter: Optional[object] = None, |
| 174 | + heartbeat: int = int(RMQ_HEARTBEAT_IN_SEC), |
| 175 | + socket_timeout: Optional[float] = None, |
| 176 | + blocked_connection_timeout: float = 600.0, |
| 177 | + connection_attempts: int = 5, |
| 178 | + retry_delay: int = 3, |
| 179 | + log_body: bool = False): |
| 180 | + self._host, self._port, self._vhost = host, int(port), vhost |
| 181 | + self._queue = queue |
| 182 | + self._username, self._password = username, password |
| 183 | + self.forward = forward |
| 184 | + self.message_handlers = message_handlers or [] |
| 185 | + self.message_converter = message_converter |
| 186 | + self.log_body = log_body |
| 187 | + |
| 188 | + self._heartbeat = heartbeat |
| 189 | + self._socket_timeout = socket_timeout |
| 190 | + self._blocked_connection_timeout = blocked_connection_timeout |
| 191 | + self._connection_attempts = connection_attempts |
| 192 | + self._retry_delay = retry_delay |
| 193 | + |
| 194 | + self._connection: Optional[pika.BlockingConnection] = None |
| 195 | + self._channel: Optional[pika.adapters.blocking_connection.BlockingChannel] = None |
| 196 | + self._in_shutdown = False |
| 197 | + |
| 198 | + self._executor = ThreadPoolExecutor(max_workers=1) |
| 199 | + |
| 200 | + signal.signal(signal.SIGTERM, self._on_term_signal) |
| 201 | + signal.signal(signal.SIGINT, self._on_term_signal) |
| 202 | + |
| 203 | + def _params(self) -> pika.ConnectionParameters: |
| 204 | + return pika.ConnectionParameters( |
| 205 | + host=self._host, |
| 206 | + port=self._port, |
| 207 | + virtual_host=self._vhost, |
| 208 | + credentials=pika.PlainCredentials(self._username, self._password), |
| 209 | + heartbeat=self._heartbeat, |
| 210 | + blocked_connection_timeout=self._blocked_connection_timeout, |
| 211 | + connection_attempts=self._connection_attempts, |
| 212 | + retry_delay=self._retry_delay, |
| 213 | + socket_timeout=self._socket_timeout, |
| 214 | + client_properties={"connection_name": "keda-single-shot"}, |
| 215 | + ) |
| 216 | + |
| 217 | + def connect(self): |
| 218 | + logger.info(f"Connecting to RabbitMQ at {self._host}:{self._port} vhost='{self._vhost}'") |
| 219 | + self._connection = pika.BlockingConnection(self._params()) |
| 220 | + self._channel = self._connection.channel() |
| 221 | + logger.info("Connection established and channel opened") |
| 222 | + |
| 223 | + def close(self): |
| 224 | + try: |
| 225 | + if self._channel and self._channel.is_open: |
| 226 | + self._channel.close() |
| 227 | + except Exception as e: |
| 228 | + logger.warning(f"Error closing channel: {e}") |
| 229 | + try: |
| 230 | + if self._connection and self._connection.is_open: |
| 231 | + self._connection.close() |
| 232 | + except Exception as e: |
| 233 | + logger.warning(f"Error closing connection: {e}") |
| 234 | + self._executor.shutdown(wait=False, cancel_futures=True) |
| 235 | + |
| 236 | + def _on_term_signal(self, signum, _frame): |
| 237 | + self._in_shutdown = True |
| 238 | + logger.warning(f"Received signal {signum}; will exit after current message finishes.") |
| 239 | + |
| 240 | + # -------- worker function (no channel ops here) -------- |
| 241 | + def _process_messages(self, basic_deliver, properties, body): |
| 242 | + ack = True |
| 243 | + err = None |
| 244 | + |
| 245 | + # Convert if needed |
| 246 | + if self.message_converter: |
| 247 | + try: |
| 248 | + body, content_type = self.message_converter.convert(body) |
| 249 | + if properties is None: |
| 250 | + properties = pika.BasicProperties(content_type=content_type) |
| 251 | + else: |
| 252 | + properties.content_type = content_type |
| 253 | + logger.info("Message converted") |
| 254 | + except Exception as error: |
| 255 | + logger.error(f"Message conversion failed: {error}\n{traceback.format_exc()}") |
| 256 | + ack = False |
| 257 | + err = error |
| 258 | + |
| 259 | + # Handlers |
| 260 | + if ack and self.message_handlers: |
| 261 | + for message_handler in self.message_handlers: |
| 262 | + try: |
| 263 | + logger.info(f"Handling message with handler: {message_handler.__class__.__name__}") |
| 264 | + body, properties = message_handler.handle(body, properties=properties, channel=None) |
| 265 | + except Exception as error: |
| 266 | + logger.error(f"Message handling failed: {error}\n{traceback.format_exc()}") |
| 267 | + logger.exception("Message handling failed, see traceback in document") |
| 268 | + ack = False |
| 269 | + err = error |
| 270 | + break |
| 271 | + |
| 272 | + return ack, body, properties, err, basic_deliver.delivery_tag |
| 273 | + |
| 274 | + # -------- single-message main -------- |
| 275 | + def run(self) -> int: |
| 276 | + """ |
| 277 | + Exit codes: |
| 278 | + 0 -> processed OK or queue empty |
| 279 | + 2 -> conversion/handler failed (rejected) |
| 280 | + 3 -> connection/setup error |
| 281 | + """ |
| 282 | + try: |
| 283 | + self.connect() |
| 284 | + except Exception as e: |
| 285 | + logger.error(f"Failed to connect to RabbitMQ: {e}") |
| 286 | + return 3 |
| 287 | + |
| 288 | + try: |
| 289 | + method, properties, body = self._channel.basic_get(self._queue, auto_ack=False) |
| 290 | + if not method: |
| 291 | + logger.warning(f"No message available in queue '{self._queue}', exiting") |
| 292 | + return 0 |
| 293 | + |
| 294 | + delivery_tag = method.delivery_tag |
| 295 | + logger.info(f"Received message #{delivery_tag} from {getattr(properties,'app_id',None)} meta: {getattr(properties,'headers',None)}") |
| 296 | + if self.log_body: |
| 297 | + logger.debug(f"Message body: {body!r}") |
| 298 | + |
| 299 | + future = self._executor.submit(self._process_messages, method, properties, body) |
| 300 | + |
| 301 | + # keep heartbeats flowing while waiting for worker completion |
| 302 | + while not future.done(): |
| 303 | + try: |
| 304 | + self._connection.process_data_events(time_limit=0) |
| 305 | + except Exception: |
| 306 | + pass |
| 307 | + time.sleep(0.25) |
| 308 | + |
| 309 | + ack, out_body, out_props, err, dtag = future.result() |
| 310 | + |
| 311 | + # Check if properties has some status flag set from handler |
| 312 | + _success = out_props.headers.get('success', True) |
| 313 | + |
| 314 | + if not ack or not _success: |
| 315 | + logger.warning(f"Rejecting message due to handler failure or success flag: {_success}, error: {err}") |
| 316 | + try: |
| 317 | + self._channel.basic_reject(dtag, requeue=False) |
| 318 | + except Exception as e: |
| 319 | + logger.error(f"Failed to REJECT message #{dtag}: {e}") |
| 320 | + return 3 |
| 321 | + return 2 |
| 322 | + |
| 323 | + if self.forward: |
| 324 | + logger.info(f"Publishing message to exchange/queue: {self.forward}") |
| 325 | + try: |
| 326 | + self._channel.basic_publish( |
| 327 | + exchange=self.forward, routing_key="", body=out_body, properties=out_props |
| 328 | + ) |
| 329 | + except Exception as e: |
| 330 | + logger.error(f"Publish failed: {e}") |
| 331 | + return 3 # leave unacked for redelivery |
| 332 | + |
| 333 | + try: |
| 334 | + self._channel.basic_ack(dtag) |
| 335 | + logger.info(f"ACKed message #{dtag}") |
| 336 | + if self._in_shutdown: |
| 337 | + logger.info("Shutdown requested; exiting cleanly after finishing message") |
| 338 | + return 0 |
| 339 | + except Exception as e: |
| 340 | + logger.error(f"Failed to ACK message #{dtag}: {e}") |
| 341 | + return 3 |
| 342 | + |
| 343 | + finally: |
| 344 | + self.close() |
| 345 | + |
| 346 | + |
161 | 347 | class RMQConsumer: |
162 | 348 | """This is an example consumer that will handle unexpected interactions |
163 | 349 | with RabbitMQ such as channel and connection closures. |
|
0 commit comments