This question already has an answer here:
How to create cron jobs in firebase programmatically
(1 answer)
Closed 2 years ago.
Does anyone know how can I set up Cron Jobs dynamically with Firebase? I want to build a rule engine using which the Client can specify the rules, actions, and schedule and based on that I need to schedule that particular rule. Since the scheduling is in the hand of the Client. I can set a predefined frequency for the JOB.
I know in Node.js I can do it with libraries like "node-schedule". But since the Client strictly wants to use Firebase and Angular. I can't use that solution
There is no way to dynamically create schedule functions with just Cloud Functions.
The two most common approaches are:
Have a regularly scheduled Cloud Function (say every minute) that then reads the tasks from a database, and executes the tasks that are up.
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).
Also see:
How to create cron jobs in firebase programmatically
Related
I am building a feature where user subscribe to app by making payment, it sets a value in cloud Firestore like {"subscribed": true}. I want to set the value false after one year. Can you anyone show me an example how to do it ?
There's no way you can directly use Firestore to schedule any task in the future. If you're not running any custom backend service, then you probably have to look into doing it with Cloud Tasks (Cloud Functions to trigger them).
You can create a cloud function to schedule the Task and one function to handle the task (this will include the logic to update the subscribed flag to false again.
I am looking to integrate an automated posting service in my firebase application. The users will create a post with a desired posting time from the client application which will be added to my Firestore database.
I would like to be able to create a Cloud Task to actually add the post to the client dashboard at the desired time, which could be weeks/months in the future.
Is a cloud function Firestore trigger that creates a cloud task the best implementation?
I know that Cloud Scheduler/ Pub/Sub /App Engine is normally the flow recommended for functions run on a normal schedule, i.e once daily/weekly. But I am looking to allow my users to specify the exact time they want their post to be sent.
Is my thinking to use Cloud Tasks correct?
Any insight would be appreciated!
I think that best approach is to use the Cloud Functions for Firebase client SDKs that will let you call functions directly from a Firebase app. To call a function from your app in this way, write and deploy an HTTPS Callable function in Cloud Functions, and then add client logic to call the function from your app. Then, if you want to schedule functions to run at specified times, use functions.pubsub.schedule().onRun() This method creates a Pub/Sub topic and uses Cloud Scheduler to trigger events on that topic, ensuring that your function runs on the desired schedule.
I'm building an app that allows users to schedule social media posts. The date (and time) when the post is supposed to go live is stored (along with the content of the post) in a cloud firestore document. Is there a way to trigger the cloud function based on the date set by the user?
The built-in type to trigger scheduled functions sets the schedule at deploy time. There is no built-in trigger type to trigger at a specific dynamic time. But you can use Cloud Scheduler to dynamically schedule a callback to a Cloud Function.
Doug Stevenson wrote a good blog post about that, so I recommend checking that out: How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).
There is nowadays also a built-in trigger type for this: enqueue functions with Cloud Tasks
This question already has answers here:
Cloud Functions for Firebase trigger on time?
(3 answers)
Closed 4 years ago.
I want to automatically update a child value in firebase database, monthly.. How can I handle this?
There is nothing built into the Firebase Realtime Database to automatically update values periodically. So you will have to write the code for this yourself.
After you write the code you will need to run that code periodically.
If you put the code into the app, you can run it periodically there. It's technically possibly, but a bit involved (is the app running all the time?, what if multiple users run it?, etc).
So the more common approach is to run that code on a server. If you don't have a server yet, you can run the code in Cloud Functions for Firebase, which allows you to run small pieces of (for now only JavaScript or TypeScript) code on Google's servers. To trigger this code to run monthly, see Cloud Functions for Firebase trigger on time?
There is nothing fully integrated at the moment.
One way would be to use a Google Cloud scheduler job that will start your process in a Google Cloud function once a month.
You will only pay once a month for the duration of your function.
You can have an example here
So I know that Cloud Functions for Firebase don't natively support cron jobs, and one have to depend on an external source to trigger an HTTP cloud function. But I don't think this will work in my case.
I have an app in which users can create competitions that end in 24 hours, thus each time a competition is created, I need to set up a timer that triggers one time after 24 hours.
Do you guys have any idea on how this problem can be approached?
At this moment the solution that the Firebase guys are providing for solving the cron issue is to use Google App Engine to deploy a server and trigger the functions via Pub/Sub hooks. Here you can find the tutorial.
Basically you have to:
Create a Cloud Function that responds to a PubSub hook.
Deploy an App Engine that will publish an event depending on the cron setup you set.
If you don't want to setup an App Engine you can use an external resource like cron-job.org that can call your HTTP Cloud Function.