I set quite a few server-side timeouts with setTimeout and setInterval for each connected user that can last for 10-30 seconds. If the Node.js instance restarts in the middle of one of these timeouts, they are obviously all cleared on restart, which can cause some issues for these users. How would I go about persisting these timeouts, or are there any modules that already help with this?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Keeping track of variable instances
setTimeOut
takes delay as parameter, so when setting timeout, capture 'currentServerTime + delay
' say 'serverTriggerTime
' and persist this in DB. Then, on restart of server, create the same timer using theserverTriggerTime
. Then,delay = serverTriggerTime - currentServerTime
, use this delay to set new timer.When setting timer
On server restart:
Also, set new timer only if newDelay >= 0, meaning the trigger time has not reached and will happen after newDelay time.
I would store the start times and durations in Redis and restart incomplete timers when your application reloads. Some Redis modules:
https://github.com/joyent/node/wiki/modules#wiki-db-nosql-redis