hobots 1.9.1 → 1.9.2

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.
package/dist/index.cjs CHANGED
@@ -161,7 +161,7 @@ function scrubSecrets(text) {
161
161
 
162
162
  // src/utils.ts
163
163
  var SDK_NAME = "hobots";
164
- var SDK_VERSION = true ? "1.9.0" : "0.0.0-dev";
164
+ var SDK_VERSION = true ? "1.9.1" : "0.0.0-dev";
165
165
  function isNode() {
166
166
  return typeof process !== "undefined" && typeof process.versions?.node === "string";
167
167
  }
@@ -1559,6 +1559,7 @@ var MIN_POLL_INTERVAL_MS = 1e3;
1559
1559
  var MAX_CONCURRENCY = 5;
1560
1560
  var POLL_RETRY_DELAYS_MS = [1e3, 4e3, 16e3];
1561
1561
  var AUTH_FAILURE_INTERVAL_MS = 3e4;
1562
+ var DEFAULT_CANCEL_GRACE_MS = 1e4;
1562
1563
  function hostname() {
1563
1564
  if (!isNode()) return void 0;
1564
1565
  return process.env.HOSTNAME || process.env.COMPUTERNAME || void 0;
@@ -1577,6 +1578,7 @@ var TaskAgent = class {
1577
1578
  this.taskTimeoutMs = options.taskTimeoutMs;
1578
1579
  this.logFlushIntervalMs = options.logFlushIntervalMs;
1579
1580
  this.cancelCheckIntervalMs = options.cancelCheckIntervalMs;
1581
+ this.cancelGraceMs = options.cancelGraceMs ?? DEFAULT_CANCEL_GRACE_MS;
1580
1582
  this.logger.debug("agente configurado", { baseUrl: config.tasksBaseUrl });
1581
1583
  }
1582
1584
  logger;
@@ -1588,6 +1590,7 @@ var TaskAgent = class {
1588
1590
  taskTimeoutMs;
1589
1591
  logFlushIntervalMs;
1590
1592
  cancelCheckIntervalMs;
1593
+ cancelGraceMs;
1591
1594
  state = "idle";
1592
1595
  timer;
1593
1596
  pollAbort;
@@ -1772,14 +1775,26 @@ var TaskAgent = class {
1772
1775
  }
1773
1776
  async invokeWithTimeout(handler, run, reporter) {
1774
1777
  const invocation = Promise.resolve().then(() => handler(run.params, reporter.context));
1775
- if (!this.taskTimeoutMs || this.taskTimeoutMs <= 0) {
1778
+ const races = [];
1779
+ if (this.taskTimeoutMs && this.taskTimeoutMs > 0) {
1780
+ races.push(sleep(this.taskTimeoutMs).then(() => TIMEOUT_SENTINEL));
1781
+ }
1782
+ if (this.cancelGraceMs > 0) {
1783
+ races.push(waitForCancelGrace(reporter.context.signal, this.cancelGraceMs).then(() => CANCEL_SENTINEL));
1784
+ }
1785
+ if (races.length === 0) {
1776
1786
  await invocation;
1777
1787
  return;
1778
1788
  }
1789
+ const outcome = await Promise.race([invocation, ...races]);
1790
+ if (outcome !== TIMEOUT_SENTINEL && outcome !== CANCEL_SENTINEL) return;
1791
+ if (outcome === CANCEL_SENTINEL) {
1792
+ this.logger.warn(
1793
+ `run ${run.id} cancelada \u2014 handler n\xE3o respondeu a ctx.signal em ${this.cancelGraceMs}ms, liberando o slot (o handler pode continuar rodando em segundo plano)`
1794
+ );
1795
+ return;
1796
+ }
1779
1797
  const timeoutMs = this.taskTimeoutMs;
1780
- const timeout = sleep(timeoutMs).then(() => TIMEOUT_SENTINEL);
1781
- const outcome = await Promise.race([invocation, timeout]);
1782
- if (outcome !== TIMEOUT_SENTINEL) return;
1783
1798
  reporter.abort();
1784
1799
  invocation.then(() => this.logger.warn(`run ${run.id} terminou depois do timeout de ${timeoutMs}ms`)).catch(() => this.logger.warn(`run ${run.id} falhou depois do timeout de ${timeoutMs}ms (erro descartado)`));
1785
1800
  throw Object.assign(new Error(`timeout ap\xF3s ${timeoutMs}ms (o handler pode continuar em background)`), {
@@ -1788,6 +1803,16 @@ var TaskAgent = class {
1788
1803
  }
1789
1804
  };
1790
1805
  var TIMEOUT_SENTINEL = /* @__PURE__ */ Symbol("task-timeout");
1806
+ var CANCEL_SENTINEL = /* @__PURE__ */ Symbol("task-canceled");
1807
+ function waitForCancelGrace(signal, graceMs) {
1808
+ return new Promise((resolve) => {
1809
+ if (signal.aborted) {
1810
+ setTimeout(resolve, graceMs);
1811
+ return;
1812
+ }
1813
+ signal.addEventListener("abort", () => setTimeout(resolve, graceMs), { once: true });
1814
+ });
1815
+ }
1791
1816
  var MAX_ERROR_MESSAGE_CHARS = 1e4;
1792
1817
  var MAX_ERROR_STACK_CHARS = 2e4;
1793
1818
  function toRunError(error) {