flutter/firebase schedule a function at given time - flutter

I want to schedule the cloud function at a specific time and that time will be in firestore document.I want that when i add data inside firestore, a cloud function will trigger and get data from that latest added document and will fetch date and time from that document data and then schedule a cloud function at that specific time to perform a specific task (update status in firestore).

For the time scheduling check this official documentation out:
https://firebase.google.com/docs/functions/schedule-functions
If you want to execute code on database changes check the triggers out:
https://firebase.google.com/docs/functions/database-events
You will need something like onUpdate(). Depends on your needs.
You can store files in the firebase storage. Check out the official documentation for a code example.
https://firebase.google.com/docs/storage/web/list-files
In case you want to read data from the firebase in your Flutter app, you can implement the flutter-firebase packages.
Here you can find the instructions for all firebase packages:
https://firebase.flutter.dev/
Stack Overflow is for asking questions. Your Question sounds more like you expecting that someone will give you the whole code, so you don't have to do any research about that.
If that's not the case, sorry for the misunderstanding.

Cloud Functions trigger for Firestore writes. There is nothing built in to trigger them at a time that is specific in the document that is written.
But you can build that yourself using Cloud Tasks, as Doug shows in this excellent blog post: How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL)

I used the node-schedule package inside the Firebase Cloud Functions, after which I was able to schedule tasks at dynamic times from FireStore.
Here is sample code:
exports.scheduleMessage=functions.firestore.document('users/{userID}/pending/{messageID}').onCreate(async (snapshot,context) =>{
schedule.scheduleJob(messageId,`myDate.getUTCMinutes()
myDate.getUTCHours() * * *`, async () =>{
// your logic inside
});
});

Related

Trigger cloud function on specific date and time firebase

I want to change a document value at a specific date time in firestore, I don't want to use a scheduleFunctions of firebase i think it is costly to see every minute or an hour to check. Is there any better option which will trigger right on the given time etc.??. And it should be one time not a periodic call using flutter web and firebase for it
You could use the Google Cloud Scheduler to hit a HTTP Cloud Function periodically but using scheduled functions is your best bet.
Firebase had a very generous free tier. You mentioned you are “checking” every couple minutes or hours. That sounds like you updating a document based on an event, in which case you can use PubSub or Firestore triggered cloud functions.

How to store PubSub data to big query using cloud functions?

I am once again asking for your help.
Let me tell you my current situation first.
I have a device that connects to the "Cloud IoT core" and sends data using mqtt.
The data then goes to the Pub/Sub topic.
Then a "Cloud function" gets triggered which stores the data inside "Firestore"
Another "Cloud function" gets triggered which sends me an email with the stored data inside Firestore.
The size of the data is about 1 Kilobyte and I expect to send about 10K messages per Month
I need that data to create a dashboard for which I am using "Google Data Studio"
To get my data inside there I installed the Firebase extension "Stream Collections to BigQuery" to send the data to "BigQuery". from there I just had to click a few buttons to automaticly stream data from BigQuery to "Google Data Studio"
Everything works so far but as you can see I store the data 4 times. once via email, once inside firestore, once inside BigQuery and Data studio. All of this is going to cost alot of money in the long term, because the data stored doubles every Month.
What I need from You guys is some advice on best practices.
Is there a way to store the data directly inside BigQuery when it arrives in the Pub/Sub?
If so can I also send an email with the data as an attachment?
Is BigQuery a good solution or should I use "Cloud SQL"?
To save data inside Firestore I can execute the following inside a cloud function. Is there a similare way for BigQuery?
firestore.collection("put Collection name here").doc(put document name here).set({
'name' : name
'age' : age
}).then((writeResult) => {
//console.log('Successfully executed set');
return;
}).catch((err) => {
console.log(err);
return;
});
Is there a way to store the data directly inside BigQuery when it
arrives in the Pub/Sub?
Yes, you can use Dataflow to build a streaming pipeline, as explained in different documentation items or blogs:
GCP Doc: Pub/Sub Topic to BigQuery
A Dataflow Journey: from PubSub to BigQuery
Write a Pub/Sub Stream to BigQuery
But you could also use the Node.js Client for BigQuery in a Cloud Function, triggered by Pub/Sub. However, one could consider that this doesn't "store the data directly"...
If so can I also send an email with the data as an attachment?
If you use a Cloud Function, that's quite easy, for example by using the dedicated "Trigger Email" Firebase Extension.
You can also directly send an email from a Cloud Function by using the nodemailer package, see this official Cloud Function sample.
Is BigQuery a good solution or should I use "Cloud SQL"?
It all depends on you exact use case... There is a lot of literature on the net: https://www.google.com/search?client=firefox-b-d&q=difference+between+Cloud+SQL+and+BigQuery
However, since you are going to use Data Studio, a classical answer would be to use BigQuery since it is best suited for analytics. But again, it depends on you exact use case.
(Note that this question alone would probably be closed on SO because it is opinion based).
To save data inside Firestore I can execute the following inside a
cloud function. Is there a similar way for BigQuery?
Yes, as said above, use the Node.js Client for BigQuery in your Cloud Function.

The best practices for running CloudFunctions regularly based on the timestamp of each user

We are developing an app on Flutter on the client side and Firebase on the server side. I'm thinking of running Cloud Functions regularly using Cloud Scheduler based on each user's timestamp.
My idea is to run Cloud Functions using Cloud Scheduler every day at 12:00. Only users who have a timestamp older than 10 days perform a specific action. Is this a best practice?
Or is it possible to process Cloud Functions using the user's timestamp as a trigger?
For example, Cloud Functions is triggered when 10 days have passed since the user's timestamp.
Update
The scenario is as follows.
Cloud Firestore
/user/${userId}/funcStatus/status

Document(status) field is
timestamp:last update date(e.g. 2019/10/31)


I want to execute Cloud Function after 10 days, that is, when it becomes 11/10.

However, the timestamp varies depending on the user. e.g, userA:10/31, userB:10/20
The first option is possible with scheduled functions.
The second option is not possible with scheduled functions alone. You would have to use a Firestore onCreate trigger, then set up a callback with Cloud Tasks to get the function to execute at the right time.
Whichever one you choose is a matter of preference and whatever meets the needs of your app. There is no right or wrong way.

Run a Google Cloud Function for each file in a bucket

I have a Google Cloud Function triggered by a Google Cloud Storage object.finalize event. When I deploy a new version of this function, I would like to run it for every existing file in the bucket (which have already been processed by the previous version of the function). Processing all the existing files in the bucket is a long running task, hence I don't think a Google Cloud Function which will process all files in a row is an option.
The best option I can see for now is to make a Google Cloud Function I can triggered via HTTP that will list all the files in the bucket and publish one event per file via Google PubSub, and then process each of these events with a slightly modified version of my initial Google Cloud Function which accepts a PubSub event in place of the object.finalize storage event.
I think it can work but I was wondering if there was an easier way to perform this operation.
If the operation you're trying to perform may take longer than the maximum time that a Cloud Function can run, you will need to split that operation into multiple steps. Your approach of using a PubSub trigger for each individual file, sounds like a valid approach to do that for me.
One option might be to write a small program that lists all of the objects in a bucket and, for each object, posts a message to Cloud Pub/Sub that triggers your function in the same way a GCS change would.

Google SQL Cloud operations callback?

I currently have an application which triggers import jobs to Google SQL Cloud using the their API:
https://cloud.google.com/sql/docs/admin-api/v1beta4/instances/import
This works great. However, this is only a request to import an SQL file. I have to check that the request was successful a minute or two afterwards.
What I would like, is to somehow register a callback to notify my application when the operation is complete. Then I can delete the bucket item and mark the data as persisted.
I have no idea if this is possible, but would be grateful for any advice. Perhaps the PubSub system API could be used for this, but so far have been unable to find any documentation on how this would be done.
There's currently no out of the box way to do this. You need to poll the operation status to determine when it's finished.