AGOSSOU Yannick
AGOSSOU Yannick

Reputation: 3

NodeJs - setTimeout() for delete user subscription from db

I am new to nodeJs. I am developing a platform where users can subscribe in trial or pro version. I would like to know if I can use the setTimeout method to delete a user's info from db after the subscription expiration date. if this is not possible is there a way to do it or a library that allows you to manage subscriptions?

Upvotes: 0

Views: 726

Answers (4)

Valijon
Valijon

Reputation: 13113

Other solution: create TTL Index. Mongodb periodically checks and remove expired users.

You don't need to write any extra code / logic.

Upvotes: 0

Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 4748

A solution to your problem is to run a CRON job. That may run once every 24 hours to check user's subscription's and if trial has expired then delete the user. You can use the cron npm package to achieve this.

Upvotes: 0

Rodolfo Leal
Rodolfo Leal

Reputation: 537

You can, but it would not be a good approach since you be manging that on memory and if your server restarts you will lose this subscription status.

Why don't you just save on database the subscription date and on user login verify if the subscription date difference from the now is greater than the period of free subscription?

Upvotes: 1

sudo
sudo

Reputation: 1801

setTimeout is not reliable, service can go up or down and your request should be persistent.

You need an offline job manager, DB-based, like agenda or similar.

Question is also discussed widely here: I need a Nodejs scheduler that allows for tasks at different intervals

Upvotes: 0

Related Questions