@nmakarov/cli-toolkit 0.73.0 → 0.75.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2810,6 +2810,7 @@ function setup(opts = {}) {
2810
2810
  const partialContext = {
2811
2811
  emitter: new import_events.EventEmitter(),
2812
2812
  isStop: () => false,
2813
+ isKill: () => false,
2813
2814
  cleanupFunctions: [],
2814
2815
  registerCleanup: (fn) => {
2815
2816
  partialContext.cleanupFunctions.push(fn);
@@ -2831,6 +2832,7 @@ function setup(opts = {}) {
2831
2832
  logger,
2832
2833
  emitter: partialContext.emitter,
2833
2834
  isStop: partialContext.isStop,
2835
+ isKill: partialContext.isKill,
2834
2836
  cleanupFunctions: partialContext.cleanupFunctions,
2835
2837
  registerCleanup: partialContext.registerCleanup,
2836
2838
  // For long-running scripts (servers): with --showUsedParams=top, print
@@ -2880,6 +2882,7 @@ function printAllParameters(context) {
2880
2882
  }
2881
2883
  async function init(flow2, opts = {}) {
2882
2884
  let stop = false;
2885
+ let kill = false;
2883
2886
  let context = null;
2884
2887
  let cleanupRan = false;
2885
2888
  const runRegisteredCleanups = async (ctx) => {
@@ -2909,6 +2912,7 @@ async function init(flow2, opts = {}) {
2909
2912
  }
2910
2913
  context = setup(opts);
2911
2914
  context.isStop = () => stop;
2915
+ context.isKill = () => kill;
2912
2916
  context = await setupModules(context, opts);
2913
2917
  const stopAfter = context.args.get("stopAfter");
2914
2918
  const stopAllowanceSec = Number(context.params.get("stopAllowance", "number default 60"));
@@ -2947,6 +2951,14 @@ async function init(flow2, opts = {}) {
2947
2951
  );
2948
2952
  context.emitter.emit("stop", stopAllowanceMs);
2949
2953
  });
2954
+ process.on("SIGUSR2", () => {
2955
+ if (!context || kill) return;
2956
+ kill = true;
2957
+ stop = true;
2958
+ context.logger.warn(">> SIGUSR2: emitting kill (urgent stop)");
2959
+ context.emitter.emit("kill");
2960
+ context.emitter.emit("stop", stopAllowanceMs);
2961
+ });
2950
2962
  await flow2(context);
2951
2963
  } catch (error) {
2952
2964
  const errorLocation = error instanceof Error && error.stack ? error.stack.split("\n")[1]?.trim() || "Unknown location" : "Unknown location";
@@ -2973,7 +2985,7 @@ async function init(flow2, opts = {}) {
2973
2985
  }
2974
2986
  if (process.exitCode && process.exitCode !== 0) {
2975
2987
  process.exit(process.exitCode);
2976
- } else if (stop) {
2988
+ } else if (stop || kill) {
2977
2989
  process.exit(0);
2978
2990
  }
2979
2991
  }