Skip to content

Commit

Permalink
Web server sometimes leaves the port used
Browse files Browse the repository at this point in the history
Fixes #280
  • Loading branch information
neilenns committed Jun 20, 2020
1 parent 0be40b4 commit cfd1ddf
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,30 @@ async function startup(): Promise<void> {
}
}

async function hotLoadSettings(path: string) {
log.info("Main", `${path} change detected, reloading.`);

/**
* Shuts down all registered file system watchers and the web server
*/
async function shutdown(): Promise<void> {
// Shut down things that are running
await TriggerManager.stopWatching();
WebServer.stopApp();
}

/**
* Shuts everything down and then restarts the service with a new settings file.
* @param path The path to the settings file that changed.
*/
async function hotLoadSettings(path: string) {
log.info("Main", `${path} change detected, reloading.`);

// Start it all back up again
await shutdown();
await startup();
}

/**
* Reloads the list of triggers.
* @param path The path to the trigger file that changed.
*/
async function hotLoadTriggers(path: string) {
log.info("Main", `${path} change detected, reloading.`);

Expand Down Expand Up @@ -142,7 +155,22 @@ function startWatching(): void {
}
}

async function main() {
async function handleDeath(): Promise<void> {
log.info("Main", "Shutting down.");
await shutdown();
process.exit();
}

function registerForDeath(): void {
process.on("SIGINT", handleDeath);
process.on("SIGTERM", handleDeath);
process.on("SIGQUIT", handleDeath);
process.on("SIGBREAK", handleDeath);
}

async function main(): Promise<void> {
registerForDeath();

await startup();

startWatching();
Expand Down

0 comments on commit cfd1ddf

Please sign in to comment.