So in my android app, I am using the real-time database to store information about my users. That information should be updated every Monday at 00:00 o'clock. I am using a cloud function to do this but the problem here is the time zones. Right now I have set the time zone to 'Europe/Sofia' for testing purposes. In the documentation, it is said that the time zone for cloud functions must be from the TZ database. So I figured I could ask the user before registering in my app their preferred time zone and save it in the database. My question is after getting the user's prefered time zone is there a way to only write one cloud function and execute it dynamically for each time zone in the TZ database or do I have to create individual functions for each time zone in the TZ database?
If I correctly understand your question, you could have a scheduled Cloud Function which runs every hour from 00:00 to 23:00 UTC+14:00 on Mondays, and, for every execution (i.e. for every hour within this range), query for the users that should be updated and execute the updates.
I'm not able to enter more into details, based on the info you have provided.
It's not possible to schedule a Cloud Function using a dynamic timezone. You must know the timezone at the time you write the function and declare it statically in your code.
If you want to schedule something dynamically, read through your options in this other question: https://stackoverflow.com/a/42796988/807126
So, you could schedule a repeating function that runs every hour, and check to see if something should be run for a user at the moment in time that it was invoked. Or, you can schedule a single future invocation of a function with a service like Cloud Run, and keep rescheduling it if needed.
Related
I am trying to call an api every minute for ski lift status and check for changes. I am going to store the value of if the lift is open or closed in firebase (Real Time Database) and read to see if value from api is different and only update/ write to that node when it's a different value. Then I can set up a cloud function that will listen for database changes and send push notifications to the list of FCM tokens from that channel. I am not sure if this is the most efficient way, but I was going to set up scheduled functions to call the third party api.
I have been using these docs:
https://firebase.google.com/docs/functions/schedule-functions
I was planning to do something like this:
exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
CALL MY API IN HERE AND UPDATE DATABASE IF SNAPSHOT BACK IS DIFFERENT
});
I was wondering how would I run only between set times- say 8am-6pm EST. I am struggling to find anything about times to run. Should I just run the function every minute and then pause and resume by checking the time? In which case how does it know to keep checking the time when it is paused?
Firebase scheduled functions use Cloud Scheduler to implement the schedule. It accepts cron style time specifiers to indicate when a job should be run. The full spec for that can be found here. You will have to use ranges of numbers to indicate the valid times and frequency of the schedule. For example, you might use "8-18" in the hour field to limit the hours of execution.
I need to run a cron job to perform a specific cloud function after a set interval only once but a bit unsure of how to do it. Is there any way to do this through the current google cloud platform?
Update following our discussion below through comments:
If you want to "change a document in your Firestore database 2 hours after it has been created" you could do as follows:
When creating the document in Firestore, save the date/time of creation, e.g. with firebase.firestore.FieldValue.serverTimestamp()
Have an HTTP Cloud Function that you call regularly as explained below (every minutes? every 5 minutes?) and that, first, selects the documents that were created 2 hours ago (based on the saved timestamp) and then do the desired action on these docs.
If you want to trigger a Cloud Function through a cron job, note that you would normally do that through an HTTP Cloud Function, calling the Cloud Function URL via the cron job.
You can either use an external service like cron-job.org or you can use GCP's App Engine and Cloud Pub/Sub
See this video: https://www.youtube.com/watch?v=fEBPAMSk5_8
and this Blog post: https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html
both from the Firebase team.
Finally note that recently GCP launched a new product, Cloud Scheduler, which can be used to call HTTP Cloud Functions.
Sorry for late answer. Once upon time, I am stuck with this issue too. You sure can schedule a job executed once at particular time. But, you must use multiple platform as Firebase Cloud Function has time limit to cron task. If you look at Quotas and limits document for Time Limit of Firebase, you can see that Firebase cloud functions have set time limits until they are canceled (540 seconds or 9 minutes). So you can't cron a job executed after more than 9 minute with cloud function. But you can use Heroku server to cron a job without paying. Unfortunately, Heroku apps sleep after 30 minutes if there is no task along the time interval. However, you can keep awake with external server such as cron-job.org. You can get unlimitedly your app awake by applying pinging to your Heroku app every minute less than 30 minutes. You can use node-schedule to cron a job executed once for all time by using this code there:
const schedule = require('node-schedule');
const date = new Date(2012, 11, 21, 5, 30, 0);
const job = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
});
You can get current time or timestamp for Firestore and add time interval to current date to schedule as you desire. Dont forget to use timezone for it. You can use rule to set timezone like that:
const rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(0, 6)]; //all days
rule.hour = req.body.hour;
rule.minute = req.body.minute;
rule.second = req.body.second;
rule.tz = "Europe/Istanbul"; // You can specify a timezone!
Here, you can get request from client side by fetching time specification from user. And use schedule job module for one time task like that:
const job = schedule.scheduleJob(rule, function(data) {
console.log("Job ran #", new Date().toString());
}.bind(null, dataFuture));
Here you can use user data with .bind() by entering variable just like dataFuture variable. If your users use native android platform, you can specify time interval by entering hour_of_day and minute as:
Date currentTime = Calendar.getInstance().getTime();
Locale aLocale = Locale.forLanguageTag("tr-TR");
Calendar calendar = Calendar.getInstance(aLocale);
calendar.setTime(currentTime);
calendar.add(Calendar.MINUTE, 1);
calendar.add(Calendar.HOUR_OF_DAY, 4);
Alternatively you can use Cloud Task platform. But it may be a bit hard to use.
I have a doubt related to the data export from Google Analytics into BigQuery.
Basically, I have configured the streaming export on the Google Analytics side to, in real time, export the data into the BigQuery (table ga_realtime_sessions_YYYMMDD). This streaming is working fine.
At some point at the end of the day, the data from this real timetable is exported into the ga_sessions_YYYYMMDD.
What I need to be explained is how this export (from the real timetable into the ga_sessions one) works.
I have several automatic processes that run around 8 AM (Portugal timezone) and, in the last days, these processes are failing due to the fact that the ga_sessions for the previous day are not created yet.
I checked the time that the ga_sessions are created for every day and this time is very volatile, and for some cases is around 2 AM, 3 AM but in another case is around 7 AM, 8 AM. This time difference could be due to the data size that needs to be exported from the real timetable into the ga_sessions one?
The exports of daily sessions in BigQuery are indeed not completed at the same time everyday. This is due to a fully managed backend, which depends on workloads worldwide.
I suggest that you create an event listener on file creation for ga_sessions_YYYYMMDD, so that only once it is created you can then safely run dependent processes.
E.g. you can export the file in a Cloud Storage bucket, then use a trigger with a Cloud Function.
The official document of Firebase Realtime profiler says:
The profiler tool logs all the activity in your database over a given period of time, then generates a detailed report.
But it doesn't tell the specific time like last 24 hours.
My database usage shows that on a particular day, bandwidth consumed is X so I want to specify a particular day or time duration like last 24 hours in Firebase Realtime database profiler >
Q1. Is it possible to specify the duration in profile like last 24 hours?
Q2. How does profiler work?
I think, profiler just scans some log and keeps writing/streaming the operations to user console unless user stops the the profiling tool. Correct me if I am wrong here.
Q1. Is it possible to specify the duration in profile like last 24
hours?
No, it's not possible to profile "last" hours. But you can profile the next 24. (I'll get to that on Q2)
Q2. How does profiler work?
What the profiler does is it logs all the operations happening on your database from the time you run the command until the time you stop it. When you run the command, the console will show you how many operations have been logged so far and you can use Enter to stop logging. It will then show you (or save it to a file if you prefer) speed and bandwidth reports.
But it also has an option to set the logging duration (in seconds). For example, if you want to log the next 24 hours you can use:
firebase database:profile -d 86400
But have in mind that logging only happens if the computer that started it is still on. This means you'll need to keep your computer on for the next 24h.
Is it possible on Firebase or Parse to set up something kinda like a cron job?
Is there a way to set up some sort of timed operation that runs over the stored user data?
For example, I'm writing a program that allows people to RSVP for lunch everyday. If you have RSVPed by noon, then you get paired up with somebody else who has also RSVPed. Using JavaScript, the user can submit their RSVP in the browser.
The question is, can Firebase/Parse execute the code to match everyone at 12:00pm every day?
Yes, this can be done with Parse. You'll need to write your matching function as a background job in cloud code, and then you'll need to schedule the task in the dashboard. In terms of the flexibility in scheduling, it's not as flexible as cron but you can definitely run a task at the same time every day, or every x minutes/hours.
Tasks can take 15 mins max to execute before they're killed, so depending on the size of your database or the complexity of your task, you may need to break it up into different tasks or make it resumable.
Just to confirm about Firebase:
As #rickerbh said, it can be done with Parse, but currently there is no way for you to run your code on Firebase's server. There are 2 options for you 2 solve this:
You could use Firebase Queue and run your code in Node.js
You could use a different library such as Microsoft Azure (I still haven't tried this yet, I'm not sure if it provides Job Scheduling for Android)
However, Firebase is working on something called Firebase Trigger, which will solve our problem, however it is still not released with no confirmed release date.