110

I am looking for a way to schedule Cloud Functions for Firebase or in other words trigger them on a specific time.

benomatis
  • 5,035
  • 7
  • 30
  • 51
ahsan
  • 1,237
  • 2
  • 9
  • 6

3 Answers3

124

Update 2019-04-18

There is now a very simple way to deploy scheduled code on Cloud Functions through Firebase.

You can either use a simple text syntax:

export scheduledFunctionPlainEnglish =
functions.pubsub.schedule('every 5 minutes').onRun((context) => {
    console.log('This will be run every 5 minutes!');
})

Or the more flexible cron table format:

export scheduledFunctionCrontab =
functions.pubsub.schedule('5 11 * * *').onRun((context) => {
    console.log('This will be run every day at 11:05 AM UTC!');
});

To learn more about this, see:

Note that your project needs to be on a Blaze plan for this to work, so I'm leaving the alternative options below for reference.

If you want to schedule a single invocation of a Cloud Function on a delay from within the execution of another trigger, you can use Cloud Tasks to set that up. Read this article for an extended example of how that can work.

Original answer below...


There is no built-in runat/cron type trigger yet.

For the moment, the best option is to use an external service to trigger a HTTP function periodically. See this sample in the functions-samples repo for more information. Or use the recently introduced Google Cloud Scheduler to trigger Cloud Functions through PubSub or HTTPS:

enter image description here

I also highly recommend reading this post on the Firebase blog: How to Schedule (Cron) Jobs with Cloud Functions for Firebase and this video: Timing Cloud Functions for Firebase using an HTTP Trigger and Cron.

That last link uses cron-job.org to trigger Cloud Functions, and works for projects that are on a free plan. Note that this allows anyone to call your function without authorization, so you may want to include some abuse protection mechanism in the code itself.

Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
  • Find out `exports.cronReminders` works: changed the first letter of func name to lowcase – Konstantin Konopko Feb 10 '18 at 14:31
  • @Frank does this still hold true today? Or is there a better option, also once that is cost effective... – Famic Tech Apr 03 '18 at 02:18
  • 1
    Nothing changed around this. Including the fact that `cron-job.org` that Jen talks about in the blog post is free to use. – Frank van Puffelen Apr 03 '18 at 03:52
  • 2
    The video from Jen has been tagged as deprecated. So is there another way to do it? – b-fg May 29 '18 at 18:17
  • 2
    The approach is as valid today as it was when the video (and blog post) were made. The syntax has changed slightly, but I don't think this specific case is affected by that. If you're having trouble making this work, open a question that shows what you've already done. – Frank van Puffelen May 29 '18 at 19:15
  • 1
    Wow, finally they implemented a way to do it simply ! Finally !!!! Thanks for the update – schankam Apr 22 '19 at 04:17
  • 1
    From the Cloud Scheduler page: `Each Cloud Scheduler job costs $0.10 (USD) per month`, assuming "job" doesn't mean each time the scheduled thing fires, but the cost per timer? i.e. a job that runs every minute only costs $0.10? (excluding whatever cloud functions it calls). – Aaron Ash May 11 '19 at 11:32
  • 1
    @AaronAsh "Cloud Scheduler pricing is based on the job. A Cloud Scheduler job defines a single activity scheduled to run at a frequency provided in the definition. The actual running of a job is called an execution. A job is not billed for individual executions. For instance, if a single job is defined to run for “every day of the month”, then that job is billed $0.1/month and not $3/month for 30 executions of that single job." Source: https://medium.com/@pascalluther/scheduling-firebase-cloud-functions-with-cloud-scheduler-b5ec22ace683 – Emerica Aug 12 '19 at 07:16
  • 1
    It's also possible to schedule a function to run once at a *specific point in time* using Cloud Tasks. https://medium.com/firebase-developers/how-to-schedule-a-cloud-function-to-run-in-the-future-in-order-to-build-a-firestore-document-ttl-754f9bf3214a – Doug Stevenson Jan 18 '20 at 17:09
15

What you can do, is spin up an AppEngine instance that is triggered by cron job and emits to PubSub. I wrote a blog post specifically on that, you might want to take a look:

https://mhaligowski.github.io/blog/2017/05/25/scheduled-cloud-function-execution.html

mhaligowski
  • 2,080
  • 18
  • 23
  • 1
    What is the estimated cost per month, for a cron job that triggers once every hour? – Ehtesham Hasan Feb 28 '18 at 15:23
  • 3
    @EhteshamHasan Looks like it's potentially free: https://cloud.google.com/free/. Currently 28 instance hours / day free; Also, there's Google Compute Engine's f1-micro instance w/ Linux running crons for free atm. – bitsoflogic Mar 06 '18 at 19:29
0

It is important to first note that the default timezone your functions will execute on is America/Los_Angeles according to the documentation. You may find a list of timezones here if you'd like to trigger your function(s) on a different timezone.

NB!!: Here's a useful website to assist with cron table formats (I found it pretty useful)

Here's how you'd go about it: (Assuming you'd like to use Africa/Johannesburg as your timezone)

export const executeFunction = functions.pubsub.schedule("10 23 * * *")
    .timeZone('Africa/Johannesburg').onRun(() => { 
       console.log("successfully executed at 23:10 Johannesburg Time!!");
    });

Otherwise if you'd rather stick to the default:

export const executeFunction = functions.pubsub.schedule("10 23 * * *")
    .onRun(() => { 
       console.log("successfully executed at 23:10 Los Angeles Time!!");
    });