Firesbase cloud storage: run function on database at interval

Viewed 28

I have this firestore database by firebase. This database contains documents which clients can then 'download' (i.e. view the data and parse it locally into a model class).

What I now would like to do is to mark certain documents as 'featured'. A document is featured if the average download count of the past 7 days is more than twice than that of the average of 14 days before and if the download count exceeds a certain threshold. I still might tweek this function.

But in order to do this marking, I'd need to run a function that reads in each document's download count (I put this download count inside the data of the document) every day, store those values and compare it to the stored values of earlier days.

What I now wonder is, does firebase have such functionality that allows me to host such a function or do I really need to setup my own external server (not my desktop of course) just for doing this?

2 Answers

You can use the Firebase Scheduled functions. You can find more about them here.

You can either schedule a cron job to run every so many hours or you can setup a document listener to listen for changes to a document or collection.

This is entirely dependent upon what your desired outcome is.

db.collection("cities").doc("SF")
    .onSnapshot((doc) => {
        var source = doc.metadata.hasPendingWrites ? "Local" : "Server";
        console.log(source, " data: ", doc.data());
    });

or

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {
  console.log('This will be run every day at 11:05 AM Eastern!');
  return null;
});
Related