代码之家  ›  专栏  ›  技术社区  ›  Forepick

NodeJS:处理云Pub/Sub的突然删除

  •  0
  • Forepick  · 技术社区  · 4 年前

    我正在开发一个NodeJS应用程序,它可以监听Google Cloud Pub/Sub订阅。 这是我的相关代码:

    const messageHandler = message => {
        console.log(message.id);
    };
    subscription.on("message", messageHandler);
    

    作为系统架构的一部分,订阅可能会突然被外部资源删除,在这种情况下,我的应用程序刚刚崩溃,并显示以下错误日志:

    events.js:174
      throw er; // Unhandled 'error' event
      ^
    
    Error: Resource not found (resource=projects/proj-name/subscriptions/subscription-name).
        at MessageStream._onEnd (/Users/admin/Projects/proj-name/socket_server/node_modules/@google-cloud/pubsub/build/src/message-stream.js:244:26)
        at MessageStream._onStatus (/Users/admin/Projects/proj-name/node_modules/@google-cloud/pubsub/build/src/message-stream.js:281:18)
        at ClientDuplexStreamImpl.stream.on.once.status (/Users/admin/Projects/proj-name/node_modules/@google-cloud/pubsub/build/src/message-stream.js:146:44)
        at Object.onceWrapper (events.js:286:20)
        at ClientDuplexStreamImpl.emit (events.js:198:13)
        at Object.onReceiveStatus (/Users/admin/Projects/proj-name/node_modules/@grpc/grpc-js/build/src/client.js:389:24)
        at Object.onReceiveStatus (/Users/admin/Projects/proj-name/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:299:181)
        at process.nextTick (/Users/admin/Projects/proj-name/node_modules/@grpc/grpc-js/build/src/call-stream.js:130:78)
        at process._tickCallback (internal/process/next_tick.js:61:11)
    Emitted 'error' event at:
        at Subscriber.Subscription._subscriber.on.err (/Users/admin/Projects/proj-name/node_modules/@google-cloud/pubsub/build/src/subscription.js:198:38)
        at Subscriber.emit (events.js:198:13)
        at MessageStream._stream.on.err (/Users/admin/Projects/proj-name/node_modules/@google-cloud/pubsub/build/src/subscriber.js:328:38)
        at MessageStream.emit (events.js:198:13)
        at emitErrorNT (internal/streams/destroy.js:91:8)
        at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
        at process._tickCallback (internal/process/next_tick.js:63:19)
    
    

    有什么方法可以优雅地处理这种删除吗? 谢谢

    0 回复  |  直到 4 年前
        1
  •  1
  •   Ricco D    4 年前

    您可以查看pub/sub文档 nodejs error handling for subscribers .

    脚本的实现会持续监听错误或消息,直到达到分配的超时时间。

    顺便说一句,您应该设置一个新的订阅者并重置监听,或者重新创建已删除的订阅者并监听该主题。

    以下是 pub/sub subscriber error handling :

    /**
     * TODO(developer): Uncomment these variables before running the sample.
     */
    // const subscriptionName = 'YOUR_SUBSCRIPTION_NAME';
    // const timeout = 10;
    
    // Imports the Google Cloud client library
    const {PubSub} = require('@google-cloud/pubsub');
    
    // Creates a client; cache this for further use
    const pubSubClient = new PubSub();
    
    function listenForErrors() {
      // References an existing subscription
      const subscription = pubSubClient.subscription(subscriptionName);
    
      // Create an event handler to handle messages
      const messageHandler = function (message) {
        // Do something with the message
        console.log(`Message: ${message}`);
    
        // "Ack" (acknowledge receipt of) the message
        message.ack();
      };
    
      // Create an event handler to handle errors
      const errorHandler = function (error) {
        // Do something with the error
        console.error(`ERROR: ${error}`);
        throw error;
      };
    
      // Listen for new messages/errors until timeout is hit
      subscription.on('message', messageHandler);
      subscription.on('error', errorHandler);
    
      setTimeout(() => {
        subscription.removeListener('message', messageHandler);
        subscription.removeListener('error', errorHandler);
      }, timeout * 1000);
    }
    
    listenForErrors();
    

    这是我做的一个测试: enter image description here