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
I am running a referral campaign in my application where users who refer up to 5 persons get a discounted subscription
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
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
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.
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.
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
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
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).
Related
I'm trying to achieve a similar function as blablacar has, app users can post leisure plans. So every leisure plan has a start date and an end date (even for more than 30 days), when a leisure plan ends I want to send a push notification. If a user publishes a leisure plan, I can create a task in the cloud, the problem is that if the finish date is greater than 30 days, this approach no longer works for me. I thought about using cloud scheduler, what happens is that I do not know if it would be very expensive to make queries to see which dates are finished about 3 times a day.
What approach should I take?
As you can see here "Cloud Scheduler pricing is based exclusively on the job... The actual running of a job is called an execution. A job is not billed for individual executions". That means that it does not matter the number of times you execute a job, you always be charged for a job (in case you only have 1).
In addition, Cloud Scheduler allows you to have up 3 free jobs per month, per billing account. So, if you need to use 4 jobs for this task, you only will be charged for 1 (the other 3 are free).
Finally, the other important thing to take into account is from where you are going to retrieve that information (Cloud SQL, Datastore, etc). This is to estimate a cost for all the resources you are going to use.
I think that a good option to estimate this is to use the GCP Calculator.
I want to write a scheduler to be triggered once every month to do a particular job that affects all users in a firestore database. What I want to know is, if I have for example one million users compared to having just one user in the database. what is the cost of running that scheduler for one user compared to one million users?
The Firebase documentation states that it cost $0.10 per job per month, but this does not seem to take into account the number of users involved.
So does the pricing of the scheduler change depending on the number users, can someone please clarify this, thanks in advance.
You are using three services, each with their own pricing:
Cloud Task Scheduled, which charges per job.
Cloud Functions, which charges per invocation, and then for the memory/cpu usage for as long as the function is active.
Cloud Firestore, which is charged per read/write operation per document, and for the bandwidth used to read data.
The total you pay is the combination of all these services. I recommend putting all your info in the Google Cloud Pricing Calculator to get an estimate of the cost, and (more importantly) in the factors that determine that cost.
As explained in the official documentation here, of Cloud Scheduler - that it seems to be the one you will be using:
Cloud Scheduler pricing is based exclusively on the job. A Cloud Scheduler job defines a single activity scheduled to run at a frequency provided in the definition.
The number of users is not considered as well directly in the Cloud Functions pricing, as its per invocations of the function. To summarize, it doesn't matter the number of users, but actually the number of jobs and invocations, that you will need to control, so you can calculate the real cost you will have.
Context: I am total Google Cloud begginer and I have just convinced my company headers to use Firestore Realtime Database for pushing transaction status to our mobile application. We have around 4 millions users that will use significantly our application for small money transfers. Now-a-days we use the concept of polling from Android/IOS to our Microservice endpoints and it will replaced by Firebase SDK imported to our Mobile app which will listen/observe to our Firestore Collection following few Firestore Rules. Since all money transfer will be confirmed/denied in short time (from few seconds to 1 or 2 minutes) the idea of replacing polling by a real reactive approach straigh from Firestore sounded and is already ongoing coding.
The issue: Firstly I don't what to compare solutions. It is just my reality: the prodution support operators must look after our internal Dashboard. Isn't allowed to them look at Google Dashboard Console (please accept this for this question). I need get on demand metrics of our FIrestore. It is nothing to do with Google pricing. It is just our demand: they want to see metrics like:
how many users listening at the same time now
how many users took some exception during connection
is there any user holding connection for more than X minute
when was the connection pick this morning
any exception of any type surrounding our Firestore database
I read Code Samples carefully follow the sample step-by-step trying to figure out some idea if there is some API providing the answers I am looking for.
So, my straight question is: is there such type of Google API providing metrics about my Firestore Database? Maybe following the same idea we found in Performance Monitor which works on Mobile side also some similar aproach on Firestore side.
*** Edited
Future readers may find worth read also about a way to get Firestore metrics info striagh from curl/postman
A couple of things: You mentioned both Firestore and Realtime Database; just wanted to make sure that you are aware that those are two different databases offered under the Firebase umbrella.
how many users listening at the same time now
is there any user holding connection for more than X minute
Yes, there's a dashboard: https://support.google.com/firebase/answer/6317517?hl=en. Including lots of options, like users active in the last 30 mins.
how many users took some exception during connection
any exception of any type surrounding our Firestore database
Yes, you can track errors and other logging via Stack Driver logging. These can give you reports on your cloud functions.
https://cloud.google.com/functions/docs/monitoring
Where can I find Stackdriver in Firebase console?
when was the connection pick this morning
For this one, I'm not sure if you mean A. when did somebody log on in the morning, or B. what was the time that there was the peak \ most usage. If B see 1. If A,
Real-time database has the concept of presence, which lets you know if a user is currently logged in or not. See examples here from the official documentation:
https://firebase.google.com/docs/firestore/solutions/presence
and this post
How to make user presence mechanism using Firebase?
Also applies to your
is there any user holding connection for more than X minute
..............
Edit in response to comments: I believe you are experiencing the XY problem https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem where you are focused on a particular solution, even though your problem has other solutions. User metrics, database events, and errors are all accessible through both dashboards and cloud functions. You can cURL cloud functions if you wish, or set up cron functions to auto report, or set up database trigger functions to log errors. So, while the exact way you want this to work may not exist, you just need to connect existing tools to get the result you want.
I have a language learning app and I want to send daily reminders like Busuu.
The word to be sent will be different each day and it will be a word from the user practiced vocabulary. (for the next 5 days of no activity on the app)
How can I achieve that using firebase? I think that I can store on Firestore the last session time and a list of 5 words of the last practice and use a function with a crown job to get the users that are no active specific time(1-5 days) and send the notification with the corresponding word. I don't know if it is the best approach.
You can use cloud functions and cron jobs. Watch this video for getting started
Timing Cloud Functions for Firebase using an HTTP Trigger and Cron - Firecasts
Same with above, but using pub-sub event. And the cron job using google app engine. The step by step guide, can be found here
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
I am running a referral campaign in my application where users who refer up to 5 persons get a discounted subscription
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
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
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.
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.
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
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
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).