2

I have the following cron job that I need to execute everyday at 6 and 9AM.

cron.schedule('00 00 6,9 * * 0-6', function() {
alertAllMembersWithAppointments();//executed everyday at 6 and 9AM});

When I set the timing like this

cron.schedule('* * * * * *', function() {
    alertAllMembersWithAppointments();//executed everyday at 6 and 9AM
});

it works fine and executes every minute. Please help to set the timing since it seems to me cron settings for node and linux are quite different. I am using the following requirement const cron = require("node-cron");

KENdi
  • 7,576
  • 2
  • 16
  • 31
MbaiMburu
  • 875
  • 1
  • 10
  • 19
  • 1
    You cannot run cron jobs in firebase functions. The cloud functions execute their code and close down. In this example they would init the `cron` class set the values then destroy themselves. – sketchthat Oct 04 '18 at 11:39
  • See some other stackoverflow questions for more information: https://stackoverflow.com/questions/35737708/how-to-run-cron-job-with-firebase – sketchthat Oct 04 '18 at 11:39
  • why does it work when I set it to execute every minute – MbaiMburu Oct 04 '18 at 11:39
  • 2
    Perhaps because cloud functions default timeout is 60 seconds so it sticks around long enough to execute? – sketchthat Oct 04 '18 at 11:40
  • 1
    seems there's no edible solution, since it's firebase I'll have to put the method in a https function and then try to execute it on linux crontab. I am sure it'll work. – MbaiMburu Oct 04 '18 at 11:49
  • I usually trigger my interval-based Cloud Functions through cron-job.org. – Frank van Puffelen Oct 04 '18 at 14:43

1 Answers1

2

@sketchthat's comment isspot on with the logic here: running the cron-job each minute, likely keeps it under the timeout of the function.

But re-triggering a function like that is going to be unreliable, unless you take care of returning the right promises. If you do it correctly, it's going to work and you end up having Cloud Function that is forever active. At that point, it'll be cheaper to find a cheap node host somewhere.

For reliable time-based functions, you'll need to rely on an external trigger. See Cloud Functions for Firebase trigger on time?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807