How to create a dynamically scheduled task?

Viewed 412

I am trying to create an app that simulates opening a tab at a bar. I am running into one issue that I can't seem to figure out - it goes as follows:

  1. When someone opens a bar tab, dynamically create a scheduled task that executes code to close the tab after 24 hours.

  2. If the tab gets closed before the 24 hours, cancel the scheduled task.

  1. If the tab doesn't get closed after 24 hours, execute the code described in step 1 to initiate a payment on the card used to open the tab.

I was initially looking into Firebase Functions, and was thinking about using a setTimeout() callable function, but after doing some research I found that Firebase Function's cannot be invoked for longer than 9 minutes.

NOTE: I would like this to be dynamic. Meaning, having it account for a variable amount of users. There could be 100 or 1000 users on the platform, each of them needs the ability to have a unique scheduled task for them (sometimes multiple per user).

1 Answers

Please see the comments for the full solution.

There are multiple approaches to circumvent the 10 minutes rule (which is prevalent in the serverless code) but here's something that can help you. I suggest separating the task into three:

  1. A cloud function that close the tab when called.
  2. A schedule function that calls it (https://firebase.google.com/docs/functions/schedule-functions)
  3. A way to start and stop the schedule function.

I am not sure how firebase function work, but I worked with azure functions before and those can be controlled with command line (CLI) or with a sdk for your language of choice. To cancel using the command line, try something like this:

firebase functions:delete scheduledFunction

from How to cancel a scheduled firebase function?.

Now what's left is how to figure out how to start the function, and if it's possible to pass in a parameter to schedule it.

Good luck!

Related