diff --git a/lib/plankton/plankton.d.ts b/lib/plankton/plankton.d.ts index b6ac230..473f2ed 100644 --- a/lib/plankton/plankton.d.ts +++ b/lib/plankton/plankton.d.ts @@ -235,7 +235,7 @@ declare namespace lib_plankton.base { */ function object_merge(core: Record, mantle: Record): Record; } -declare module lib_plankton.pod { +declare namespace lib_plankton.pod { /** * @author fenris */ @@ -279,7 +279,7 @@ declare module lib_plankton.pod { show_value?: ((value: type_value) => string); }): string; } -declare module lib_plankton.pod { +declare namespace lib_plankton.pod { /** */ class class_pod { @@ -693,7 +693,13 @@ declare namespace lib_plankton.call { * * @author fenris */ - function defer(seconds: int, action: (() => type_result)): Promise; + function defer(seconds: float, action: (() => type_result)): { + id: int; + result: Promise; + }; + /** + */ + function deferral_abort(id: int): void; /** * for rate_limit_check * diff --git a/lib/plankton/plankton.js b/lib/plankton/plankton.js index 9dedef8..407f550 100644 --- a/lib/plankton/plankton.js +++ b/lib/plankton/plankton.js @@ -1621,11 +1621,19 @@ var lib_plankton; * @author fenris */ function defer(seconds, action) { - return (new Promise((resolve, reject) => { - setTimeout(() => resolve(action()), seconds); - })); + let timeout_id; + const result_promise = new Promise((resolve, reject) => { + timeout_id = setTimeout(() => resolve(action()), Math.floor(seconds * 1000)); + }); + return { "id": timeout_id, "result": result_promise }; } call.defer = defer; + /** + */ + function deferral_abort(id) { + clearTimeout(id); + } + call.deferral_abort = deferral_abort; /** * rate limiting algorithm, based on the idea of mana (magic power) in video games: * - an actor has a fixed mana capacity, i.e. the maximum amount of available power diff --git a/source/app/cli.ts b/source/app/cli.ts index 367210d..67ff65f 100644 --- a/source/app/cli.ts +++ b/source/app/cli.ts @@ -215,7 +215,7 @@ async function main( lib_plankton.log.enum_level.notice, lib_plankton.log.enum_level.warning, lib_plankton.log.enum_level.error, - ][Math.min(4, args["verbosity"])] + ][Math.min(4, /*args["verbosity"]*/0)] ), ] ); @@ -278,18 +278,70 @@ async function main( args["database_path"], args["order_path"], async () => { - await _heimdall.master.clean( - { - "time_to_live": args["time_to_live"], - "erase_state": args["erase_state"], + let ordered_to_quit : boolean = false; + let deferral_id : (null | int) = null; + const stop = function () { + ordered_to_quit = true; + if (deferral_id === null) { + // do nothing + } + else { + lib_plankton.call.deferral_abort(deferral_id); + } + }; + process.on( + "SIGINT", + () => { + lib_plankton.log.info("main_received_sigint", {}); + stop(); } ); - await _heimdall.master.run( - order, - { - "send_ok_notifications": args["send_ok_notifications"], + process.on( + "SIGTERM", + () => { + lib_plankton.log.info("main_received_sigterm", {}); + stop(); } ); + while (! ordered_to_quit) { + // cleanup + { + lib_plankton.log.debug("cleanup_starting", {}); + await _heimdall.master.clean( + { + "time_to_live": args["time_to_live"], + "erase_state": args["erase_state"], + } + ); + lib_plankton.log.debug("cleanup_ended", {}); + } + // run checks + { + lib_plankton.log.debug("checks_starting", {}); + await _heimdall.master.run( + order, + { + "send_ok_notifications": args["send_ok_notifications"], + } + ); + lib_plankton.log.debug("checks_ended", {}); + } + // pause + { + let {timeout_id, promise} = lib_plankton.call.timeout_start( + new Promise( + (resolve, reject) => { + while (! ordered_to_quit) { + // laze + } + resolve(undefined); + } + ), + 5.0 + ); + await promise; + } + } } ) ) diff --git a/source/logic/master.ts b/source/logic/master.ts index d71dab2..f950647 100644 --- a/source/logic/master.ts +++ b/source/logic/master.ts @@ -114,16 +114,21 @@ namespace _heimdall.master options.time_to_live, options.erase_state ); - lib_plankton.log.info( - lib_plankton.translate.get( - "misc.cleanup_info", + if (count <= 0) { + // do nothing + } + else { + lib_plankton.log.info( + lib_plankton.translate.get( + "misc.cleanup_info", + { + "count": count.toFixed(0), + } + ), { - "count": count.toFixed(0), } - ), - { - } - ); + ); + } } @@ -189,9 +194,11 @@ namespace _heimdall.master return Promise.reject(); } else { + lib_plankton.log.info("mutex_setting", {}); await lib_plankton.file.write(mutex_path, ""); await procedure(); await lib_plankton.file.delete_(mutex_path); + lib_plankton.log.info("mutex_released", {}); } } diff --git a/source/logic/order.ts b/source/logic/order.ts index 2b17fa2..f888656 100644 --- a/source/logic/order.ts +++ b/source/logic/order.ts @@ -384,7 +384,7 @@ namespace _heimdall.order }, ), node - ); + ) as type_check; let check : type_check = { "name": node_.name, "title": node_.title,