云功能不向 PubSub 发送消息
Cloud function doesn't send messages to PubSub
总结:
您好,我正在使用云函数作为由 PubSub 触发的异步后台工作程序。
我有 2 个云函数,第一个将向 Cloud SQL 发出请求,然后对于每个结果,将此结果发送到 PubSub。第二个将在消息(因此数据库中的结果之一)发送到 PubSub 时触发。
错误:
有时(完全随机)第一个云函数在 SQL 请求后不发送任何消息,我在日志中有 0 个错误,什么也没有。
问题:
我做错了什么吗?
我是否尝试确认消息? (我想没有原因'我在 PubSub 文档中看到由 PubSub 触发的 CF 会自动确认消息)
我要重新发送消息吗?但是如果我有 0 个错误,我怎么知道我是否必须这样做?
代码:
//[requirements]
const {PubSub} = require('@google-cloud/pubsub');
const pubSubClient = new PubSub('<PROJECT_ID>');
const topicName = "<TOPIC_NAME>";
const topicPublisher = pubSubClient.topic(topicName)
//[requirements]
//[request]
//make the setted request
conn.query(sql, (e,results) => {
//if there is an error send it
if(e) console.log(e)
//for each result of the query, log it and publish it on PubSub
results.forEach(function (element){
console.log(JSON.stringify(element))
msgPubSub(JSON.stringify(element))
})
})
//[request]
//[PubSub message publish fonction]
async function msgPubSub(data){
const messageBuffer = Buffer.from(data)
try {
var futurePublish = await topicPublisher.publish(messageBuffer)
console.log("Message id: " + futurePublish)
} catch (error) {
console.error(`Error while publishing message: ${error.message}`)
}
}
//[PubSub message publish fonction]
日志:
当它不工作时:
工作时:
我认为您应该将查询函数转换为:
let results;
try {
results = await conn.query(sql);
} catch (err) {
console.error(`SQL Error: ${err.message}`);
return;
}
// TODO Maybe you can place a check here whether `results` is array
// Or something like that
for(let result of results){
console.log(JSON.stringify(result))
await msgPubSub(JSON.stringify(result))
}
await和callback一起用是废话
总结:
您好,我正在使用云函数作为由 PubSub 触发的异步后台工作程序。 我有 2 个云函数,第一个将向 Cloud SQL 发出请求,然后对于每个结果,将此结果发送到 PubSub。第二个将在消息(因此数据库中的结果之一)发送到 PubSub 时触发。
错误:
有时(完全随机)第一个云函数在 SQL 请求后不发送任何消息,我在日志中有 0 个错误,什么也没有。
问题:
我做错了什么吗?
我是否尝试确认消息? (我想没有原因'我在 PubSub 文档中看到由 PubSub 触发的 CF 会自动确认消息)
我要重新发送消息吗?但是如果我有 0 个错误,我怎么知道我是否必须这样做?
代码:
//[requirements]
const {PubSub} = require('@google-cloud/pubsub');
const pubSubClient = new PubSub('<PROJECT_ID>');
const topicName = "<TOPIC_NAME>";
const topicPublisher = pubSubClient.topic(topicName)
//[requirements]
//[request]
//make the setted request
conn.query(sql, (e,results) => {
//if there is an error send it
if(e) console.log(e)
//for each result of the query, log it and publish it on PubSub
results.forEach(function (element){
console.log(JSON.stringify(element))
msgPubSub(JSON.stringify(element))
})
})
//[request]
//[PubSub message publish fonction]
async function msgPubSub(data){
const messageBuffer = Buffer.from(data)
try {
var futurePublish = await topicPublisher.publish(messageBuffer)
console.log("Message id: " + futurePublish)
} catch (error) {
console.error(`Error while publishing message: ${error.message}`)
}
}
//[PubSub message publish fonction]
日志:
当它不工作时:
工作时:
我认为您应该将查询函数转换为:
let results;
try {
results = await conn.query(sql);
} catch (err) {
console.error(`SQL Error: ${err.message}`);
return;
}
// TODO Maybe you can place a check here whether `results` is array
// Or something like that
for(let result of results){
console.log(JSON.stringify(result))
await msgPubSub(JSON.stringify(result))
}
await和callback一起用是废话