CentoKili53
CentoKili53

Reputation: 133

Is SetTimeout() for days a bad practice?

So, my Meteor app lets users register for a free 14-day trial. When they register, their DB document is updated with 'Free Trial: Active'.

I'd like for the document to automatically update to 'Free Trial: Expired' after 14 days from the registration date, but was wondering if SetTimeout() with a 14-day delay (in ms) would be appropriate?

Upvotes: 0

Views: 822

Answers (2)

Chris Peters
Chris Peters

Reputation: 18090

You'll want to have a field in your database called something like expiresAt set for a date/time 14 days in the future. Whenever you authenticate by querying for the account, you'll want to run logic like this:

SELECT
  # yadda yadda yadda
FROM
  accounts
WHERE
  expiresAt > NOW()

The problem with something like setTimeout is that it's a runtime construct. So that means that it occupies processor and memory space while it's running. On top of that, you would lose the setTimeout process if the machine that it's running on was powered down or crashed, and you wouldn't be able to replicate that functionality across multiple machines or processes.

Upvotes: 2

Joundill
Joundill

Reputation: 7594

No, it's not the correct way of solving your problem.

You'll want to store a date of when their trial expires against their account, and check on login whether their trial has expired (i.e. is today's date > the expiration date?) displaying a message if it has.

Upvotes: 3

Related Questions