Firebase 安排具有多个 crontab 的函数
Firebase schedule a function with multiple crontabs
我希望我的功能每天 运行 一次,周末两次。我如何使用 Firebase 做到这一点?
我的 crontabs:
- 0 16 * * *
- 0 22 * * 6,0
据我所知,我唯一的选择是使用不同的 crontab 创建相同功能的另一个实例。有没有更好的方法?
谢谢
From what I know, my only choice is to create another instance of the
same function with a different crontab. Is there a better way?
是的,这是唯一的解决办法。但是你可以将“main”代码放在一个函数中,如下所示:
exports.scheduledFunction1 = functions.pubsub.schedule('...').onRun(async (context) => {
try {
await mainFunction();
return null;
} catch (error) {
console.log(error);
return null;
}
});
exports.scheduledFunction2 = functions.pubsub.schedule('...').onRun(async (context) => {
try {
await mainFunction();
return null;
} catch (error) {
console.log(error);
return null;
}
});
async function mainFunction() {
//await ...
}
我希望我的功能每天 运行 一次,周末两次。我如何使用 Firebase 做到这一点?
我的 crontabs:
- 0 16 * * *
- 0 22 * * 6,0
据我所知,我唯一的选择是使用不同的 crontab 创建相同功能的另一个实例。有没有更好的方法?
谢谢
From what I know, my only choice is to create another instance of the same function with a different crontab. Is there a better way?
是的,这是唯一的解决办法。但是你可以将“main”代码放在一个函数中,如下所示:
exports.scheduledFunction1 = functions.pubsub.schedule('...').onRun(async (context) => {
try {
await mainFunction();
return null;
} catch (error) {
console.log(error);
return null;
}
});
exports.scheduledFunction2 = functions.pubsub.schedule('...').onRun(async (context) => {
try {
await mainFunction();
return null;
} catch (error) {
console.log(error);
return null;
}
});
async function mainFunction() {
//await ...
}