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 a way for me to do this on the server side with Firebase?
I see that there's this module here, but I don't understand how it works at all. If anyone has any ideas or know how the aforementioned module could work that would be fantastic.
EDIT: Idea. What if I were to create a data entry that showed the time that the function was completed. Basically check to see that last time the function was called and completed. If it's past 12am then the function is called and the field is updated so it's not called until the next day. Would this be a good idea? I don't need the function to happen at 12am exactly, I just need it to be completed by the time the first user logs in.
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!”);
});
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
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.
Update: There are cron jobs now, see https://stackoverflow.com/a/56063698/757508
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With