I am trying to run a cron job with Firebase. Basically I need to run a function that makes a ton of API calls and pushes data to firebase at 12:00am PST every day. Is there
There are no cron job or schedule options on Firebase, you have to use another platform for that. Here´s one, for example: https://zapier.com/zapbook/firebase/schedule/
More information: Best way to globally set up alerts for when a Firebase location is changed
There are also some online cron jobs you can use, just search for "online cron job" with Google. Basically, they just call an URL in the specified time.
Last month Firebase released the Cloud Functions.
There, you can trigger lambda-like functions depending on different hooks: Pub/Sub events, HTTP events, ... They are planning to release a cron hook where you can trigger a specific function using a cron rule. It's not released yet, but meanwhile you can use Google App Engine
Back in March 2017 Abe Haskins published an article on how to utilize Google's App Engine Cron to schedule cron jobs for Firebase. If you want to follow this approach, you will need:
There's a cool tutorial by Jeff Delaney on how to make dynamic cron jobs for Firebase. You can have a look at it here.
The goal of this solution is to provide dynamic task scheduling that can be managed both clientside and serverside based on user interaction. Let’s consider some uses cases:
- Scheduled or snooze-able reminder notifications
- Send a happy birthday push notification
- Send welcome email 24 hours after signup
- Retry tasks if they fail due to errors
Finally, you can also use free third-party cron job services like cron-job.org. It looks pretty straight-forward and it gives you:
Hope that some of these will help.
At I/O 2019 it was announced a new feature to do cronjobs with firebase functions.
Check it out
export scheduledFunction = functions.pubsub.schedule(‘every 5 minutes’).onRun((context) => {
console.log(‘This will be run every 5 minutes!’);
});
exports.scheduleJobs = functions.pubsub.
schedule(“every day 03:00”).onRun(async() => {
// do something
console.log(“All done! See you tomorrow morning!”);
});
If you don't want to use a free online HTTP trigger/request generator (e.g. https://atrigger.com/), one approach is to fire the cloud function upon user (client-side) interaction with your app. A stored number
type field could hold a UNIX timestamp. If that timestamp is greater than 24 hours old, continue to fire your server-side cloud function (and refresh timestamp) else stop/break/quit. Could be a week or month or whatever time interval you want.
What if no user interacts with your app? In that case, why update your app's data? Regarding stale data for the 1st user, that's a matter of tuning your time interval. Also, you could possible alert the user that new data is being fetched and to hold on/wait.
To answer the question about how firebase-cron
works:
This module needs to be run on a server someplace so it's always on.
When jobs are run, all this means is that it takes the specified data for that job and puts it into the specified firebase-queue
reference. As a user, you set up the firebase-queue to run when the data is added to the queue.
You can add a "cron" job by specifying the name, the cron pattern, and the data add to the queue using the .addJob method:
cron.addJob('foo', '0 0 0 * * *', {foo: 'bar'}, function(err) {
if (err) return console.error(err);
console.log('added foo');
});
Use the .run method to start firebase-cron
. This checks the list of jobs and executes any that needed to be executed. This method also takes a callback function that will be called each time the jobs are checked (I use this for additional logging)
There are some things that I want to add to firebase-cron
and it's documentation but haven't gotten to them yet. I've been using it in production environments for a while now and haven't had any problems with firebase-cron
itself... just user errors in my queue logic ;)
Also, I've been using the new Firebase Cloud Functions and they're great. I may add a feature to be able to trigger a cloud function instead of using firebase-queue
. This probably won't be necessary once their cron service is running.