3

I have a swift iPhone application paired with Google Firebase. Within the application the user will complete an action (press a button). At this point, I would like to schedule a Google Firebase Function to run 45 minutes after the user action. However, I would like this function to be able to be cancelled when another action (press a button) is completed within the iPhone application. I could do this within swift with a timer but that wouldn't work if the user closes the application.

I am not opposed to using a third party scheduler or something of the sorts. Any suggestion welcome.

I have looked at the possible duplicate questions and answers (Cloud Functions for Firebase trigger on time?) However, most of the links in that answer are deprecated and out of date. Also, that is referring to scheduling something repeatedly. For example, every hour, once a day, etc... I am looking to schedule the job 45 minutes after trigger (user action) with the ability to cancel the job within that 45 minute window.

goalguy
  • 159
  • 1
  • 3
  • 10
  • You can't make it in your app. It must be on your backend. Is your only backend your firebase db ? – GIJOW Dec 13 '18 at 15:40
  • Yes, my backend is Firebase. I wouldn't be opposed to adding another backend for this function. Maybe something within AWS? – goalguy Dec 13 '18 at 18:09
  • You can write your own on AWS Lambda for example, but AFAIK, there is no plug n play service for this at AWS – GIJOW Dec 13 '18 at 18:16

5 Answers5

2

Would a delayed Cloud Task work for you?

You can create a Cloud Task that is delayed and calls your function through an HTTP trigger. The task can be then cancelled if your user performs the second action you desribe. There is a number of dedicated client libraries for Cloud Tasks, as well as a REST API.

Voy
  • 5,286
  • 1
  • 49
  • 59
0

There is no built-in way to delay the trigger on a Cloud Function, or to re-trigger it after a certain delay. The best way I can think of using Cloud Functions is to set up a periodic trigger as shown here: Cloud Functions for Firebase trigger on time?. And then in that periodic trigger you determine what jobs have to run.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Is there another backend or service that could accomplish this? Possibly something within AWS? – goalguy Dec 13 '18 at 18:09
  • 1
    That's asking for a technology recommendation, which is off-topic on Stack Overflow (since it tends to attract opinionated answers). – Frank van Puffelen Dec 13 '18 at 18:19
  • Is this still the answer or did google add the feature to delay functions recently? – yuval Apr 15 '20 at 10:39
  • Nothing changed on the product level for this, although there are some different examples out there now. I recommend reading Doug's blog: https://medium.com/firebase-developers/how-to-schedule-a-cloud-function-to-run-in-the-future-in-order-to-build-a-firestore-document-ttl-754f9bf3214a – Frank van Puffelen Apr 15 '20 at 13:31
0

You could use https://delayedrequest.com as long as you can invoke your Firebase Function via an http request.

Ken
  • 172
  • 7
0

I recently had a similar issue that was solved by using Cloud Scheduler and Firestore. I set up a job that would trigger a cloud function every 5 minutes that would check a Firestore collection. It would retrieve the data on when a cloud function should be triggered and trigger it. It also depends how accurate you want your time triggers to be. It's not optimal to have it trigger all the time for no reason but in my case it worked.

Jacob Bonk
  • 303
  • 3
  • 8
-1

Javascript/Typescript allows you to delay using the setTimeout function

// time is in milliseconds
function delay(time:number) {
  return new Promise(res => {
    setTimeout(() => {
      res("VALUE TO RESOLVE");
    }, time);
  });
}

Then call this by doing const res = await delay(1000);

  • Don't do this unless you have a short delay. The function will still be running (costing money) and will likely timeout. The maximum timeout is 10 or 60 minutes depending on the type of function. – D W May 13 '22 at 09:00