How to trigger cloud function based on date stored in cloud firestore? - firebase

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

Related

Flutter firebase change a field after 1 year

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.

Firebase and GCP Cloud Tasks for scheduled posting

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.

Firebase Cloud Functions pricing for Admin SDK

Is 125k invocations per month for Firebase cloud functions applies to admin-sdk used on custom server? How price of cloud functions is calculated if used with admin sdk on self hosted server?
The 125k invocations per month you are referring to is talking about how many times a Cloud Function is executed/triggered (the term used by Firebase here is invoked). Use of the admin SDK on your own hardware/third-party server doesn't make use of Cloud Functions and is unrelated to the invocation limits.
Let's say you set up a HTTPS Cloud Function called date. Each time a user visits https://us-central1-<project-id>.cloudfunctions.net/date, this would count as 1 invocation of that Cloud Function (ignoring response caching/use of a CDN).
Another example is listening for new data in Cloud Firestore that you can call createUser. Each time a new users/someUserID document is created, the createUser function would be invoked.
For such trivial use cases, you aren't likely to hit the 125k limit. But if you have Cloud Functions that deal with frequently modified data or rapidly triggered pub/sub topics, you can quickly approach these limits if care is not taken.
One example of this is if you set up a RTDB Cloud Function that was incorrectly listening to any data under /posts. Every time a user (or server) changed any data under /posts, the function would be invoked. If your cloud function updated /posts/count everytime it was called, the cloud function would retrigger itself leading to an infinite loop.
Is 125k invocations per month for Firebase cloud functions applies to admin-sdk used on custom server?
Cloud Functions billing is not at all related to whatever billing is incurred by the Admin SDK. If you use the Admin SDK, you will be billed according to the products it uses, in addition to whatever billing might happen for Cloud Functions.
How price of cloud functions is calculated if used with admin sdk on self hosted server?
It's not possible to self-host Cloud Functions. Cloud Functions only runs within Google Cloud infrastructure.
If you use the Admin SDK on your own host, outside of Cloud Functions or any Google Cloud hosting, it does not change the billing compared to the same usage in Cloud Functions.
If you want to know what the cost is for using the Admin SDK, you should understand which product you are accessing with that SDK, and look up its own pricing. The Admin SDK itself does not bill - it is the usage of the underlying Firebase or Cloud product that incurs billing.

Can we delete an attribute of a document in firestore automatically?

I am working on an android project using java where we have a subscription for pro users. Right now I am using firestore as my database, is there any way that I can automatically change the pro attribute in user from true to false in a specific time automatically as the subscription time end? In my user collection's document, there are attributes is_a_pro and pro_availability_time which is in json (YYYY-MM-DD). Thank you
There is nothing built into Firestore to automatically change documents at a given time.
The most common solution is to schedule a Cloud Function, that runs periodically, and then check which subscriptions have expired.
An alternative to running them periodically is to schedule a Cloud Task for when each subscription expires. For an example of how to do that, see Doug's blog post How to schedule a Cloud Function to run in the future with Cloud Tasks.

What is the appropriate way to load data from Firestore into BigQuery?

I was looking at a few ways to export data out from Firestore without using export (expensive operation in the long term as it doesn't support incremental backups) to use in BigQuery and Data Studio.
1) Using Google Pub/Sub.
This will probably require function to both write to pub/sub and then another to trigger to BQ.
2) Using Cloud Functions to trigger from an onCreate event to write directly to a BigQuery dataset and table.
(This is using table.insert)
What would the advantage be to use Pub/Sub - other than it would appear that it will cost more in the long term?
Or is there another way I am unaware of to do this?
I'm new at this. Some advise and pro and cons of the above scenarios are much appreciated.
The official solution is here.
In case of using Cloud Functions to trigger from an onCreate event, what will you create? Create File on Cloud Storage or create Firestore Document?
I think that in case of using Cloud Functions you should use PubSub trigger.
I recommend asynchronous architecture like Pub/Sub. Because rerun is easy and the scope of influence is limited.
I developed sample is here. I'm using Cloud Scheduler not cron.yaml. The cost of Cloud Scheduler is here.
(If you want) Export Firebase Authentication users to Cloud Firestore Collection. Use Firestore, Cloud Functions(PubSub) and Cloud Scheduler.
Export All Cloud Firestore Collections and Specified collections to Cloud Storage. Use AppEngine and Cloud Scheduler.
Export Specified Cloud Firestore Collections to BigQuery( as Partitioned tables). Use AppEngine and Cloud Scheduler.

Resources