Incorrect message_count from RabbitMQ

Viewed 55

I have some actions that consume lots of resources, so I decided to queue them. The functionality works fine, but I wanted to show the number of messages somewhere on the client.

It is done within GraphQL subscription, so I get the current status every 10s. The problem is after using queue_declare() from Kombu.

from kombu import Connection

def get_queue_message_count(channel, queue: str) -> int:
    _1, message_count, _2 = channel.queue_declare(
        queue=queue, passive=True, durable=True, auto_delete=False, arguments={"x-max-priority": 10}
    )
    print(f"{queue}: {message_count}")

    return message_count


async def generate_queue_status(_obj, _info) -> AsyncGenerator[Dict[str, Any], None]:
    with Connection(app_config.queue.broker) as connection:
        with connection.channel() as channel:
            while True:
                message_count = get_queue_message_count(channel, "actions")
                yield {"error": None, "messages": message_count}
                await asyncio.sleep(10)

For the 1st time I get correct number, for the rest it is 0 even though the total number of messages in the management tool is greater than 0:

Queue status

0 Answers
Related