Google Cloud功能(包含Pubsub)错误

时间:2017-08-11 17:33:29

标签: google-cloud-functions google-cloud-pubsub

我们正在使用GCP和Cloud功能(使用Pubsub主题触发器),虽然它在大多数情况下都能正常工作,但我们确实看到它经常抛出以下身份验证错误:

  

错误:请求的身份验证凭据无效。预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据。见https://developers.google.com/identity/sign-in/web/devconsole-project。   at(/user_code/node_modules/@google-cloud/pubsub/node_modules/grpc/src/node/src/client.js:569)

有没有人看过这个或者知道是什么导致它或如何解决?

云功能代码:

require('dotenv').config()
const datastore = require('@google-cloud/datastore')()
const pubsub = require('@google-cloud/pubsub')()
const promiseRetry = require('promise-retry')
const elasticsearch = require('elasticsearch')
const SupplierSearchManager = require('shared-managers').SupplierSearchManager
const BaseManager = require('shared-managers').BaseManager
const Util = require('shared-managers').Util
const StatsManager = require('shared-managers').StatsManager
var unitTestMode = false
var searchManagerMock, pubsubMock

exports.setUnitTestMode = (searchManagerMockP, pubsubMockP) => {
  unitTestMode = true
  searchManagerMock = searchManagerMockP
  pubsubMock = pubsubMockP
}

exports.updateElastic = event => {
  let dataObject
  try {
    dataObject = JSON.parse(Buffer.from(event.data.data, 'base64').toString())
  } catch (err) {
    console.log(err)
    console.log(event.data.data)
    return Promise.reject(err)
  }
  const baseManager = new BaseManager(datastore, new Util('base-manager'), pubsub)
  const statsManager = new StatsManager(baseManager, new Util('stats-manager'))
  let supplierId = dataObject.supplierId
  let retryCount = dataObject.retryCount
  let refresh = dataObject.refresh === true
  if (!retryCount) retryCount = 0
  return new Promise((resolve, reject) => {
    let elasticClient = new elasticsearch.Client({
      host: process.env.ELASTIC_HOST,
      httpAuth: process.env.ELASTIC_AUTH
    })
    const supplierSearchManager = unitTestMode ? searchManagerMock : new SupplierSearchManager(elasticClient, baseManager, statsManager)
    supplierSearchManager.indexSupplier(process.env.ELASTIC_INDEX, supplierId, refresh).then(resolve, err => {
      console.log(err)
      if (retryCount < 2) {
        const topic = unitTestMode ? pubsubMock : pubsub.topic(process.env.PUBSUB_PREFIX + process.env.PUBSUB_TOPIC_ELASTIC)
        promiseRetry({retries: 4, maxTimeout: 8000}, (retry, number) => {
          return topic.publish({ supplierId: supplierId, retryCount: retryCount + 1 }).catch(err => {
            retry(err)
          })
        }).then(resolve, err => {
          console.log(err)
          reject(err)
        })
      } else {
        resolve(true)
      }
    })
  })
}

0 个答案:

没有答案