Why is my Cron Scheduled Cloud Function for Firebase running on Pacific time rather than UTC?
Asked Answered
C

3

5

I have a Scheduled Cloud Function (using Google's new solution) that is meant to run every Monday at 12:00am.

export const updateHighScores = functions.pubsub.schedule('0 0 * * 1').onRun((context) => {

    // (code)
    // console.log(‘This code will run every Monday at 12:00 AM UTC’);

});

Functions Dashboard

I was expecting it to run at 12:00am UTC; however, when midnight UTC rolled around, nothing happened. So I went to bed, saddened that my scheduled cloud function didn't work, but determined to keep working at it.

But the next day I checked the logs, and it appears it did work, but it ran at 12:00am Pacific time instead.

Functions Log

  • The Cloud Function's region is set to us-central1, but I don't think that affects this.
  • My computer's time zone is set to Pacific time and I'm located in central time, but I wouldn't think either would matter.
  • I also clicked around in Firebase and Google Cloud Platform to see if maybe I had a setting that was affecting it, but didn't find anything.

Any idea why this ran at midnight Pacific time rather than UTC?

(I'm going to do a bunch of guess-and-checking by changing all these variables and observing how it affects the scheduled cloud function, but figured might as well ask here in case someone knows off the top of their head. Thanks!)

Constantine answered 20/5, 2019 at 12:11 Comment(0)
N
13

Scheduled functions are triggered on a scheduled by Google Cloud Scheduler. When deploying with the Firebase CLI, the entry in Cloud Scheduler is automatically created for you. If you click through to the Cloud console to show your schedules, you'll see the timezone is set to "America/Los_Angeles", which is PST.

The timezone is set using the function builder API. You can read about that in the documentation.

Neils answered 20/5, 2019 at 15:9 Comment(2)
Got it. Thanks again Doug!Constantine
I edited the answer here to reflect the fact that you can set the timezone using the function's builder API. It's documented.Neils
A
5

I do something like this

exports.scheduledFunction = functions.pubsub
.schedule("0 4 * * *")
.timeZone("Asia/Baku")
.onRun((context) => {
    groupCustomers.set({});
    tables.set({});
    calledCustomers.set({});
    groups.set({
        A: { name: "Altering", activeOnTablet: false },
        
    });

    return null;
});
Anthonyanthophore answered 31/10, 2020 at 10:36 Comment(2)
what is the best practice to schedule functions for multiple timezones?Machinery
Maybe different functions. I think this may be a great Stackoverflow question :)Anthonyanthophore
F
2

For anyone who is wondering how to do set the timezone of scheduled functions using 2nd Gen firebase functions (e.g. onSchedule), you can use the ScheduleOptions parameter. For example,

exports.updateHighScores = onSchedule(
    {
        schedule: "0 0 * * 1",
        timeZone: "America/Los_Angeles"
    }, 
    (context) => {
        // Code to execute on schedule
    }
);
Fridafriday answered 14/4 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.