diff --git a/src/commands/run.ts b/src/commands/run.ts index badebee..a03bedb 100644 --- a/src/commands/run.ts +++ b/src/commands/run.ts @@ -20,12 +20,12 @@ export async function run(options: RunOptions) { process.on("unhandledRejection", async (error) => { log.error("Unhandled promise rejection", error); - stop(1); + await stop(1); }); process.on("SIGINT", async function () { log.info("Caught interrupt signal."); - stop(); + await stop(); }); let exitCode = 0; @@ -54,7 +54,7 @@ export async function run(options: RunOptions) { log.error("Unexpected error thrown when running watchtower", error); exitCode = 1; } finally { - stop(exitCode); + await stop(exitCode); } } @@ -62,12 +62,19 @@ export async function run(options: RunOptions) { * Run actions required when stopping the watch-tower from run mode * @param exitCode Exit code to return to the shell */ -function stop(exitCode?: number) { +async function stop(exitCode?: number) { const log = getLogger("commands:stop"); - log.info("Stopping Rest API server..."); - ApiService.getInstance().stop(); - log.info("Closing database..."); - DBService.getInstance().close(); + const stopServices = [ + ApiService.getInstance().stop(), + DBService.getInstance().close(), + ]; + await Promise.allSettled(stopServices).then((results) => { + results.forEach((result) => { + if (result.status === "rejected") { + log.error("Error stopping service", result.reason); + } + }); + }); log.info("Exiting watchtower..."); process.exit(exitCode || 0); } diff --git a/src/utils/api.ts b/src/utils/api.ts index 500826f..13e2534 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -73,8 +73,8 @@ export class ApiService { throw new Error("Server is not running"); } - const log = getLogger("api"); - log.info("Stopping Rest API server"); + const log = getLogger("api:stop"); + log.info("Stopping Rest API server..."); this.server.once("close", resolve); this.server.close(); diff --git a/src/utils/db.ts b/src/utils/db.ts index d4c5c74..8ed3cd1 100644 --- a/src/utils/db.ts +++ b/src/utils/db.ts @@ -1,4 +1,5 @@ import { DatabaseOptions, Level } from "level"; +import { getLogger } from "./logging"; const DEFAULT_DB_LOCATION = "./database"; @@ -34,6 +35,8 @@ export class DBService { } public async close() { + const log = getLogger("dbService:close"); + log.info("Closing database..."); await this.db.close(); }