inngest
Version:
Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.
83 lines (81 loc) • 2.43 kB
JavaScript
import { onShutdown } from "../../os.js";
import { ConnectionState } from "../../types.js";
//#region src/components/connect/strategies/core/BaseStrategy.ts
/**
* Base class for connection strategies providing common functionality for state
* management, shutdown signal handling, and close lifecycle.
*/
var BaseStrategy = class {
_state = ConnectionState.CONNECTING;
closingPromise;
resolveClosingPromise;
cleanupShutdownSignal;
internalLogger;
constructor({ logger }) {
this.internalLogger = logger;
this.closingPromise = new Promise((resolve) => {
this.resolveClosingPromise = resolve;
});
}
get state() {
return this._state;
}
get closed() {
return this.closingPromise;
}
/**
* Set up shutdown signal handlers that will trigger close() on SIGINT/SIGTERM.
*/
setupShutdownSignal(signals) {
if (this.cleanupShutdownSignal) return;
this.internalLogger.debug({ signals }, "Setting up shutdown signal handler");
const cleanupShutdownHandlers = onShutdown(signals, () => {
this.internalLogger.debug({ signals }, "Received shutdown signal, closing connection");
this.close();
});
this.cleanupShutdownSignal = () => {
this.internalLogger.debug("Cleaning up shutdown signal handler");
cleanupShutdownHandlers();
};
}
/**
* Clean up shutdown signal handlers. Call at the start of close().
*/
cleanupShutdown() {
if (this.cleanupShutdownSignal) {
this.cleanupShutdownSignal();
this.cleanupShutdownSignal = void 0;
}
}
/**
* Mark the connection as closing. Call after cleanupShutdown().
*/
setClosing() {
this._state = ConnectionState.CLOSING;
}
/**
* Mark the connection as closed and resolve the closing promise.
* Call at the end of close().
*/
setClosed() {
this._state = ConnectionState.CLOSED;
this.resolveClosingPromise?.();
}
/**
* Set up shutdown signals if configured in options.
* Call at the start of connect().
*/
setupShutdownSignalIfConfigured(handleShutdownSignals) {
if (handleShutdownSignals && handleShutdownSignals.length > 0) this.setupShutdownSignal(handleShutdownSignals);
}
/**
* Throw if the connection is closing or closed.
* Call at the start of connect().
*/
throwIfClosingOrClosed() {
if (this._state === ConnectionState.CLOSING || this._state === ConnectionState.CLOSED) throw new Error("Connection already closed");
}
};
//#endregion
export { BaseStrategy };
//# sourceMappingURL=BaseStrategy.js.map