maansthoernvik / rabbitmq_client

Client for a RabbitMQ server.
MIT License
9 stars 5 forks source link

Ack the message manually #9

Closed ksrigo closed 2 years ago

ksrigo commented 2 years ago

Hi,

this is my code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import signal
import logging
import threading
import sys
from logging.handlers import RotatingFileHandler
import os
from lib.db import get_session
from pika.exchange_type import ExchangeType
from rabbitmq_client import RMQConsumer, ConsumeParams, QueueParams, ExchangeParams, ConsumeOK
from settings import config

#from parse_acc import Accounting

def on_msg(message):
    if isinstance(message, ConsumeOK):
        logging.info("GOT CONSUME OK")
    else:
        print(f"GOT MESSAGE: {message}")

if __name__ == '__main__':
    try:
        logging.basicConfig(
            handlers=[RotatingFileHandler(config.LOG_FILENAME, maxBytes=500000, backupCount=10)],
            level=config.NUMERIC_LOG_LEVEL,
            format="[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s")
    except:pass
    logging.info("Started main pid:"+str(os.getpid()))
    consumer = RMQConsumer(config.RMQ_CONNECTION)
    consumer.start()
    consumer.consume(
        ConsumeParams(on_msg),
        exchange_params=ExchangeParams(config.RMQ_EXCHANGE, exchange_type=ExchangeType.topic, durable=True),
        queue_params=QueueParams(config.RMQ_QUEUE, durable=True, arguments=config.RMQ_ARGUMENTS),
        routing_key=config.RMQ_RKEY
    )

    def stop(*args):
        consumer.stop()
        sys.exit(0)

    signal.signal(signal.SIGINT, stop)

    threading.Event().wait()

I would like to able to ack or not the message according my logic. The acknowledgement part seems to be hard coded: https://github.com/maansthoernvik/rabbitmq_client/blob/568fef5d388dddcdce5eb8fc6175fc38d8ede9d3/rabbitmq_client/consumer.py#L352

Thank you

maansthoernvik commented 2 years ago

Very good! Thanks for submitting this issue, I will get to work on this during next week. You are right, the acknowledgement is hard-coded, I will change this so that you as a user can choose when to acknowledge a consumed message.

maansthoernvik commented 2 years ago

PR is up, implementation and testing is done. Finalizing solution and documenting

ksrigo commented 2 years ago

thank you