Search code examples
firebasegoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-scheduler

Firebase schedule a function with multiple crontabs


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


Solution

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