pupuupup
pupuupup

Reputation: 275

setInterval on firebase instead of using cron

I am making a spam counter ( on Firebase ). What I do is I use database trigger on firebase cloud functions to increment a path (/counter/${uid}). This path will hold an integer for each user that other path will have a security rule that reference to it and check whether it exceed the limit. However, I would like to clear the counter once a day.

When I search on google I found official way of firebase to do this by using another Google cloud service to deploy cron job. However, I wonder if I use setInterval on cloud function instead would work. This task would only be a one line execution ( admin.database().ref('/counter').set({}) . And it is not so serious that if it were to skip once or twice of the execution due to some problem, it should be ok.

Thanks

Upvotes: 1

Views: 2633

Answers (2)

sketchthat
sketchthat

Reputation: 2688

Don't use setInterval as you'll be paying for un-used compute time.

Instead see this video on YouTube; https://www.youtube.com/watch?v=CbE2PzvAMxA

They go into detail of how to setup a free-schedule service and setup a HTTP trigger that should achieve the result you're after.

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317342

The use of setInterval won't work, and it's not really ever recommended to do so. You can use setInterval to keep a function alive for some amount of time, but you will be paying for that time even if the function is just waiting. Also you are still subject to the way Cloud Functions will time out your function (default 60 seconds, max 9 minutes by special configuration).

Upvotes: 5

Related Questions