In OpenAF this can be achieved using addOnOpenAFShutdown:
addOnOpenAFShutdown(function() {
// Some closing code
// ...
});
The following example closes the created mini web server upon normal or forced termination:
ow.loadServer();
// Setting up
!pidCheckIn("testserver.pid") && !log("Already running") && exit(0);
// Add code to execute on termination
addOnOpenAFShutdown(function() {
log("Stopping...");
hs.stop();
log("Done!");
});
// Starting the httpd server
log("Starting...");
var hs = ow.server.httpd.start(8888);
hs.add("/", function(r) {
return hs.replyOKText("Current date: " + new Date());
});
log("Ready");
// Converting the script into a daemon so that it doesn't terminate upon Ctrl-C or similar
ow.server.daemon();
Do note that every call to addOnOpenAFShutdown will add a new hook. Upon termination, the last added hook will be the first to be executed and backwards to the first added.
No comments:
Post a Comment