Reputation: 1897
I am writing service using node.js + express. I have problems with timer.
I don't store any state at all, have 2 functions in my route.
route.js
var timeOutId;
exports.linkUpRequest = function() {
// do some request to another user => insert record in db
...
timeOutId = setTimeout(function() {
//delete request => delete record from db
}, 1000 * 60 * 60);
res.send(...);
};
exports.confirm = function() {
// record will be stored until user explicitly delete it => update that user confirm request
clearTimeout(timeOutId);
res.send(...);
};
I know that is is wrong solution because it is server and many user can access timeOutId
variable. My question is how to deal with timer and timeout id in REST service.
Note: In comments I call methods from my models, which is also stateless.
Upvotes: 0
Views: 134
Reputation: 967
Keeping track of all timer references is not going to be easy. One thing you can do is to store a confirmation status in db against each request. On confirmation, set status to confirmed. Check this status before deleting the record when timer ends, if confirmed do not delete the request. This way you wont be cancelling timers for confirmed requests but these requests do not get deleted on timeout.
Upvotes: 1