@bunt/unit
Advanced tools
Comparing version 0.21.6 to 0.21.7
/// <reference types="node" /> | ||
import { Logger, Promisify } from "@bunt/util"; | ||
import { Logger } from "@bunt/util"; | ||
import { RuntimeTask } from "."; | ||
import { IDisposable, IRunnable } from "./interfaces"; | ||
@@ -7,3 +8,3 @@ export declare const Signals: NodeJS.Signals[]; | ||
export declare function isRunnable(target: unknown): target is IRunnable; | ||
export declare function main(...chain: (() => Promisify<unknown>)[]): void; | ||
export declare function main(...tasks: RuntimeTask[]): void; | ||
export declare const SystemLogger: Logger; |
@@ -15,6 +15,5 @@ "use strict"; | ||
exports.isRunnable = isRunnable; | ||
function main(...chain) { | ||
process.nextTick(async () => { | ||
await Runtime_1.Runtime.run(...chain); | ||
}); | ||
function main(...tasks) { | ||
Runtime_1.Runtime.run(tasks) | ||
.watch(); | ||
} | ||
@@ -21,0 +20,0 @@ exports.main = main; |
@@ -9,3 +9,4 @@ import { Logger } from "@bunt/util"; | ||
private constructor(); | ||
static kill(code?: number): Promise<void>; | ||
static kill(code?: number, reason?: unknown): Promise<void>; | ||
kill(code?: number, reason?: unknown): Promise<void>; | ||
static isDebugEnable(): boolean; | ||
@@ -15,7 +16,7 @@ static isProduction(): boolean; | ||
static isTest(): boolean; | ||
static run(...tasks: RuntimeTask[]): Runtime; | ||
static run(tasks: RuntimeTask[]): Runtime; | ||
private run; | ||
private watch; | ||
watch(): Promise<void>; | ||
private handle; | ||
private error; | ||
} |
@@ -13,3 +13,3 @@ "use strict"; | ||
}; | ||
var _Runtime_running, _Runtime_working, _Runtime_state; | ||
var _Runtime_running, _Runtime_pending, _Runtime_state; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -27,17 +27,23 @@ exports.Runtime = void 0; | ||
_Runtime_running.set(this, []); | ||
_Runtime_working.set(this, []); | ||
_Runtime_pending.set(this, []); | ||
_Runtime_state.set(this, new util_1.Defer()); | ||
this.created = new Date(); | ||
this.logger.info("register", { ENV, DEBUG }); | ||
// @TODO Send an event when a signal has been received. | ||
this.logger.info("run", { ENV, DEBUG, date: this.created }); | ||
for (const signal of internal_1.Signals) { | ||
this.logger.debug("listen", signal); | ||
process.on(signal, async () => Runtime.kill()); | ||
this.logger.debug("watch", signal); | ||
process.on(signal, async () => Runtime.kill(0, `Signal ${signal} has been received`)); | ||
} | ||
this.onDispose(util_1.Logger); | ||
} | ||
static async kill(code = 0) { | ||
const runtime = ref.ensure(); | ||
await Promise.allSettled(__classPrivateFieldGet(runtime, _Runtime_working, "f")); | ||
await (0, Dispose_1.dispose)(runtime); | ||
if (!this.isTest()) { | ||
static kill(code = 0, reason) { | ||
return ref | ||
.ensure() | ||
.kill(code, reason); | ||
} | ||
async kill(code = 0, reason) { | ||
const fn = code > 0 ? this.logger.error : this.logger.info; | ||
fn("Kill { code: %d, reason: %s }", code, reason); | ||
await Promise.allSettled(__classPrivateFieldGet(this, _Runtime_pending, "f")); | ||
await (0, Dispose_1.dispose)(this); | ||
if (!Runtime.isTest()) { | ||
process.exit(code); | ||
@@ -58,3 +64,3 @@ } | ||
} | ||
static run(...tasks) { | ||
static run(tasks) { | ||
return ref | ||
@@ -66,8 +72,3 @@ .once(() => new Runtime()) | ||
for (const task of tasks) { | ||
try { | ||
__classPrivateFieldGet(this, _Runtime_working, "f").push(Promise.resolve(task())); | ||
} | ||
catch (error) { | ||
__classPrivateFieldGet(this, _Runtime_working, "f").push(Promise.reject(error)); | ||
} | ||
__classPrivateFieldGet(this, _Runtime_pending, "f").push(this.handle(task)); | ||
} | ||
@@ -79,3 +80,3 @@ return this; | ||
try { | ||
await Promise.allSettled(__classPrivateFieldGet(this, _Runtime_working, "f").map((job) => this.handle(job))); | ||
await Promise.allSettled(__classPrivateFieldGet(this, _Runtime_pending, "f")); | ||
await Promise.all(__classPrivateFieldGet(this, _Runtime_running, "f").map((heartbeat) => heartbeat.watch())); | ||
@@ -89,8 +90,10 @@ } | ||
} | ||
await (0, Dispose_1.dispose)(this); | ||
const code = __classPrivateFieldGet(this, _Runtime_state, "f").rejected ? 1 : 0; | ||
const reason = __classPrivateFieldGet(this, _Runtime_state, "f").rejected ? "Some tasks were rejected" : "As expected"; | ||
await this.kill(code, reason); | ||
} | ||
async handle(result) { | ||
async handle(task) { | ||
try { | ||
const object = await result; | ||
this.logger.debug("handle", { object }); | ||
this.logger.debug("Run task"); | ||
const object = await task(); | ||
if ((0, util_1.isUndefined)(object) || (0, util_1.isNull)(object)) { | ||
@@ -118,3 +121,3 @@ return; | ||
} | ||
_Runtime_running = new WeakMap(), _Runtime_working = new WeakMap(), _Runtime_state = new WeakMap(); | ||
_Runtime_running = new WeakMap(), _Runtime_pending = new WeakMap(), _Runtime_state = new WeakMap(); | ||
__decorate([ | ||
@@ -121,0 +124,0 @@ util_1.logger |
{ | ||
"name": "@bunt/unit", | ||
"version": "0.21.6", | ||
"version": "0.21.7", | ||
"keywords": [ | ||
@@ -29,6 +29,6 @@ "typescript" | ||
"dependencies": { | ||
"@bunt/util": "^0.21.6" | ||
"@bunt/util": "^0.21.7" | ||
}, | ||
"license": "MIT", | ||
"gitHead": "dfb8b992306230396f0f83c77f50fc56610592f3" | ||
"gitHead": "ac62ace73285765a97d306b1a4fd678d2a9ddfeb" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61641
953
Updated@bunt/util@^0.21.7