简体   繁体   English

Function 执行耗时 540029 毫秒,完成状态:“超时”

[英]Function execution took 540029 ms, finished with status: 'timeout'

I have created a cloud function that connects to an MQTT broker I have used a third-party MQTT broker (Mosquito MQTT broker), and sends the data to the Firebase real-time database every time the MQTT broker receives data from the machine.我创建了一个连接到 MQTT 代理的云 function 我使用了第三方 MQTT 代理(Mosquito MQTT 代理),并在每次 MQTT 代理从机器接收数据时将数据发送到 Firebase 实时数据库。 I am using the GCP console for writing and deploying the function. I successfully deployed the function without any errors, however, when I test it from the GCP console, it starts sending data but stops after the time specified in the timeout.我正在使用 GCP 控制台编写和部署 function。我成功部署了 function,没有任何错误,但是,当我从 GCP 控制台测试它时,它开始发送数据,但在超时指定的时间后停止。 I have tried timeout values from 60 to 540 seconds, but it still stops after the specified time.我已经尝试过 60 到 540 秒的超时值,但在指定时间后它仍然停止。 I have also increased the allocated memory, but it hasn't resolved the issue and I keep getting the same timeout error.我还增加了分配的 memory,但它没有解决问题,我不断收到相同的超时错误。 This is my code这是我的代码

 const Admin = require("firebase-admin"); const functions = require("firebase-functions"); const mqtt = require('mqtt'); const clientId = 'mqtt_googleserver_********7' const topic = '#' const serviceAccount = require("./service.json"); Admin.initializeApp({ credential: Admin.credential.cert(serviceAccount), databaseURL: "https://***************firebaseio.com/" }); exports.rtdb_mains = functions.https.onRequest((_request, _response) => { const client = mqtt.connect('mqtt://**.**.**.****.***',{ clientId, clean: true, connectTimeout: 4000, username: '******', password: '********', reconnectPeriod: 1000, }); const db = Admin.database(); client.addListener('connect', () => { console.log('Connected'); client.subscribe([topic], { qos: 1 }); console.log(`Subscribe to topic '${topic}'`); }); client.on('message', async (topic, payload) => { console.log('Received Message:', topic, payload.toString()); if (payload.toString().== "" && topic;== "") { const ref = db.ref("All_machines"). const childref = ref;child(topic.toString()). await childref;set(payload.toString()); const topicDetails = topic;split("/"); const machineId = topicDetails[1]. const machineParameter = topicDetails[2]; if (machineParameter === "BoardID") { const ref = db.ref(machineParameter); await ref;set(machineId); } } }); });

can anyone please help me with this problem.谁能帮我解决这个问题。

You don't need to specify a service.json if you push the CF on firebase. You can directly use the default configuration.如果在firebase上推送CF,则不需要指定service.json,直接使用默认配置即可。 You can do directly this:你可以直接这样做:

admin.initializeApp();

Secondly, the way you use your MQTT implementation and the cloud function are not correct.其次,您使用 MQTT 实现和云 function 的方式不正确。 You are listenning and waiting for a message in a function that is trigger only by a POST or GET request.您正在收听并等待 function 中的消息,该消息仅由 POST 或 GET 请求触发。 I suggest to use the pub/sub api for doing such a thing and have a good implementation for sending / receiving messages.我建议使用 pub/sub api 来做这样的事情,并且有一个很好的发送/接收消息的实现。

In case of you really need to listen for message in your MQTT implementation, you will need another provider than Cloud Function or calling the native MQTT of Cloud Function如果您确实需要在您的 MQTT 实现中监听消息,您将需要 Cloud Function 以外的其他提供者或调用 Cloud Function 的本机 MQTT

https://cloud.google.com/functions/docs/calling/pubsub https://cloud.google.com/functions/docs/calling/pubsub

https://www.googlecloudcommunity.com/gc/Serverless/Can-a-cloud-function-subscribe-to-an-MQTT-topic/mp/402965 https://www.googlecloudcommunity.com/gc/Serverless/Can-a-cloud-function-subscribe-to-an-MQTT-topic/mp/402965

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM