Firebase schedule a function with multiple crontabs

403 Views Asked by At

I want my function to run once a day, but twice on weekends. How do I do that with Firebase?

My crontabs:

  • 0 16 * * *
  • 0 22 * * 6,0

From what I know, my only chooce is to create another instance of the same function with a different crontab. Is there a better way?

Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

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?

Yes, this is the only solution. However you can put the "main" code in one function, as follows:

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