How To Create Cron Jobs In Firebase Programmatically

I have a unique problem here. I actually need to schedule a task to send emails to eligible users at specific times. I have checked and found out that scheduling cron jobs can solve the problem. However, in my case, I need to schedule these tasks programmatically based on when specific users meet certain conditions.

Here is my scenario

  1. I am running a referral campaign in my application where users who refer up to 5 persons get a discounted subscription

  2. After the user has referred 5 persons, the cost of subscription is slashed as long as he pays within 24 hours. Otherwise the cost returns to the original value

  3. I'm using firebase cloud functions to send eligible users reminder email reminders at certain times to make payment before the 24 hours elapses

Now the problem

  1. I want to send reminder emails at specific times say as soon as he refers 5 persons, when his time is remaining say 3 hours etc.

  2. If I just schedule a function regardless of the user, I may have to set cron jobs at regular intervals say 1 minute to always check if the users in my db are eligible and then send emails to them. This pose serious issue such that cloud functions are ran whether or not there are eligible users.

  3. Another problem is that with the above implementation, I cannot send emails to users at specific/exact times unless I schedule tasks for like every 1 second which will of course sky rocket price as cloud functions will be called around a whooping 86400 times (86400 seconds) a day

My proposed solution

  1. I just need a way to schedule cron tasks dynamically so that I can schedule a job for particular users. This will solve many problems like sending the emails at particular times, prevent cloud functions from running when not necessary, ability to set different email send times for different users etc

  2. I plan using http triggers with a request parameter of the user ID for scheduling tasks so that firebase can use this ID to assign tasks to only specific users, i.e each eligible user will have his own http trigger

Please is there a way to achieve this or any other good solution for my case?



Instead of trying to dynamically creating scheduled function, consider having a single function that runs on a fixed schedule, and then implementing your timing logic inside of that function.

So instead of saying "I need to schedule a function that runs 3 hours from now and send a message to these 5 people", think of it as a task that you can write into a database: "at x:yz send an email to these 5 people", and then have a periodic Cloud Function that checks what tasks it needs to execute.

Also see Delay Google Cloud Function, How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?

Alternatively you can use Cloud Scheduler to create a task for the specific action you want to perform, and then have it post to Cloud Functions via PubSub.

As an even newer alternative: Use a separate scheduler service that has an API to create schedules, like Cloud Tasks. Doug wrote a great article about that in How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).