Skip to content

Commit

Permalink
fix: runtime und logger
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Apr 1, 2020
1 parent 3007f32 commit 4f0cffe
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 7 deletions.
14 changes: 8 additions & 6 deletions packages/unit/src/Runtime/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -34,6 +36,10 @@ export class Runtime {
return this[RuntimeRef];
}

public get online() {
return !this.#disposed;
}

public static isDebugEnable() {
return DEBUG;
}
Expand All @@ -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;
Expand All @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/unit/src/Runtime/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface IDisposableSync {
}

export interface IDisposable {
dispose(): Promisify<void>;
dispose(): Promisify<void | Disposable | Disposable[]>;
}

export type DisposableFn = () => Promisify<void>;
Expand Down
8 changes: 8 additions & 0 deletions packages/unit/src/Runtime/internal.ts
Original file line number Diff line number Diff line change
@@ -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"];
Expand All @@ -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);
});
}
5 changes: 5 additions & 0 deletions packages/util/src/Logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions packages/util/src/Logger/Transport/InOutTransportAbstract.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Promisify} from "../../interfaces";
import {ILoggerTransport, LogFormat, LogMessage} from "../interfaces";
import {defaultLogFormat} from "./formatters";

Expand All @@ -23,5 +24,7 @@ export abstract class InOutTransportAbstract implements ILoggerTransport {
}
}

public abstract close(): Promisify<void>;

protected abstract test(log: LogMessage): boolean;
}
4 changes: 4 additions & 0 deletions packages/util/src/Logger/Transport/StdErrorTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ export class StdErrorTransport extends InOutTransportAbstract {
protected test(log: LogMessage) {
return log.severity < SeverityLevel.NOTICE;
}

public close() {
return void 0;
}
}
4 changes: 4 additions & 0 deletions packages/util/src/Logger/Transport/StdOutTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ export class StdOutTransport extends InOutTransportAbstract {
protected test(log: LogMessage) {
return log.severity >= SeverityLevel.NOTICE;
}

public close() {
return void 0;
}
}
1 change: 1 addition & 0 deletions packages/util/src/Logger/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ILoggerTransport {
readonly writable: boolean;

write(log: LogMessage): Promisify<void>;
close(): Promisify<void>;
}

export interface ILogger {
Expand Down

0 comments on commit 4f0cffe

Please sign in to comment.