首页 > 解决方案 > Firebase 安排具有多个 crontab 的函数

问题描述

我希望我的函数每天运行一次,但在周末运行两次。如何使用 Firebase 做到这一点?

我的 crontab:

据我所知,我唯一的选择是使用不同的 crontab 创建同一函数的另一个实例。有没有更好的办法?

谢谢

标签: firebasegoogle-cloud-functionsgoogle-cloud-scheduler

解决方案


据我所知,我唯一的选择是使用不同的 crontab 创建同一函数的另一个实例。有没有更好的办法?

是的,这是唯一的解决方案。但是,您可以将“主”代码放在一个函数中,如下所示:

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 ...
    
}

推荐阅读