【发布时间】:2019-11-02 03:17:54
【问题描述】:
我有一个 Google Cloud 应用,其中包含多个调用 API 的云函数,然后将响应保存在 Firebase 中。我设置了这个预定的函数来重试错误的 API,它工作得很好。但如果我需要的数据还不存在,我想重试呼叫。如果我抛出错误,立即再次调用可能会起作用,但丢失的数据不太可能在几秒钟后可用,所以我宁愿在那之后每小时检查一次,直到我有有效的数据。
以下是我的函数的简化版本。我可以想象添加一个 setTimeout 并让函数再次调用自身,但我不确定我会这样做,或者这是一个好主意,因为它会使函数长时间保持活动状态。有没有办法在任意时间间隔内自动重试这个预定的功能?
exports.fetchData= functions.pubsub
.schedule('every Tuesday 6:00')
.timeZone('America/New_York')
.onRun(async context => {
const response = fetch(...)
.then(res => {
if (res.status < 400) {
return res;
} else {
throw new Error(`Network response was not ok. ${res}`);
}
})
.then(res => res.json());
const resObj = await response;
resObj.map(x => {
// check response for valid data
})
if (// data is valid) {
// save to Firebase
} else {
// retry in 1 hour
}
});
});
【问题讨论】:
标签: firebase google-cloud-platform google-cloud-functions