[mod] signalhandling

This commit is contained in:
Christian Fraß 2024-03-04 16:00:41 +01:00
parent 519254c44a
commit 9f602b0fce
5 changed files with 97 additions and 24 deletions

View file

@ -235,7 +235,7 @@ declare namespace lib_plankton.base {
*/ */
function object_merge(core: Record<string, any>, mantle: Record<string, any>): Record<string, any>; function object_merge(core: Record<string, any>, mantle: Record<string, any>): Record<string, any>;
} }
declare module lib_plankton.pod { declare namespace lib_plankton.pod {
/** /**
* @author fenris * @author fenris
*/ */
@ -279,7 +279,7 @@ declare module lib_plankton.pod {
show_value?: ((value: type_value) => string); show_value?: ((value: type_value) => string);
}): string; }): string;
} }
declare module lib_plankton.pod { declare namespace lib_plankton.pod {
/** /**
*/ */
class class_pod<type_value> { class class_pod<type_value> {
@ -693,7 +693,13 @@ declare namespace lib_plankton.call {
* *
* @author fenris * @author fenris
*/ */
function defer<type_result>(seconds: int, action: (() => type_result)): Promise<type_result>; function defer<type_result>(seconds: float, action: (() => type_result)): {
id: int;
result: Promise<type_result>;
};
/**
*/
function deferral_abort(id: int): void;
/** /**
* for rate_limit_check * for rate_limit_check
* *

View file

@ -1621,11 +1621,19 @@ var lib_plankton;
* @author fenris * @author fenris
*/ */
function defer(seconds, action) { function defer(seconds, action) {
return (new Promise((resolve, reject) => { let timeout_id;
setTimeout(() => resolve(action()), seconds); 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; 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: * 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 * - an actor has a fixed mana capacity, i.e. the maximum amount of available power

View file

@ -215,7 +215,7 @@ async function main(
lib_plankton.log.enum_level.notice, lib_plankton.log.enum_level.notice,
lib_plankton.log.enum_level.warning, lib_plankton.log.enum_level.warning,
lib_plankton.log.enum_level.error, 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["database_path"],
args["order_path"], args["order_path"],
async () => { async () => {
await _heimdall.master.clean( let ordered_to_quit : boolean = false;
{ let deferral_id : (null | int) = null;
"time_to_live": args["time_to_live"], const stop = function () {
"erase_state": args["erase_state"], 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( process.on(
order, "SIGTERM",
{ () => {
"send_ok_notifications": args["send_ok_notifications"], 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<void>(
new Promise<void>(
(resolve, reject) => {
while (! ordered_to_quit) {
// laze
}
resolve(undefined);
}
),
5.0
);
await promise;
}
}
} }
) )
) )

View file

@ -114,16 +114,21 @@ namespace _heimdall.master
options.time_to_live, options.time_to_live,
options.erase_state options.erase_state
); );
lib_plankton.log.info( if (count <= 0) {
lib_plankton.translate.get( // do nothing
"misc.cleanup_info", }
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(); return Promise.reject();
} }
else { else {
lib_plankton.log.info("mutex_setting", {});
await lib_plankton.file.write(mutex_path, ""); await lib_plankton.file.write(mutex_path, "");
await procedure(); await procedure();
await lib_plankton.file.delete_(mutex_path); await lib_plankton.file.delete_(mutex_path);
lib_plankton.log.info("mutex_released", {});
} }
} }

View file

@ -384,7 +384,7 @@ namespace _heimdall.order
}, },
), ),
node node
); ) as type_check;
let check : type_check = { let check : type_check = {
"name": node_.name, "name": node_.name,
"title": node_.title, "title": node_.title,