0

So I have an application that allow users to create events with their friends, they can specify the time and duration of the event. What I want to do is that I want to delete the firestore document related to that event as soon as the event is finished (eventTime + eventDuration). I figured I would use cloud functions to achieve that by listening to onWrite event on all event documents, and then schedule deletion using setTimeout.

Event document structure

The thing is that if an event is edited, and the event time or duration is changed, I need to clear the first timeout and create a new one. To do so, I thought I would have to store the timeout reference in firestore, but I can't because the datatype is not supported.

I searched for a solution for this task but couldn't find anything useful for my application. Can anyone help ?

Thank you.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Instead of setting duration as a number you can set it as a timestamp so then you can perform difference operations on them as described in [this answer](https://stackoverflow.com/a/70721504/20239914). Then you can create `firestore onWrite` trigger to check the change before and after update as described [here](https://firebase.google.com/docs/functions/firestore-events#trigger_a_function_when_a_document_is_updated). – Rohit Kharche Mar 26 '23 at 12:35
  • I don't have a problem with calculating the delay time, rather I'm struggling with scheduling the deletion of the document, especially when the document is edited, because I'll have to clear the former timer before creating a new one, and I don't know how I can store that timerId. @RohitKharche – Daimellah Sofiane Mar 26 '23 at 13:45

1 Answers1

1

I would use Cloud Functions to achieve that by listening to onWrite event on all event documents, and then schedule deletion using setTimeout.

Using setTimeout() in a Cloud Function is not recommended. The main reasons are:

  • With Cloud Functions you're being billed for as long as your function is running, i.e. "from the time your function receives a request to the time it completes". So the time configured in your setTimeout() will be billed.
  • Cloud Functions have timeouts. In Cloud Functions 1st and 2nd generations, the maximum timeout duration is 9 minutes for event-driven functions (e.g. onWrite() event).

I searched for a solution for this task but couldn't find anything useful for my application. Can anyone help ?

You can implement the technique detailed in the article titled "How to schedule a Cloud Function to run in the future". In a nutshell, this technique consists in scheduling a Cloud Tasks task which will execute in the future and will call an HTTPS Cloud Function (in which you delete the corresponding event doc).

In case "an event is edited, and the event time or duration is changed" you can cancel the task (as explained in the "But what if I need to cancel the task?" section) and re-create a new task.

Note that, due to a Cloud Tasks limitation, "you won’t be able to schedule a task to execute greater than 30 days in the future". A possible workaround is to re-schedule a new task in the HTTPS Cloud Function triggered by the first task.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121