import { realpath, stat } from 'node:fs/promises'; import { basename, isAbsolute } from 'node:path'; import { pathToFileURL } from 'node:url'; import { Engine, type EngineOptions, type ExecutionState, type Workflow, } from 'bunqueue/workflow'; import { managedConnection } from '../managedConnection'; import type { ServerConfig } from '../manager'; import { configuredWorkflowModule, messageOf, readyRuntimeStatus, stoppedRuntimeStatus, WorkflowRuntimeUnavailableError, workflowRuntimeOptions, workflowRuntimeSignature, } from './runtimeConfig'; export { WorkflowRuntimeUnavailableError } from './runtimeConfig'; export interface WorkflowRuntimeStatus { configured: boolean; ready: boolean; moduleName?: string; workflowNames: string[]; queueName?: string; concurrency?: number; error?: string; needsInitialization?: boolean; } export interface WorkflowRuntimePort { status(config: ServerConfig, serverRunning?: boolean): Promise; initialize(config: ServerConfig): Promise; reload(config: ServerConfig): Promise; start(config: ServerConfig, workflowName: string, input?: unknown): Promise; signal(config: ServerConfig, id: string, event: string, payload?: unknown): Promise; recover(config: ServerConfig): Promise; resumeCompensation(config: ServerConfig, id: string): Promise; abandonCompensation(config: ServerConfig, id: string): Promise; archive(config: ServerConfig, maxAgeMs: number, states: ExecutionState[]): Promise; cleanup(config: ServerConfig, maxAgeMs: number, states: ExecutionState[]): Promise; close(): Promise; } interface ActiveRuntime { engine: Engine; signature: string; moduleName: string; workflowNames: string[]; queueName: string; concurrency: number; initializationFailure?: string; closeFailure?: string; } interface RuntimeModule { workflows?: Workflow[]; workflowNames?: string[]; registerWorkflowRuntime?: (engine: Engine) => unknown | Promise; default?: (engine: Engine) => unknown | Promise; } export type WorkflowEngineFactory = (options: EngineOptions) => Engine; export class WorkflowRuntime implements WorkflowRuntimePort { private active: ActiveRuntime | null = null; private gate: Promise = Promise.resolve(); constructor( private readonly createEngine: WorkflowEngineFactory = (options) => new Engine(options) ) {} status(config: ServerConfig, serverRunning = true): Promise { return this.serial(async () => { const modulePath = configuredWorkflowModule(config); try { if (!modulePath) { return { configured: false, ready: false, workflowNames: [] }; } if (!serverRunning) { return stoppedRuntimeStatus(config, modulePath); } const active = this.active; if (active?.closeFailure !== undefined) throw quarantinedRuntimeError(active); const path = await realpath(modulePath); const modified = (await stat(path)).mtimeMs; const options = workflowRuntimeOptions(config); if (active?.signature === workflowRuntimeSignature(config, path, modified, options)) { return readyRuntimeStatus(active); } return { configured: true, ready: false, needsInitialization: true, moduleName: basename(modulePath), workflowNames: [], ...options, }; } catch (error) { const active = this.active; return { configured: Boolean(modulePath), ready: false, moduleName: modulePath ? basename(modulePath) : active?.moduleName, workflowNames: [], queueName: active?.queueName, concurrency: active?.concurrency, error: messageOf(error), }; } }); } initialize(config: ServerConfig): Promise { return this.serial(async () => readyRuntimeStatus(await this.ensure(config))); } reload(config: ServerConfig): Promise { return this.serial(async () => { await this.closeActive(); return readyRuntimeStatus(await this.ensure(config)); }); } start(config: ServerConfig, workflowName: string, input?: unknown): Promise { return this.run(config, (engine) => engine.start(workflowName, input)); } signal(config: ServerConfig, id: string, event: string, payload?: unknown): Promise { return this.run(config, (engine) => engine.signal(id, event, payload)); } recover(config: ServerConfig): Promise { return this.run(config, (engine) => engine.recover()); } resumeCompensation(config: ServerConfig, id: string): Promise { return this.run(config, (engine) => engine.resumeCompensation(id)); } abandonCompensation(config: ServerConfig, id: string): Promise { return this.run(config, (engine) => engine.abandonCompensation(id)); } archive( config: ServerConfig, maxAgeMs: number, states: ExecutionState[] ): Promise { return this.run(config, (engine) => engine.archive(maxAgeMs, states)); } cleanup( config: ServerConfig, maxAgeMs: number, states: ExecutionState[] ): Promise { return this.run(config, (engine) => engine.cleanup(maxAgeMs, states)); } close(): Promise { return this.serial(() => this.closeActive()); } private run(config: ServerConfig, operation: (engine: Engine) => Promise | T): Promise { return this.serial(async () => operation((await this.ensure(config)).engine)); } private async ensure(config: ServerConfig): Promise { if (this.active?.closeFailure !== undefined) { throw quarantinedRuntimeError(this.active); } const requested = configuredWorkflowModule(config); if (!requested) { throw new WorkflowRuntimeUnavailableError( 'Set BUNQUEUE_WORKFLOW_MODULE in Server extraEnv to an absolute runtime module path.' ); } if (!isAbsolute(requested)) { throw new WorkflowRuntimeUnavailableError('BUNQUEUE_WORKFLOW_MODULE must be absolute.'); } const path = await realpath(requested).catch(() => { throw new WorkflowRuntimeUnavailableError( 'The configured workflow runtime module was not found.' ); }); const modified = (await stat(path)).mtimeMs; const options = workflowRuntimeOptions(config); const signature = workflowRuntimeSignature(config, path, modified, options); if (this.active?.signature === signature) return this.active; await this.closeActive(); const imported = (await import(`${pathToFileURL(path).href}?v=${modified}`)) as RuntimeModule; const definitions = Array.isArray(imported.workflows) ? imported.workflows : []; const register = imported.registerWorkflowRuntime ?? imported.default; if (!register && definitions.length === 0) { throw new WorkflowRuntimeUnavailableError( 'Workflow module must export workflows[] or registerWorkflowRuntime(engine).' ); } const engine = this.createEngine({ dataPath: config.dataPath, queueName: options.queueName, concurrency: options.concurrency, connection: managedConnection(config), }); const active: ActiveRuntime = { engine, signature, moduleName: basename(path), workflowNames: [], ...options, }; this.active = active; try { const names = new Set(); const originalRegister = engine.register.bind(engine); engine.register = ((workflow: Workflow) => { const registered = originalRegister(workflow); names.add(workflowName(workflow.name)); return registered; }) as Engine['register']; try { for (const workflow of definitions) engine.register(workflow); if (register) await register(engine); } finally { engine.register = originalRegister as Engine['register']; } if (imported.workflowNames !== undefined && !Array.isArray(imported.workflowNames)) { throw new WorkflowRuntimeUnavailableError('workflowNames must be an array of strings.'); } for (const name of imported.workflowNames ?? []) names.add(workflowName(name)); active.workflowNames = Array.from(names).sort(); return active; } catch (error) { try { active.initializationFailure ??= messageOf(error); } finally { await this.closeActive(); } throw error; } } private async closeActive(): Promise { const previous = this.active; if (!previous) return; try { await previous.engine.close(true); } catch (error) { previous.closeFailure ??= messageOf(error); throw quarantinedRuntimeError(previous); } if (this.active === previous) this.active = null; } private serial(operation: () => Promise): Promise { const result = this.gate.then(operation, operation); this.gate = result.then( () => undefined, () => undefined ); return result; } } function quarantinedRuntimeError(active: ActiveRuntime): WorkflowRuntimeUnavailableError { const initialization = active.initializationFailure !== undefined ? ` Initialization failed: ${active.initializationFailure}.` : ''; return new WorkflowRuntimeUnavailableError( `Workflow Engine is quarantined.${initialization} Shutdown failed: ${active.closeFailure}. ` + 'Restart the control agent before running or reloading workflows.' ); } function workflowName(value: unknown): string { if (typeof value !== 'string' || value.length < 1 || value.length > 256) { throw new WorkflowRuntimeUnavailableError( 'workflowNames must contain strings between 1 and 256 characters.' ); } return value; }