0

I need to execute a cronjob function after some change has been done to one document in a collection

For example, when my user store is open , I need to launch a cronjob that will close that store at the time the user specifies, what I was thinking was something like this

exports.updateUser = functions.firestore
    .document('stores/{storeId}')
    .onUpdate((change, context) => {

     //Logic to get the store close time here, lets say I get close in 4 hours
     //Now here I plan to run a cronjob like this

     exports.scheduledFunctionCrontab = functions.pubsub.schedule('* 4 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {

   //Close my store in 4 hours

});


    });

Can I do this ?? Is there any way to do something like this ?

Thanks !

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
SNM
  • 5,625
  • 9
  • 28
  • 77

1 Answers1

2

You cannot change the configuration of a scheduled Cloud Function from another Cloud Function.

In your case, one solution is to:

  1. Write the closing time in a specific field of your store document;
  2. Schedule a Cloud Function that (a) runs e.g. every minute, (b) checks if there are any opened store documents with a closing time inferior to the current running time, and (c) if there are any such store documents modifies their status to closed.
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Thanks a lot Renaud, for your time and the answer, but one question, if I do this every minute I will need to read all documents each minute to see if there is a close time, if not I have wasted a read, is there a better way to just launch the cron when the close time arrives to a shop ? – SNM Apr 30 '20 at 15:39
  • I imagine if I have 100.000 shops I will be reading 100.000 documents each minute for maybe getting 2 or 3 shops that has a close time – SNM Apr 30 '20 at 15:41
  • You're welcome. You can write a query based on (firstly) the closingTime field and (secondly) the status which should be different than closed. – Renaud Tarnec Apr 30 '20 at 15:42
  • Oh ! Thanks a lot ! you are right, so the query will only read the documents with those results only and not all the documents right ? – SNM Apr 30 '20 at 15:42
  • Exactly! If you give a try writing the query and encounter problems, do not hesitate to ask for help! You should assign a timestamp value to the closingTime field. You will find several answers on SO which shows how to write such a query, but again do not hesitate to ask help. – Renaud Tarnec Apr 30 '20 at 15:44
  • Nice ! What do you think about having this feature in Firebase ? to trigger cron functions when an event happends ? this is really usefull I think to prevent scheduled functiosn to be hardcoded like this (I mean need to run every 5 - 10 minutes to check) – SNM Apr 30 '20 at 15:45
  • Not sure to fully understand your comment. cron functions are equivalent to scheduled functions. When I wrote "Schedule a Cloud Function" I meant a scheduled function `functions.pubsub.schedule` – Renaud Tarnec Apr 30 '20 at 15:46
  • I mean a feature that allow us to listen for certain document triggers (eg: update) and inside that trigger schedule a function to happend at x time, because doing it this way that is the only way to do it right now needs a function to keep executing each 5 minutes for the whole day and thats not performant I think – SNM Apr 30 '20 at 15:47
  • Like I said, from a Cloud Function you cannot schedule another function at a specific time. "keep executing each 5 minutes for the whole day and that's not performant I think" -> there shouldn't be any problem of performance, don't worry, it is Google Cloud infrastructure ;-) and the cost will be neglectable ($0.40/million of CF invocations, plus the cost of the scheduler, see https://firebase.google.com/docs/functions/schedule-functions#before_you_begin) – Renaud Tarnec Apr 30 '20 at 16:04