在模拟器中以编程方式调用 gcloud 后台函数
Programmatically calling gcloud background functions in the Emulator
我已经在模拟器中部署了 gcloud 后台功能 (pubsub)。
它正在从命令行成功调用
functions call helloPubSub --data='{"message":"Hello World"}'
如何从本地服务器代码调用 gcloud 本地函数?
= = =
下面是我在服务器端发布到主题的代码
pubsub
.topic(topicName)
.publisher()
.publish(dataBuffer)
.then(results => {
const messageId = results[0];
console.log(`Message ${messageId} published.`);
res.status(200)
res.send({hello:'world'})
})
.catch(err => {
console.error('ERROR:', err);
res.status(200)
res.send({err:err})
});
我收到以下错误消息
{"err":{"code":7,"metadata":{"_internal_repr":{}},"note":"Exception occurred in retry method that was not classified as transient"}}
在 official docs 中指出:
Note: Functions deployed in the Emulator with non-HTTP triggers like Cloud Storage or Cloud Pub/Sub will not be invoked by these services. The Emulator is intended as a development environment only, so you would need to call these functions manually to have them invoked.
因此,如果您使用 Cloud Pub/Sub 触发器在本地部署了一个函数,调用它的唯一方法是使用命令行命令:
functions call [your-function]
我已经在模拟器中部署了 gcloud 后台功能 (pubsub)。
它正在从命令行成功调用
functions call helloPubSub --data='{"message":"Hello World"}'
如何从本地服务器代码调用 gcloud 本地函数?
= = =
下面是我在服务器端发布到主题的代码
pubsub
.topic(topicName)
.publisher()
.publish(dataBuffer)
.then(results => {
const messageId = results[0];
console.log(`Message ${messageId} published.`);
res.status(200)
res.send({hello:'world'})
})
.catch(err => {
console.error('ERROR:', err);
res.status(200)
res.send({err:err})
});
我收到以下错误消息
{"err":{"code":7,"metadata":{"_internal_repr":{}},"note":"Exception occurred in retry method that was not classified as transient"}}
在 official docs 中指出:
Note: Functions deployed in the Emulator with non-HTTP triggers like Cloud Storage or Cloud Pub/Sub will not be invoked by these services. The Emulator is intended as a development environment only, so you would need to call these functions manually to have them invoked.
因此,如果您使用 Cloud Pub/Sub 触发器在本地部署了一个函数,调用它的唯一方法是使用命令行命令:
functions call [your-function]