diff --git a/packages/unit/src/Runtime/Runtime.ts b/packages/unit/src/Runtime/Runtime.ts index e14cd6bf..57bee142 100644 --- a/packages/unit/src/Runtime/Runtime.ts +++ b/packages/unit/src/Runtime/Runtime.ts @@ -22,6 +22,8 @@ export class Runtime { this.logger = Logger.factory(this); this.created = new Date(); this.logger.info("register", {ENV, DEBUG}); + this.accept(this.logger); + this.accept(() => new Promise((resolve) => process.nextTick(resolve))); // @TODO Send an event when a signal has been received. for (const signal of Signals) { @@ -34,6 +36,10 @@ export class Runtime { return this[RuntimeRef]; } + public get online() { + return !this.#disposed; + } + public static isDebugEnable() { return DEBUG; } @@ -50,10 +56,6 @@ export class Runtime { return ENV === "test"; } - public get online() { - return !this.#disposed; - } - public static initialize(before?: () => any) { if (this[RuntimeRef]) { return; @@ -79,12 +81,12 @@ export class Runtime { } catch (error) { this.logger.emergency(error.message, error.stack); } finally { + finish(); + this.logger.info("finish"); if (this.online) { await this.release(); } - - finish(); } } diff --git a/packages/unit/src/Runtime/interfaces.ts b/packages/unit/src/Runtime/interfaces.ts index 3e7a261b..df2443a1 100644 --- a/packages/unit/src/Runtime/interfaces.ts +++ b/packages/unit/src/Runtime/interfaces.ts @@ -15,7 +15,7 @@ export interface IDisposableSync { } export interface IDisposable { - dispose(): Promisify; + dispose(): Promisify; } export type DisposableFn = () => Promisify; diff --git a/packages/unit/src/Runtime/internal.ts b/packages/unit/src/Runtime/internal.ts index a47b1161..64e7eb4c 100644 --- a/packages/unit/src/Runtime/internal.ts +++ b/packages/unit/src/Runtime/internal.ts @@ -1,4 +1,5 @@ import {IDisposable, IRunnable} from "./interfaces"; +import {Runtime} from "./Runtime"; export const DisposableSync = Symbol(); export const Signals: NodeJS.Signals[] = ["SIGINT", "SIGQUIT", "SIGTERM"]; @@ -14,3 +15,10 @@ export function isDisposableSync(candidate: any): candidate is IDisposable { export function isRunnable(candidate: any): candidate is IRunnable { return "getHeartbeat" in candidate; } + +export function main(fn: (runtime: Runtime) => any, before?: () => any) { + process.nextTick(async () => { + Runtime.initialize(before); + Runtime.run(fn); + }); +} diff --git a/packages/util/src/Logger/Logger.ts b/packages/util/src/Logger/Logger.ts index 7e8f9d3d..1a3bd949 100644 --- a/packages/util/src/Logger/Logger.ts +++ b/packages/util/src/Logger/Logger.ts @@ -151,6 +151,11 @@ export class Logger { }; } + public dispose() { + writers.splice(0, writers.length); + return Promise.allSettled(transports.map((transport) => transport.close())); + } + public add(child: ILogger) { if (child.logger === this || isDefined(child.logger.groupId) || isUndefined(this.groupId)) { return; diff --git a/packages/util/src/Logger/Transport/InOutTransportAbstract.ts b/packages/util/src/Logger/Transport/InOutTransportAbstract.ts index 40937593..f0b3b586 100644 --- a/packages/util/src/Logger/Transport/InOutTransportAbstract.ts +++ b/packages/util/src/Logger/Transport/InOutTransportAbstract.ts @@ -1,3 +1,4 @@ +import {Promisify} from "../../interfaces"; import {ILoggerTransport, LogFormat, LogMessage} from "../interfaces"; import {defaultLogFormat} from "./formatters"; @@ -23,5 +24,7 @@ export abstract class InOutTransportAbstract implements ILoggerTransport { } } + public abstract close(): Promisify; + protected abstract test(log: LogMessage): boolean; } diff --git a/packages/util/src/Logger/Transport/StdErrorTransport.ts b/packages/util/src/Logger/Transport/StdErrorTransport.ts index f6e2bbe3..fc04ce42 100644 --- a/packages/util/src/Logger/Transport/StdErrorTransport.ts +++ b/packages/util/src/Logger/Transport/StdErrorTransport.ts @@ -9,4 +9,8 @@ export class StdErrorTransport extends InOutTransportAbstract { protected test(log: LogMessage) { return log.severity < SeverityLevel.NOTICE; } + + public close() { + return void 0; + } } diff --git a/packages/util/src/Logger/Transport/StdOutTransport.ts b/packages/util/src/Logger/Transport/StdOutTransport.ts index c4be3b15..eec6fb21 100644 --- a/packages/util/src/Logger/Transport/StdOutTransport.ts +++ b/packages/util/src/Logger/Transport/StdOutTransport.ts @@ -9,4 +9,8 @@ export class StdOutTransport extends InOutTransportAbstract { protected test(log: LogMessage) { return log.severity >= SeverityLevel.NOTICE; } + + public close() { + return void 0; + } } diff --git a/packages/util/src/Logger/interfaces.ts b/packages/util/src/Logger/interfaces.ts index a149a0ae..583cc63e 100644 --- a/packages/util/src/Logger/interfaces.ts +++ b/packages/util/src/Logger/interfaces.ts @@ -16,6 +16,7 @@ export interface ILoggerTransport { readonly writable: boolean; write(log: LogMessage): Promisify; + close(): Promisify; } export interface ILogger {