我遇到了这种现象,我不太确定这是正常现象还是我的编码方式错误。我正在执行预定的pub / sub函数,该函数的执行和完成时间比实际执行的时间短得多(如日志中所示)。
在Firebase控制台上,这是我看到的:
7:21:09.403 PM Saving page ...
7:21:07.187 PM Saving page ...
7:15:01.720 PM Function execution took 816 ms, finished with status: 'ok'
7:15:00.906 PM Function execution started
在这里,我的日志比函数执行晚一些,例如,函数在816毫秒内执行,而该函数的实际执行时间约为6-7分钟。由于我的代码正在调用某些后端Webscraping API,因此6-7分钟的时间有点符合预期。
这里是我的代码,为了简洁起见:
//The pubsub function in index.js
exports.scrape = functions.pubsub
.schedule('0 12-22/1 * * *')
.onRun((context) => {
var scraper = new Scrape()
const group = constants.group
return scraper.scrape(group);
})
//At Scrape.js file, the entry point scrape function
Scrape.prototype.scrape = async function(group) {
startScrapingForGroup(group)
}
function startScrapingForGroup(group) {
var ref = firestore.db.collection('scrape_config')
return ref.where('group', '==', group).get()
.then((snapshot) => {
if (snapshot.empty) {
return;
}
const chapters = snapshot.docs.map((doc) => {
...
return chapter
})
return chapters
})
.then(async (chapters) => {
for (chapter of chapters) {
await getSubChapters(chapter)
}
})
.catch((err) => {
console.log('Error', err)
})
}
function getSubChapters(chapter) {
const url = constants.url;
return axios.get(url, config)
.then(response => {
const subChapters = response.data.subChapters;
return subChapters;
})
.then(async (subChapters) => {
var promises = []
for (var subChap of subChapters) {
//setup promises
promises.push(promise)
}
const changedChapters = await Promise.all(promises);
if (changedChapters.length > 0) {
scrapeSubChapters(changedChapters);
} else {
return;
}
})
.catch(error => {
console.log(error);
return;
});
}
function scrapeSubChapters(changedChapters) {
for (const chapter of changedChapters) {
getPage(chapter);
}
}
function getPage(chapter) {
const url = constants.url;
return axios.get(url)
.then((response) => {
const page = response.data.page;
return page;
})
.then((page) => {
saveUpdatedPageToFirestore(page);
})
.catch((err) => {
console.log('Error scraping page for path', chapter.url);
console.log(err)
return;
});
}
function saveUpdatedPageToFirestore(page) {
console.log("Saving page...", page.substring(0,100));
return;
}
您可能会说,这是一个很长的脚本,预计将花费大量时间。然而,云功能在1000ms以下完成,但日志消息确实在最后执行。这是预期的吗?