【发布时间】:2021-04-23 17:12:57
【问题描述】:
我有一个每分钟运行一次以更新文档的后台作业。 如果更新时间超过一分钟,我需要跳过下一个后台作业。 同步此逻辑的最佳方式是什么?
我现在有
export const pickWinnerBackgroundJob = functions.pubsub.schedule('every 1 minutes').onRun(async (context) => {
const lock = await admin.firestore()
.collection(`lock`)
.doc(BG_JOB_LOCK_DOCUMENT_ID)
.get();
if (lock.exists && lock.data()?.locked == true) {
return;
}
await admin.firestore()
.collection(LOCK_COLLECTION_NAME)
.doc(BG_JOB_LOCK_DOCUMENT_ID)
.set({ locked: true })
var promises: Promise<any>;
// Add document update promises and execute job
return Promise.all(promises).then(() => {
return admin.firestore()
.collection(LOCK_COLLECTION_NAME)
.doc(BG_JOB_LOCK_DOCUMENT_ID)
.set({ locked: false })
});
});
这是一种有效的方法吗?
【问题讨论】:
标签: node.js firebase google-cloud-firestore google-cloud-functions