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.
98 lines (96 loc) • 3.61 kB
JavaScript
const require_connect = require('../../../../proto/src/components/connect/protobuf/connect.cjs');
const require_buffer = require('../../buffer.cjs');
const require_BaseStrategy = require('../core/BaseStrategy.cjs');
const require_connection = require('../core/connection.cjs');
//#region src/components/connect/strategies/sameThread/index.ts
const ResponseAcknowledgeDeadline = 5e3;
/**
* Same-thread connection strategy.
*
* This strategy runs the WebSocket connection, heartbeater, and lease extender
* in the same thread as user code execution. This is the default strategy.
*/
var SameThreadStrategy = class extends require_BaseStrategy.BaseStrategy {
config;
messageBuffer;
core;
constructor(config) {
super({ logger: config.internalLogger });
this.config = config;
this.core = new require_connection.ConnectionCore({
apiBaseUrl: config.apiBaseUrl,
appIds: Object.keys(config.requestHandlers),
connectionData: config.connectionData,
envName: config.envName,
hashedFallbackKey: config.hashedFallbackKey,
hashedSigningKey: config.hashedSigningKey,
instanceId: config.options.instanceId,
maxWorkerConcurrency: config.options.maxWorkerConcurrency,
mode: config.mode,
gatewayUrl: config.options.gatewayUrl
}, {
logger: this.internalLogger,
onStateChange: (state) => {
this._state = state;
},
getState: () => this._state,
handleExecutionRequest: async (request) => {
const handler = this.config.requestHandlers[request.appName];
if (!handler) throw new Error(`No handler for app: ${request.appName}`);
const response = await handler(request);
const responseBytes = require_connect.SDKResponse.encode(response).finish();
this.messageBuffer.addPending(request.requestId, responseBytes, ResponseAcknowledgeDeadline);
return responseBytes;
},
onReplyAck: (requestId) => {
this.messageBuffer.acknowledgePending(requestId);
},
onBufferResponse: (requestId, responseBytes) => {
this.messageBuffer.append(requestId, responseBytes);
},
beforeConnect: async (signingKey) => {
await this.messageBuffer.flush(signingKey);
}
});
this.messageBuffer = new require_buffer.MessageBuffer({
envName: config.envName,
getApiBaseUrl: () => this.core.getApiBaseUrl(),
logger: this.internalLogger
});
}
get connectionId() {
return this.core.connectionId;
}
async close() {
this.cleanupShutdown();
this.setClosing();
this.internalLogger.debug("Cleaning up connection resources");
await this.core.cleanup();
this.internalLogger.debug("Connection closed");
this.internalLogger.debug("Waiting for in-flight requests to complete");
await this.core.waitForInProgress();
this.internalLogger.debug("Flushing messages before closing");
try {
await this.messageBuffer.flush(this.config.hashedSigningKey);
} catch (err) {
this.internalLogger.debug({ err }, "Failed to flush messages, using fallback key");
await this.messageBuffer.flush(this.config.hashedFallbackKey);
}
this.setClosed();
this.internalLogger.debug("Fully closed");
}
async connect(attempt = 0) {
this.throwIfClosingOrClosed();
this.setupShutdownSignalIfConfigured(this.config.options.handleShutdownSignals);
try {
await this.messageBuffer.flush(this.config.hashedSigningKey);
} catch (err) {
this.internalLogger.debug({ err }, "Failed to flush messages, using fallback key");
await this.messageBuffer.flush(this.config.hashedFallbackKey);
}
await this.core.connect(attempt);
}
};
//#endregion
exports.SameThreadStrategy = SameThreadStrategy;
//# sourceMappingURL=index.cjs.map