Google Scheduled Cloud Function not working

Viewed 28

Not really sure what the issue is or really how to deploy it, my code is as follows:

exports.autoLockWeek = functions.pubsub.schedule('50 20 * * FRI')
.timeZone('America/New_York')
.onRun((context) => {
    
    sceduleLockRef.child("AutoLockWeek").once('value').then(function(snap) {

        gamesRef.orderByChild("WeekId").equalTo(parseInt(snap.val())).once('value').then(function(snapshot) {

            snapshot.forEach(function(child) {
            
                gamesRef.child(child.key).update({

                    isPlayed: true
                
                });
                
            });

        });

    });

    return null;

});

I am unsure how do i deploy this now?!

I was able to deploy it, as I can see it in my cloud functions list but when the time 'hit' the function did not trigger like i was expecting!?

1 Answers

Cloud Functions are short-lived and exit when they've finished their execution or after a set timeout (max 9 minutes for 1st gen, 60 minutes for 2nd gen). So you can't set a schedule within your function as it wont be running to be able to execute it.

You need to set the schedule outside of your function which can trigger the function to execute. You can do this using Cloud Scheduler. This can manage the cron schedule as a job and trigger the function by making a HTTP request to the URL.

Related