从pubsub提取消息而不会超时 [英] Pull messages from pubsub without timeout

查看:101
本文介绍了从pubsub提取消息而不会超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施,到目前为止每个示例Google都具有超时功能,该功能会在1分钟后停止监听消息.

I'm trying to implement this and so far for every example Google has a timeout function that stops listenting for messages after 1 minute.

  setTimeout(() => {
    subscription.removeListener('message', messageHandler);
    console.log(`${messageCount} message(s) received.`);
  }, timeout * 1000)

比方说,我想永远听新消息.没有超时功能的不利之处是什么?还是最好无限循环?

Let's say I want to listen forever for new messages. What's the downside of not having the timeout function? Or would rather be better to just loop infinitely?

我正在本地测试,没关系,但是我想知道在生产模式下会发生什么.是的,我想发送消息.

I'm testing locally and that's ok, but I'm wondering about what could happen in a production mode. And yes, I want to PULL messages.

推荐答案

设置超时时间以删除消息侦听器.如果您想不确定地收听,请删除这4条线,仅此而已!

The timeout is set to remove the message listener. If you want to listen indefinitively, remove these 4 lines, and that's all!

但是,请问一下自己如何停止优雅地聆听?"如果出现抢占计算引擎,停止应用程序或VM的情况,请卸载实例(适用于Cloud Run,Cloud Functions和App Engine).

However, ask yourselves about "How to stop listening gracefully?" In case of compute engine preemption, app or VM stop, unload instances (for Cloud Run, Cloud Functions and App Engine).

这里是超时.您可以想象一个while循环并检查一个布尔值以继续还是停止.

Here the trigger it's a timeout. You can imagine a while loop and check a boolean to continue or to stop.

这篇关于从pubsub提取消息而不会超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆