在Firebase上运行计划云功能时,出现Cloud Function finished with status: 'timeout'"
错误。也许我没有很好地完成它,但我找不到问题,我真的很感激任何帮助。
所以,我必须在一个序列中执行两个https请求,我使用promise。最后,我得到了结果,它有效,但我不想超时,因为它是周期性运行的。
const functions = require("firebase-functions");
const https = require('https');
exports.somePeriodicRequest = functions.pubsub
.schedule('40 */11 * * *')
.onRun(async (context) => {
return new Promise((resolve, reject) => {
const firstRequest = https.request(firstRequestOptions, (firstRequestResult) => {
firstRequestResult.on('data', (response) => {
var data = JSON.parse(response);
resolve(data.property);
});
});
firstRequestRequest.on('error', (e) => {
functions.logger.error(e);
reject();
});
firstRequestRequest.write(firstRequestBody);
firstRequestRequest.end();
}).then((property) => {
return new Promise((resolve, reject) => {
const secondRequest = https.request(secondRequestOptions, (secondRequestResult) => {
secondRequestResult.on('data', (response) => {
resolve();
});
});
secondRequestRequest.on('error', (e) => {
functions.logger.error(e);
reject();
});
secondRequestRequest.write(property);
secondRequestRequest.end();
});
});
});
非常感谢!
好吧,多亏了@doug-stevenson,我用axios http客户端取代了promise,现在它可以在没有任何超时的情况下工作。
示例:
exports.extendEvaluationTokenLife = functions.pubsub
.schedule('every 5 minutes')
.retryConfig({
retryCount: 3,
maxBackoffDuration: '120s',
maxRetryDuration: '300s',
minBackoffDuration: '5s'
})
.onRun(async (context) => {
await axios({
method: 'put',
url: 'https://some.url/api/something/',
headers: {
'Content-Type': "application/json"
}
}).catch(function(error) {
if (error.response) {
// Request made and server responded
functions.logger.error(error.response.data);
functions.logger.error(error.response.status);
functions.logger.error(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
functions.logger.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
functions.logger.error('Error', error.message);
}
});
return null;
});