node.js - NodeJS run code in X minutes architecture -
i want schedule code run @ variable time. example, after 60 minutes want send http request endpoint , update database document, want able cancel code being ran if action occurs before 60 minutes.
what best way architect this? want able survive server restarts , work if app scaled across multiple servers.
thanks!
you use settimeout()
, save timer id returns because can use timer id cancel timer. schedule timer:
var timer = settimeout(myfunc, 60 * 60 * 1000);
then, sometime later before timer fires, can cancel timer with:
cleartimeout(timer);
if want survive server restarts, need save actual system time want timer fire persistent store (like config file on disk or database). then, when server starts, read value persistent store and, if set, set new settimeout()
trigger @ time. likewise, when timer fires or when clear timer because no longer need it, update persistent store there no future time stored there.
you make clean use creating persistenttimer
object had method setting timer, clearing timer , initializing whatever persistent store using upon server restart.
Comments
Post a Comment