mirror of
https://github.com/n8n-io/n8n.git
synced 2026-12-04 00:30:34 +00:00
249 lines
9.3 KiB
TypeScript
249 lines
9.3 KiB
TypeScript
import type { StoredWorkflowGraph } from '../graph';
|
|
import type { LifecycleEventSubscription } from '../lifecycle-events';
|
|
import type { ResponseExpectation } from '../response-channel';
|
|
import type {
|
|
CallerContext,
|
|
EndedExecutionStatus,
|
|
ErrorSourceLink,
|
|
ExecutionMode,
|
|
ExecutionStatus,
|
|
ParentLink,
|
|
SeededSteps,
|
|
StepKey,
|
|
StepKeyId,
|
|
StepSlots,
|
|
TriggerOutputs,
|
|
WorkflowDocument,
|
|
} from './execution.types';
|
|
|
|
/** The fields the write side supplies and the execution path reads back. */
|
|
interface BaseExecutionRecord {
|
|
/** Caller-minted id. The store never mints one. */
|
|
id: string;
|
|
workflowId: string;
|
|
status: ExecutionStatus;
|
|
mode: ExecutionMode;
|
|
/** As stored: the engine's own marks on top of what the caller sent. */
|
|
graph: StoredWorkflowGraph;
|
|
/** Stored for the read path only. Nothing on the execution path reads it. */
|
|
workflow: WorkflowDocument;
|
|
triggerOutputs: TriggerOutputs | null;
|
|
callerContext: CallerContext;
|
|
/** What kind of a response the caller expects. */
|
|
responseExpectation: ResponseExpectation;
|
|
/** The parent link, or `null` for a top-level execution. */
|
|
parent: ParentLink | null;
|
|
/** Which lifecycle events the caller subscribes to. Missing or `null` means none. */
|
|
lifecycleEventSubscription?: LifecycleEventSubscription | null;
|
|
}
|
|
|
|
/**
|
|
* A new execution to persist. Timestamps are assigned by the store. The seeded
|
|
* outputs are stored beside the row, in their own table, and read back by key.
|
|
*
|
|
* The error fields are written here and never read back into a record: the
|
|
* read path reports the error source, and only the SQL that ends an execution
|
|
* reads the flag.
|
|
*/
|
|
export type NewExecutionRecord = BaseExecutionRecord & {
|
|
seededSteps?: SeededSteps;
|
|
/** The execution this error execution reports on, or `null`. */
|
|
errorSource: ErrorSourceLink | null;
|
|
/** Whether a failure of this execution resolves an error workflow. See `StartExecutionRequest`. */
|
|
resolveErrorWorkflowOnFailure: boolean;
|
|
};
|
|
|
|
/**
|
|
* What running an execution needs of its row. The execution path decides on
|
|
* `status`, never on when anything happened, so the only time it carries is
|
|
* `finishedAt`, which it reports but never decides on. The read path has its
|
|
* own view (`ExecutionView`).
|
|
*/
|
|
export type ExecutionRecord = BaseExecutionRecord & {
|
|
/** When the execution ended, or `null` while it has not. */
|
|
finishedAt: Date | null;
|
|
};
|
|
|
|
/** What `createExecution` did with the record. */
|
|
export interface CreateExecutionResult {
|
|
/**
|
|
* `true` when the record has an error source and that source already has an
|
|
* error execution.
|
|
*/
|
|
errorExecutionAlreadyExists: boolean;
|
|
}
|
|
|
|
/** An execution that is still `queued` after its `stale_after` time. */
|
|
export interface StaleQueuedExecution {
|
|
id: string;
|
|
staleAfter: Date;
|
|
}
|
|
|
|
/** An ended execution that the retention sweep can delete. */
|
|
export interface PrunableExecution {
|
|
id: string;
|
|
workflowId: string;
|
|
finishedAt: Date;
|
|
}
|
|
|
|
export interface PrunableExecutionQuery {
|
|
/** Select an execution that ended more than this long ago. */
|
|
maxAgeMs: number;
|
|
/** Also select an execution at or before this one, by finish time and id. See `findRetentionCountCutoff`. */
|
|
countCutoff?: Pick<PrunableExecution, 'id' | 'finishedAt'>;
|
|
limit: number;
|
|
/** The last row of the previous batch. The result starts after it. */
|
|
after?: Pick<PrunableExecution, 'id' | 'finishedAt'>;
|
|
}
|
|
|
|
/** Thrown by `loadExecution` when no execution exists for the given id. */
|
|
export class ExecutionNotFoundError extends Error {
|
|
constructor(readonly executionId: string) {
|
|
super(`Execution not found: ${executionId}`);
|
|
this.name = 'ExecutionNotFoundError';
|
|
}
|
|
}
|
|
|
|
/** Thrown by `createExecution` when the parent of a sub-execution has ended. */
|
|
export class ParentExecutionEndedError extends Error {
|
|
constructor(
|
|
readonly parentExecutionId: string,
|
|
readonly status: ExecutionStatus,
|
|
) {
|
|
super(`Parent execution ${parentExecutionId} is ${status}`);
|
|
this.name = 'ParentExecutionEndedError';
|
|
}
|
|
}
|
|
|
|
/** Whether `error` says that execution `executionId` has no row. */
|
|
export function isMissingExecution(error: unknown, executionId: string): boolean {
|
|
return error instanceof ExecutionNotFoundError && error.executionId === executionId;
|
|
}
|
|
|
|
/**
|
|
* Persistence interface for executions.
|
|
*
|
|
* A write that makes an execution `queued` also records when the engine treats
|
|
* it as stuck in that status (see `StalenessThresholds`). A transition out of
|
|
* `queued` clears that time.
|
|
*
|
|
* Only `finishExecution` and `cancelExecution` move a `queued` or live
|
|
* execution to an ended status, so a host that wraps the store sees every end.
|
|
*/
|
|
export interface ExecutionStore {
|
|
/**
|
|
* Persist a new execution record under the caller-minted `record.id`.
|
|
*
|
|
* A record with a parent link is written only while the parent has not
|
|
* ended, and the check holds the parent row until the write commits. So an
|
|
* end of the parent either waits and then finds this sub-execution, or
|
|
* commits first and this write refuses.
|
|
*
|
|
* A record with an error source link clears the pending marker of the
|
|
* source in the same transaction. When the source already has an error
|
|
* execution, nothing is written and the result sets `errorExecutionAlreadyExists`.
|
|
*
|
|
* @throws {ExecutionNotFoundError} when the parent row is missing.
|
|
* @throws {ParentExecutionEndedError} when the parent has ended.
|
|
*/
|
|
createExecution(record: NewExecutionRecord): Promise<CreateExecutionResult>;
|
|
|
|
/** Load a full execution by id. Throws `ExecutionNotFoundError` if absent. */
|
|
loadExecution(id: string): Promise<ExecutionRecord>;
|
|
|
|
/** Load only an execution's status. Throws `ExecutionNotFoundError` if absent. */
|
|
loadExecutionStatus(id: string): Promise<ExecutionStatus>;
|
|
|
|
/**
|
|
* The outputs the caller seeded for the given steps, by step key id. A key
|
|
* with no seeded outputs is absent from the result.
|
|
*/
|
|
loadSeededOutputs(executionId: string, keys: StepKey[]): Promise<Map<StepKeyId, StepSlots>>;
|
|
|
|
/**
|
|
* Compare-and-set status transition. Returns `true` iff this call performed
|
|
* the transition, so duplicate/redelivered events are handled idempotently.
|
|
*/
|
|
transitionStatus(
|
|
id: string,
|
|
from: ExecutionStatus,
|
|
to: Exclude<ExecutionStatus, EndedExecutionStatus>,
|
|
): Promise<boolean>;
|
|
|
|
/**
|
|
* Record an execution's outcome: writes the final status and the finish time
|
|
* together, as a compare-and-set on the live statuses, so they can't be
|
|
* observed apart. Returns the finish time it wrote, so the caller reports the
|
|
* time the row records, or `null` when the compare-and-set lost.
|
|
*
|
|
* A `failed` outcome of an execution with `resolveErrorWorkflowOnFailure` also
|
|
* sets the pending error workflow marker, in the same write.
|
|
*/
|
|
finishExecution(id: string, status: 'completed' | 'failed'): Promise<{ finishedAt: Date } | null>;
|
|
|
|
/**
|
|
* End an execution on request, from any status that has not ended. Returns
|
|
* the finish time it wrote, so the caller reports the time the row records,
|
|
* or `null` when the execution had already ended.
|
|
*/
|
|
cancelExecution(id: string): Promise<{ finishedAt: Date } | null>;
|
|
|
|
/**
|
|
* The executions that are `queued` and whose `stale_after` time is `at` or
|
|
* earlier, oldest first, at most `limit`. A plain read: it locks no row.
|
|
*/
|
|
findStaleQueuedExecutions(at: Date, limit: number): Promise<StaleQueuedExecution[]>;
|
|
|
|
/**
|
|
* Gives a `queued` execution a new `stale_after` time. A compare-and-set on
|
|
* the status and on the `staleAfter` value that the caller read, so only one
|
|
* of several concurrent callers gets `true`.
|
|
*/
|
|
renewQueuedExecution(id: string, staleAfter: Date): Promise<boolean>;
|
|
|
|
/**
|
|
* Sets a live execution's status from the state of its steps: `waiting` when
|
|
* every step it still owes is suspended, `running` when one step can run.
|
|
*
|
|
* Call this when a step suspends and when a step settles. A resume does not
|
|
* call it, so the status can lag until the resumed step settles.
|
|
*
|
|
* The refresh takes no lock, so it can lose a race and confuse `running`
|
|
* with `waiting`. Callers may test whether the execution ended, as both step
|
|
* handlers do. Read the step rows to tell `running` from `waiting`.
|
|
*/
|
|
refreshLiveStatus(id: string): Promise<void>;
|
|
|
|
/**
|
|
* The ids of the sub-executions of an execution that have not ended: `queued`,
|
|
* `running` or `waiting`. A cancel cascades through them.
|
|
*/
|
|
loadLiveSubExecutionIds(parentExecutionId: string): Promise<string[]>;
|
|
|
|
/**
|
|
* The ended executions that finished more than `maxAgeMs` ago or that are
|
|
* at or before `countCutoff`, oldest first by finish time and id, at most
|
|
* `limit`. The database clock sets the age cutoff. It skips an execution
|
|
* that still has a step that has not settled, and an awaited sub-execution
|
|
* whose parent step still waits. A plain read: it locks no row.
|
|
*/
|
|
findPrunableExecutions(query: PrunableExecutionQuery): Promise<PrunableExecution[]>;
|
|
|
|
/**
|
|
* The newest ended execution that is not among the newest `maxCount` ended
|
|
* executions, by finish time and id. `undefined` when there are `maxCount`
|
|
* or fewer. The count includes the ended executions that
|
|
* `findPrunableExecutions` skips.
|
|
*/
|
|
findRetentionCountCutoff(
|
|
maxCount: number,
|
|
): Promise<Pick<PrunableExecution, 'id' | 'finishedAt'> | undefined>;
|
|
|
|
/**
|
|
* Deletes the given executions that have ended, with their step rows and
|
|
* seeded rows. It skips an execution that has not ended. Returns the number
|
|
* of executions it deleted.
|
|
*/
|
|
deleteEndedExecutions(ids: string[]): Promise<number>;
|
|
}
|