@sentio/runtime 3.4.2-rc.1 → 3.4.3-rc.1

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.
@@ -30455,6 +30455,10 @@ var options = {
30455
30455
  target: program2.args[program2.args.length - 1]
30456
30456
  };
30457
30457
  var logLevel = process.env["LOG_LEVEL"]?.toLowerCase();
30458
+ var isParentShuttingDown = false;
30459
+ var areChildrenShuttingDown = false;
30460
+ var shutdownChildren = () => {
30461
+ };
30458
30462
  setupLogger(options.logFormat === "json", logLevel === "debug" ? true : options.debug);
30459
30463
  console.debug("Starting with", options.target);
30460
30464
  await setupOTLP(options.otlpDebug);
@@ -30462,6 +30466,13 @@ Error.stackTraceLimit = 20;
30462
30466
  configureEndpoints(options);
30463
30467
  console.debug("Starting Server", options);
30464
30468
  var isChildProcess = process.env["IS_CHILD"] === "true";
30469
+ function exitProcess(exitCode) {
30470
+ if (!isChildProcess) {
30471
+ isParentShuttingDown = true;
30472
+ shutdownChildren();
30473
+ }
30474
+ process.exit(exitCode);
30475
+ }
30465
30476
  if (!isChildProcess) {
30466
30477
  const sdkPackageJson = locatePackageJson("@sentio/sdk");
30467
30478
  const runtimePackageJson = locatePackageJson("@sentio/runtime");
@@ -30562,7 +30573,19 @@ if (!isChildProcess) {
30562
30573
  }, 1e3 * 60);
30563
30574
  }
30564
30575
  }
30565
- process.on("SIGINT", function() {
30576
+ process.once("SIGINT", function() {
30577
+ if (isParentShuttingDown) {
30578
+ return;
30579
+ }
30580
+ isParentShuttingDown = true;
30581
+ shutdownChildren();
30582
+ shutdownServers(0);
30583
+ }).once("SIGTERM", function() {
30584
+ if (isParentShuttingDown) {
30585
+ return;
30586
+ }
30587
+ isParentShuttingDown = true;
30588
+ shutdownChildren();
30566
30589
  shutdownServers(0);
30567
30590
  }).on("uncaughtException", (err) => {
30568
30591
  console.error("Uncaught Exception, please checking if await is properly used", err);
@@ -30601,17 +30624,18 @@ function shutdownServers(exitCode) {
30601
30624
  if (httpServer) {
30602
30625
  httpServer.close(function() {
30603
30626
  console.log("Http server shut down");
30604
- process.exit(exitCode);
30627
+ exitProcess(exitCode);
30605
30628
  });
30606
30629
  } else {
30607
- process.exit(exitCode);
30630
+ exitProcess(exitCode);
30608
30631
  }
30609
30632
  }
30610
30633
  if (options.worker > 1 && !isChildProcess) {
30611
- const childProcesses = [];
30634
+ const childProcesses = /* @__PURE__ */ new Map();
30635
+ const restartTimers = /* @__PURE__ */ new Map();
30612
30636
  const basePort = parseInt(options.port);
30613
- for (let i = 1; i < options.worker; i++) {
30614
- const childPort = basePort + i;
30637
+ const restartDelayMs = 1e3;
30638
+ const spawnChild = (childPort) => {
30615
30639
  const args = process.argv.slice(2);
30616
30640
  const childArgs = ["--port=" + String(childPort)];
30617
30641
  for (let j = 0; j < args.length; j++) {
@@ -30634,19 +30658,48 @@ if (options.worker > 1 && !isChildProcess) {
30634
30658
  child.on("error", (err) => {
30635
30659
  console.error(`Child process on port ${childPort} error:`, err);
30636
30660
  });
30637
- child.on("exit", (code) => {
30638
- console.log(`Child process on port ${childPort} exited with code ${code}`);
30661
+ child.on("exit", (code, signal) => {
30662
+ childProcesses.delete(childPort);
30663
+ console.log(`Child process on port ${childPort} exited with code ${code}, signal ${signal ?? "none"}`);
30664
+ const isNormalShutdown = signal === null && code === 0;
30665
+ if (isParentShuttingDown || isNormalShutdown) {
30666
+ return;
30667
+ }
30668
+ if (restartTimers.has(childPort)) {
30669
+ return;
30670
+ }
30671
+ console.log(`Scheduling restart for child process on port ${childPort} in ${restartDelayMs}ms`);
30672
+ const timer = setTimeout(() => {
30673
+ restartTimers.delete(childPort);
30674
+ if (isParentShuttingDown) {
30675
+ return;
30676
+ }
30677
+ console.log(`Restarting child process on port ${childPort}`);
30678
+ spawnChild(childPort);
30679
+ }, restartDelayMs);
30680
+ restartTimers.set(childPort, timer);
30639
30681
  });
30640
- childProcesses.push(child);
30641
- console.log(`Spawned child server process for port ${childPort}`);
30682
+ childProcesses.set(childPort, child);
30683
+ console.log(`Spawned child server process for port ${childPort} (pid: ${child.pid})`);
30684
+ };
30685
+ for (let i = 1; i < options.worker; i++) {
30686
+ const childPort = basePort + i;
30687
+ spawnChild(childPort);
30642
30688
  }
30643
- const shutdownChildren = () => {
30644
- for (const child of childProcesses) {
30689
+ shutdownChildren = () => {
30690
+ if (areChildrenShuttingDown) {
30691
+ return;
30692
+ }
30693
+ areChildrenShuttingDown = true;
30694
+ isParentShuttingDown = true;
30695
+ for (const timer of restartTimers.values()) {
30696
+ clearTimeout(timer);
30697
+ }
30698
+ restartTimers.clear();
30699
+ for (const child of childProcesses.values()) {
30645
30700
  child.kill("SIGINT");
30646
30701
  }
30647
30702
  };
30648
- process.on("SIGINT", shutdownChildren);
30649
- process.on("SIGTERM", shutdownChildren);
30650
30703
  }
30651
30704
  import("node:process").then((p) => p.stdout.write(""));
30652
30705
  /*! Bundled license information: