当我访问发布/订阅消息时,它将不会被重新发送

2024-05-16 13:12:53 发布

您现在位置:Python中文网/ 问答频道 /正文

It seems that when I Nack a pub/sub message (message.nack()), pub/sub dumps the message and never retries to send it again. I also do not see an unacknowledged message in the subscription console on google cloud. If I don`t acknowledge the message, pub/sub releases the message after a time and in that case redeliveres it (with the message: Dropping 1 items because they were leased too long).

Service is my own class which will either return the message if it can be published, return 'acknowledge' if it is not relevant (acknowledge without publishing it) or unacknowledge it, if the service it not yet ready to publish it.

I am using the following packages: google-api-core==1.16.0 google-cloud-pubsub==1.5.0 Flask==1.1.2

The whole service runs with flask on a kubernetes cluster.

subscriber = pubsub_v1.SubscriberClient(credentials=credentials)
subscription_path = subscriber.subscription_path(project_id, subscription_name)
def callback(message):
    enriched_message = Service.enrich(promo, message.data, 0)

    # Ckeck if a message has to be published to the topic or not.
    if enriched_message == 'acknowledge':
        message.ack()
    elif enriched_message == 0:
        message.nack()
    elif enriched_message != 0:
        answer = CrmPosService.pub(enriched_message)
        if answer == 'acknowledge':
            message.ack()

try:
    future = subscriber.subscribe(subscription_path, callback=callback)
    # Keep the main thread from exiting so the subscriber can
    # process messages in the background.
    while True:
        time.sleep(1)
except Exception as ex:
    logger.error('Could not subscribe')
    logger.error(ex, exc_info=True)
    raise ex

Tags: thetopathinmessageifgooglecallback