rework exit hooks
parent
167d710cfe
commit
e344377025
76
bin/www.js
76
bin/www.js
|
|
@ -3,20 +3,19 @@ import http from "node:http";
|
||||||
import * as configFunctions from "../helpers/functions.config.js";
|
import * as configFunctions from "../helpers/functions.config.js";
|
||||||
import exitHook from "exit-hook";
|
import exitHook from "exit-hook";
|
||||||
import ntfyPublish from "@cityssm/ntfy-publish";
|
import ntfyPublish from "@cityssm/ntfy-publish";
|
||||||
import debug from "debug";
|
import Debug from "debug";
|
||||||
const debugWWW = debug("lot-occupancy-system:www");
|
const debug = Debug("lot-occupancy-system:www");
|
||||||
let httpServer;
|
|
||||||
const onError = (error) => {
|
const onError = (error) => {
|
||||||
if (error.syscall !== "listen") {
|
if (error.syscall !== "listen") {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
switch (error.code) {
|
switch (error.code) {
|
||||||
case "EACCES": {
|
case "EACCES": {
|
||||||
debugWWW("Requires elevated privileges");
|
debug("Requires elevated privileges");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
case "EADDRINUSE": {
|
case "EADDRINUSE": {
|
||||||
debugWWW("Port is already in use.");
|
debug("Port is already in use.");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
|
@ -28,37 +27,44 @@ const onListening = (server) => {
|
||||||
const addr = server.address();
|
const addr = server.address();
|
||||||
if (addr) {
|
if (addr) {
|
||||||
const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port.toString();
|
const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port.toString();
|
||||||
debugWWW("Listening on " + bind);
|
debug("Listening on " + bind);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const ntfyStartupConfig = configFunctions.getProperty("application.ntfyStartup");
|
||||||
const httpPort = configFunctions.getProperty("application.httpPort");
|
const httpPort = configFunctions.getProperty("application.httpPort");
|
||||||
if (httpPort) {
|
const httpServer = http.createServer(app);
|
||||||
httpServer = http.createServer(app);
|
httpServer.listen(httpPort);
|
||||||
httpServer.listen(httpPort);
|
httpServer.on("error", onError);
|
||||||
httpServer.on("error", onError);
|
httpServer.on("listening", () => {
|
||||||
httpServer.on("listening", () => {
|
onListening(httpServer);
|
||||||
onListening(httpServer);
|
|
||||||
});
|
|
||||||
debugWWW("HTTP listening on " + httpPort.toString());
|
|
||||||
const ntfyStartupConfig = configFunctions.getProperty("application.ntfyStartup");
|
|
||||||
if (ntfyStartupConfig) {
|
|
||||||
const topic = ntfyStartupConfig.topic;
|
|
||||||
const server = ntfyStartupConfig.server;
|
|
||||||
const ntfyMessage = {
|
|
||||||
topic,
|
|
||||||
title: configFunctions.getProperty("application.applicationName"),
|
|
||||||
message: "Application Started",
|
|
||||||
tags: ["arrow_up"]
|
|
||||||
};
|
|
||||||
if (server) {
|
|
||||||
ntfyMessage.server = server;
|
|
||||||
}
|
|
||||||
await ntfyPublish(ntfyMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exitHook(() => {
|
|
||||||
if (httpServer) {
|
|
||||||
debugWWW("Closing HTTP");
|
|
||||||
httpServer.close();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
debug("HTTP listening on " + httpPort.toString());
|
||||||
|
exitHook(() => {
|
||||||
|
debug("Closing HTTP");
|
||||||
|
httpServer.close();
|
||||||
|
});
|
||||||
|
if (ntfyStartupConfig) {
|
||||||
|
const topic = ntfyStartupConfig.topic;
|
||||||
|
const server = ntfyStartupConfig.server;
|
||||||
|
const ntfyStartupMessage = {
|
||||||
|
topic,
|
||||||
|
title: configFunctions.getProperty("application.applicationName"),
|
||||||
|
message: "Application Started",
|
||||||
|
tags: ["arrow_up"]
|
||||||
|
};
|
||||||
|
const ntfyShutdownMessage = {
|
||||||
|
topic,
|
||||||
|
title: configFunctions.getProperty("application.applicationName"),
|
||||||
|
message: "Application Shut Down",
|
||||||
|
tags: ["arrow_down"]
|
||||||
|
};
|
||||||
|
if (server) {
|
||||||
|
ntfyStartupMessage.server = server;
|
||||||
|
ntfyShutdownMessage.server = server;
|
||||||
|
}
|
||||||
|
await ntfyPublish(ntfyStartupMessage);
|
||||||
|
exitHook(() => {
|
||||||
|
debug("Sending ntfy notification");
|
||||||
|
ntfyPublish(ntfyShutdownMessage);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
90
bin/www.ts
90
bin/www.ts
|
|
@ -7,13 +7,12 @@ import http from "node:http";
|
||||||
import * as configFunctions from "../helpers/functions.config.js";
|
import * as configFunctions from "../helpers/functions.config.js";
|
||||||
|
|
||||||
import exitHook from "exit-hook";
|
import exitHook from "exit-hook";
|
||||||
|
|
||||||
import ntfyPublish from "@cityssm/ntfy-publish";
|
import ntfyPublish from "@cityssm/ntfy-publish";
|
||||||
import type * as ntfyTypes from "@cityssm/ntfy-publish/types";
|
import type * as ntfyTypes from "@cityssm/ntfy-publish/types";
|
||||||
|
|
||||||
import debug from "debug";
|
import Debug from "debug";
|
||||||
const debugWWW = debug("lot-occupancy-system:www");
|
const debug = Debug("lot-occupancy-system:www");
|
||||||
|
|
||||||
let httpServer: http.Server;
|
|
||||||
|
|
||||||
interface ServerError extends Error {
|
interface ServerError extends Error {
|
||||||
syscall: string;
|
syscall: string;
|
||||||
|
|
@ -29,14 +28,14 @@ const onError = (error: ServerError) => {
|
||||||
switch (error.code) {
|
switch (error.code) {
|
||||||
// eslint-disable-next-line no-fallthrough
|
// eslint-disable-next-line no-fallthrough
|
||||||
case "EACCES": {
|
case "EACCES": {
|
||||||
debugWWW("Requires elevated privileges");
|
debug("Requires elevated privileges");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
// break;
|
// break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line no-fallthrough
|
// eslint-disable-next-line no-fallthrough
|
||||||
case "EADDRINUSE": {
|
case "EADDRINUSE": {
|
||||||
debugWWW("Port is already in use.");
|
debug("Port is already in use.");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
// break;
|
// break;
|
||||||
}
|
}
|
||||||
|
|
@ -53,52 +52,61 @@ const onListening = (server: http.Server) => {
|
||||||
|
|
||||||
if (addr) {
|
if (addr) {
|
||||||
const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port.toString();
|
const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port.toString();
|
||||||
debugWWW("Listening on " + bind);
|
debug("Listening on " + bind);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Initialize HTTP
|
* Initialize HTTP
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const ntfyStartupConfig = configFunctions.getProperty("application.ntfyStartup");
|
||||||
|
|
||||||
const httpPort = configFunctions.getProperty("application.httpPort");
|
const httpPort = configFunctions.getProperty("application.httpPort");
|
||||||
|
|
||||||
if (httpPort) {
|
const httpServer = http.createServer(app);
|
||||||
httpServer = http.createServer(app);
|
|
||||||
|
|
||||||
httpServer.listen(httpPort);
|
httpServer.listen(httpPort);
|
||||||
|
|
||||||
httpServer.on("error", onError);
|
httpServer.on("error", onError);
|
||||||
httpServer.on("listening", () => {
|
httpServer.on("listening", () => {
|
||||||
onListening(httpServer);
|
onListening(httpServer);
|
||||||
});
|
});
|
||||||
|
|
||||||
debugWWW("HTTP listening on " + httpPort.toString());
|
debug("HTTP listening on " + httpPort.toString());
|
||||||
|
|
||||||
const ntfyStartupConfig = configFunctions.getProperty("application.ntfyStartup");
|
|
||||||
|
|
||||||
if (ntfyStartupConfig) {
|
|
||||||
const topic = ntfyStartupConfig.topic;
|
|
||||||
const server = ntfyStartupConfig.server;
|
|
||||||
|
|
||||||
const ntfyMessage: ntfyTypes.NtfyMessageOptions = {
|
|
||||||
topic,
|
|
||||||
title: configFunctions.getProperty("application.applicationName"),
|
|
||||||
message: "Application Started",
|
|
||||||
tags: ["arrow_up"]
|
|
||||||
};
|
|
||||||
|
|
||||||
if (server) {
|
|
||||||
ntfyMessage.server = server;
|
|
||||||
}
|
|
||||||
|
|
||||||
await ntfyPublish(ntfyMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exitHook(() => {
|
exitHook(() => {
|
||||||
if (httpServer) {
|
debug("Closing HTTP");
|
||||||
debugWWW("Closing HTTP");
|
httpServer.close();
|
||||||
httpServer.close();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (ntfyStartupConfig) {
|
||||||
|
const topic = ntfyStartupConfig.topic;
|
||||||
|
const server = ntfyStartupConfig.server;
|
||||||
|
|
||||||
|
const ntfyStartupMessage: ntfyTypes.NtfyMessageOptions = {
|
||||||
|
topic,
|
||||||
|
title: configFunctions.getProperty("application.applicationName"),
|
||||||
|
message: "Application Started",
|
||||||
|
tags: ["arrow_up"]
|
||||||
|
};
|
||||||
|
|
||||||
|
const ntfyShutdownMessage: ntfyTypes.NtfyMessageOptions = {
|
||||||
|
topic,
|
||||||
|
title: configFunctions.getProperty("application.applicationName"),
|
||||||
|
message: "Application Shut Down",
|
||||||
|
tags: ["arrow_down"]
|
||||||
|
};
|
||||||
|
|
||||||
|
if (server) {
|
||||||
|
ntfyStartupMessage.server = server;
|
||||||
|
ntfyShutdownMessage.server = server;
|
||||||
|
}
|
||||||
|
|
||||||
|
await ntfyPublish(ntfyStartupMessage);
|
||||||
|
|
||||||
|
exitHook(() => {
|
||||||
|
debug("Sending ntfy notification");
|
||||||
|
ntfyPublish(ntfyShutdownMessage);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue