@atolis-hq/wake 0.4.61 → 0.4.62
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/src/bootstrap/composition-root.d.ts +4 -1
- package/dist/src/bootstrap/composition-root.js +5 -2
- package/dist/src/bootstrap/integration-runtime.d.ts +4 -2
- package/dist/src/bootstrap/integration-runtime.js +74 -13
- package/dist/src/bootstrap/trigger-checkpoint-store.d.ts +5 -5
- package/dist/src/bootstrap/trigger-checkpoint-store.js +15 -40
- package/dist/src/bootstrap/version.js +1 -1
- package/dist/src/control-plane/application/control-plane-projection.js +11 -1
- package/dist/src/control-plane/application/runner-pipeline.js +0 -2
- package/dist/src/control-plane/application/trigger-facts.d.ts +11 -0
- package/dist/src/control-plane/application/trigger-facts.js +38 -0
- package/dist/src/control-plane/application/trigger-invocation-projection.d.ts +4 -0
- package/dist/src/control-plane/application/trigger-invocation-projection.js +32 -0
- package/dist/src/control-plane/application/trigger-projection.d.ts +24 -0
- package/dist/src/control-plane/application/trigger-projection.js +98 -0
- package/dist/src/control-plane/application/trigger-reactor.d.ts +4 -0
- package/dist/src/control-plane/application/trigger-reactor.js +30 -0
- package/dist/src/control-plane/application/trigger-recovery.d.ts +10 -0
- package/dist/src/control-plane/application/trigger-recovery.js +61 -0
- package/dist/src/control-plane/application/trigger-rules.d.ts +19 -0
- package/dist/src/control-plane/application/trigger-rules.js +38 -0
- package/dist/src/control-plane/application/trigger-service.d.ts +34 -20
- package/dist/src/control-plane/application/trigger-service.js +182 -102
- package/dist/src/control-plane/application/trigger-source-cursor.d.ts +16 -0
- package/dist/src/control-plane/application/trigger-source-cursor.js +35 -0
- package/dist/src/control-plane/application/trigger-state-reader.d.ts +13 -0
- package/dist/src/control-plane/application/trigger-state-reader.js +34 -0
- package/dist/src/control-plane/application/trigger-workflow.d.ts +17 -0
- package/dist/src/control-plane/application/trigger-workflow.js +18 -0
- package/dist/src/control-plane/contracts/event-factory.js +10 -0
- package/dist/src/control-plane/contracts/events.d.ts +50 -3
- package/dist/src/control-plane/contracts/events.js +100 -4
- package/dist/src/control-plane/contracts/streams.d.ts +9 -2
- package/dist/src/control-plane/contracts/streams.js +13 -1
- package/dist/src/control-plane/domain/cron-trigger-policy.d.ts +1 -1
- package/dist/src/control-plane/domain/cron-trigger-policy.js +3 -1
- package/dist/src/control-plane/index.d.ts +3 -0
- package/dist/src/control-plane/index.js +3 -0
- package/package.json +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { correlationId, EventActorKind } from '@atolis-hq/eventing';
|
|
2
|
+
import { workflowInstanceId } from '../../orchestration/index.js';
|
|
3
|
+
import { workItemId } from '../../work/index.js';
|
|
4
|
+
import { ControlEventType } from '../contracts/events.js';
|
|
5
|
+
import { exhaustedFailures, finishedStatus } from './trigger-rules.js';
|
|
6
|
+
import { ensureTriggerWorkflow } from './trigger-workflow.js';
|
|
7
|
+
/** Blocked invocations remain recoverable through operator retry or clarification. */
|
|
8
|
+
export async function recoverTriggerInvocation(dependencies, invocation, context) {
|
|
9
|
+
const claim = invocation.claim;
|
|
10
|
+
const workflow = workflowInstanceId(claim.workflowInstanceId);
|
|
11
|
+
const invocationContext = {
|
|
12
|
+
commandId: `${workflow}:create`,
|
|
13
|
+
correlationId: correlationId(workflow),
|
|
14
|
+
occurredAt: invocation.claimedAt,
|
|
15
|
+
actor: { kind: EventActorKind.System, id: claim.triggerId },
|
|
16
|
+
};
|
|
17
|
+
const instance = await ensureTriggerWorkflow(dependencies, claim, invocationContext);
|
|
18
|
+
if (invocation.startedAt === undefined) {
|
|
19
|
+
await dependencies.emit({
|
|
20
|
+
eventType: ControlEventType.TriggerInvocationStarted,
|
|
21
|
+
payload: {
|
|
22
|
+
triggerId: claim.triggerId,
|
|
23
|
+
coordinationKey: claim.coordinationKey,
|
|
24
|
+
source: claim.source,
|
|
25
|
+
workItemId: workItemId(claim.workItemId),
|
|
26
|
+
workflowInstanceId: workflow,
|
|
27
|
+
workflowName: claim.workflowName,
|
|
28
|
+
},
|
|
29
|
+
}, invocationContext, `${workflow}:${ControlEventType.TriggerInvocationStarted}`);
|
|
30
|
+
}
|
|
31
|
+
if (instance === null)
|
|
32
|
+
return;
|
|
33
|
+
if (exhaustedFailures(instance) &&
|
|
34
|
+
invocation.failureOutcomeCount !== instance.acceptedOutcomes.length) {
|
|
35
|
+
// Retrying remains an explicit workflow command; an operator must resume the trigger.
|
|
36
|
+
await dependencies.emit({
|
|
37
|
+
eventType: ControlEventType.TriggerPaused,
|
|
38
|
+
payload: {
|
|
39
|
+
triggerId: claim.triggerId,
|
|
40
|
+
coordinationKey: claim.coordinationKey,
|
|
41
|
+
workflowInstanceId: claim.workflowInstanceId,
|
|
42
|
+
outcomeCount: instance.acceptedOutcomes.length,
|
|
43
|
+
reason: instance.blockReason ?? 'Invocation exhausted automatic failure retries',
|
|
44
|
+
},
|
|
45
|
+
}, context, `${workflow}:pause:${instance.acceptedOutcomes.length}`);
|
|
46
|
+
}
|
|
47
|
+
const status = finishedStatus(instance);
|
|
48
|
+
if (status === undefined)
|
|
49
|
+
return;
|
|
50
|
+
await dependencies.emit({
|
|
51
|
+
eventType: ControlEventType.TriggerInvocationFinished,
|
|
52
|
+
payload: {
|
|
53
|
+
triggerId: claim.triggerId,
|
|
54
|
+
coordinationKey: claim.coordinationKey,
|
|
55
|
+
workItemId: claim.workItemId,
|
|
56
|
+
workflowInstanceId: workflow,
|
|
57
|
+
status,
|
|
58
|
+
...(instance.blockReason === undefined ? {} : { reason: instance.blockReason }),
|
|
59
|
+
},
|
|
60
|
+
}, context, `${workflow}:finished`);
|
|
61
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { EventEnvelope } from '@atolis-hq/eventing';
|
|
2
|
+
import { type WorkflowInstanceView } from '../../orchestration/index.js';
|
|
3
|
+
import { type ControlEventPayloads, type ControlEventType } from '../contracts/events.js';
|
|
4
|
+
import type { TriggerView } from './trigger-projection.js';
|
|
5
|
+
type FinishedStatus = ControlEventPayloads[typeof ControlEventType.TriggerInvocationFinished]['status'];
|
|
6
|
+
export declare function hasActiveInvocation(view: TriggerView, coordinationKey: string): boolean;
|
|
7
|
+
/** Events caused by this scope's own invocations must not re-trigger it. */
|
|
8
|
+
export declare function isOwnScopeSource(view: TriggerView, scope: {
|
|
9
|
+
readonly triggerId: string;
|
|
10
|
+
readonly coordinationKey: string;
|
|
11
|
+
}, event: EventEnvelope | undefined, resolve: {
|
|
12
|
+
readonly keyOf: (triggerId: string) => string | undefined;
|
|
13
|
+
readonly sourceWorkItem: (event: EventEnvelope) => Promise<string | null>;
|
|
14
|
+
readonly invocationScope: (workItemId: string) => Promise<string | null>;
|
|
15
|
+
}): Promise<boolean>;
|
|
16
|
+
/** Blocking or a failed outcome with no retry wait left ends automatic workflow progress. */
|
|
17
|
+
export declare function exhaustedFailures(instance: WorkflowInstanceView): boolean;
|
|
18
|
+
export declare function finishedStatus(instance: WorkflowInstanceView): FinishedStatus | undefined;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ActivityOutcomeKind } from '../../activities/index.js';
|
|
2
|
+
import { WorkflowStatus } from '../../orchestration/index.js';
|
|
3
|
+
import { selectControlEvent, } from '../contracts/events.js';
|
|
4
|
+
export function hasActiveInvocation(view, coordinationKey) {
|
|
5
|
+
return view.invocations.some((invocation) => invocation.claim.coordinationKey === coordinationKey && invocation.result === undefined);
|
|
6
|
+
}
|
|
7
|
+
/** Events caused by this scope's own invocations must not re-trigger it. */
|
|
8
|
+
export async function isOwnScopeSource(view, scope, event, resolve) {
|
|
9
|
+
if (event === undefined)
|
|
10
|
+
return false;
|
|
11
|
+
const control = selectControlEvent(event);
|
|
12
|
+
if (control !== null && 'triggerId' in control.event.payload) {
|
|
13
|
+
const id = control.event.payload.triggerId;
|
|
14
|
+
if (id === scope.triggerId ||
|
|
15
|
+
('coordinationKey' in control.event.payload &&
|
|
16
|
+
control.event.payload.coordinationKey === scope.coordinationKey) ||
|
|
17
|
+
resolve.keyOf(id) === scope.coordinationKey ||
|
|
18
|
+
view.invocations.some((invocation) => invocation.claim.triggerId === id &&
|
|
19
|
+
invocation.claim.coordinationKey === scope.coordinationKey))
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
const sourceItem = await resolve.sourceWorkItem(event);
|
|
23
|
+
return (sourceItem !== null && (await resolve.invocationScope(sourceItem)) === scope.coordinationKey);
|
|
24
|
+
}
|
|
25
|
+
/** Blocking or a failed outcome with no retry wait left ends automatic workflow progress. */
|
|
26
|
+
export function exhaustedFailures(instance) {
|
|
27
|
+
const failed = instance.lastOutcome?.kind === ActivityOutcomeKind.Failed;
|
|
28
|
+
return (instance.status === WorkflowStatus.Blocked ||
|
|
29
|
+
(failed && instance.status === WorkflowStatus.Completed) ||
|
|
30
|
+
(failed &&
|
|
31
|
+
instance.status === WorkflowStatus.Waiting &&
|
|
32
|
+
instance.waitingFor?.signalKind === ActivityOutcomeKind.Failed));
|
|
33
|
+
}
|
|
34
|
+
export function finishedStatus(instance) {
|
|
35
|
+
if (instance.status === WorkflowStatus.Completed || instance.status === WorkflowStatus.Superseded)
|
|
36
|
+
return instance.status;
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
@@ -1,35 +1,49 @@
|
|
|
1
|
-
import { type CommandContext, type EventEnvelope, type EventJournal } from '@atolis-hq/eventing';
|
|
1
|
+
import { type CommandContext, type EventEnvelope, type EventJournal, type ProcessorRunSerialiser, type ProcessorStateStore } from '@atolis-hq/eventing';
|
|
2
2
|
import type { IdGenerator } from '../../kernel/index.js';
|
|
3
|
-
import { workflowInstanceId, type StartWorkflowInstance } from '../../orchestration/index.js';
|
|
4
|
-
import { workItemId, type CreateWorkItem } from '../../work/index.js';
|
|
5
3
|
import type { TriggerConfig } from '../contracts/config.js';
|
|
6
4
|
import { type TriggerSource } from '../contracts/events.js';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
export
|
|
5
|
+
import { type TriggerInvocationView, type TriggerView } from './trigger-projection.js';
|
|
6
|
+
import { type TriggerCheckpointStore } from './trigger-source-cursor.js';
|
|
7
|
+
import type { TriggerWorkflowPorts } from './trigger-workflow.js';
|
|
8
|
+
export type { TriggerCheckpointStore } from './trigger-source-cursor.js';
|
|
9
|
+
export declare const triggerCriticalSectionConsumer = "control-plane:trigger-coordination";
|
|
10
|
+
export interface TriggerServiceDependencies extends TriggerWorkflowPorts {
|
|
12
11
|
readonly checkpoint: TriggerCheckpointStore;
|
|
13
12
|
readonly journal: EventJournal;
|
|
13
|
+
readonly state: ProcessorStateStore;
|
|
14
|
+
readonly history: () => Promise<readonly TriggerInvocationView[]>;
|
|
15
|
+
readonly invocationScope: (workItemId: string) => Promise<string | null>;
|
|
16
|
+
readonly serialise: ProcessorRunSerialiser;
|
|
14
17
|
readonly ids: IdGenerator;
|
|
15
|
-
readonly
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
readonly
|
|
19
|
-
start(command: StartWorkflowInstance, context: CommandContext): Promise<unknown>;
|
|
20
|
-
get(id: ReturnType<typeof workflowInstanceId>): Promise<{
|
|
21
|
-
readonly workItemId: ReturnType<typeof workItemId>;
|
|
22
|
-
} | null>;
|
|
23
|
-
};
|
|
18
|
+
readonly configurations: () => readonly TriggerConfig[];
|
|
19
|
+
readonly coordinationKey?: (config: TriggerConfig) => string;
|
|
20
|
+
readonly sourceWorkItem: (event: EventEnvelope) => Promise<string | null>;
|
|
21
|
+
readonly isDispatchPaused: () => Promise<boolean>;
|
|
24
22
|
readonly capacityAvailable: (config: TriggerConfig) => Promise<boolean>;
|
|
25
23
|
readonly now: () => string;
|
|
26
24
|
}
|
|
25
|
+
/** Requests and claims are journal facts; the cron cursor is processor recovery state. */
|
|
27
26
|
export declare class TriggerService {
|
|
28
27
|
private readonly dependencies;
|
|
29
28
|
private readonly policy;
|
|
29
|
+
private readonly reader;
|
|
30
|
+
private readonly sourceCursor;
|
|
30
31
|
constructor(dependencies: TriggerServiceDependencies);
|
|
32
|
+
get(): Promise<TriggerView>;
|
|
33
|
+
reconcile(context: CommandContext): Promise<void>;
|
|
34
|
+
pause(triggerId: string, reason: string, context: CommandContext): Promise<void>;
|
|
35
|
+
resume(triggerId: string, context: CommandContext): Promise<void>;
|
|
31
36
|
run(config: TriggerConfig, context: CommandContext): Promise<readonly string[]>;
|
|
32
|
-
start(config: TriggerConfig, source: TriggerSource, context: CommandContext): Promise<boolean>;
|
|
33
|
-
private
|
|
37
|
+
start(config: TriggerConfig, source: TriggerSource, context: CommandContext, event?: EventEnvelope): Promise<boolean>;
|
|
38
|
+
private serialise;
|
|
39
|
+
private key;
|
|
40
|
+
private commandKey;
|
|
41
|
+
private view;
|
|
42
|
+
private configured;
|
|
43
|
+
private emit;
|
|
44
|
+
private request;
|
|
45
|
+
private suppressed;
|
|
46
|
+
private eligible;
|
|
47
|
+
private advanceCoalesced;
|
|
48
|
+
private recover;
|
|
34
49
|
}
|
|
35
|
-
export declare function createTriggerReactor(service: TriggerService, config: TriggerConfig, decode: (event: EventEnvelope) => void): import("@atolis-hq/eventing").EventProcessor;
|
|
@@ -1,118 +1,198 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { orchestrationGroupId, workflowInstanceId, workflowName, } from '../../orchestration/index.js';
|
|
3
|
-
import { workItemId } from '../../work/index.js';
|
|
4
|
-
import { createControlPlaneEventData } from '../contracts/event-factory.js';
|
|
5
|
-
import { ControlEventType, selectControlEvent, TriggerSourceKind, } from '../contracts/events.js';
|
|
6
|
-
import { controlPlaneStream, ControlStreamKind } from '../contracts/streams.js';
|
|
1
|
+
import { ControlEventType, TriggerRequestDisposition, TriggerSourceKind, } from '../contracts/events.js';
|
|
7
2
|
import { CronTriggerPolicy } from '../domain/cron-trigger-policy.js';
|
|
3
|
+
import { appendTriggerFact, sourceIdentity } from './trigger-facts.js';
|
|
4
|
+
import { maximumTriggerSources, } from './trigger-projection.js';
|
|
5
|
+
import { recoverTriggerInvocation } from './trigger-recovery.js';
|
|
6
|
+
import { hasActiveInvocation, isOwnScopeSource } from './trigger-rules.js';
|
|
7
|
+
import { TriggerSourceCursor } from './trigger-source-cursor.js';
|
|
8
|
+
import { TriggerStateReader } from './trigger-state-reader.js';
|
|
9
|
+
export const triggerCriticalSectionConsumer = 'control-plane:trigger-coordination';
|
|
10
|
+
/** Requests and claims are journal facts; the cron cursor is processor recovery state. */
|
|
8
11
|
export class TriggerService {
|
|
9
12
|
dependencies;
|
|
10
13
|
policy = new CronTriggerPolicy();
|
|
14
|
+
reader;
|
|
15
|
+
sourceCursor;
|
|
11
16
|
constructor(dependencies) {
|
|
12
17
|
this.dependencies = dependencies;
|
|
18
|
+
this.reader = new TriggerStateReader(dependencies.journal);
|
|
19
|
+
this.sourceCursor = new TriggerSourceCursor(dependencies);
|
|
20
|
+
}
|
|
21
|
+
async get() {
|
|
22
|
+
return this.serialise(async () => ({
|
|
23
|
+
...(await this.view()),
|
|
24
|
+
invocations: await this.dependencies.history(),
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
async reconcile(context) {
|
|
28
|
+
await this.serialise(async () => {
|
|
29
|
+
await this.recover(context);
|
|
30
|
+
await this.advanceCoalesced(context);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async pause(triggerId, reason, context) {
|
|
34
|
+
await this.serialise(async () => {
|
|
35
|
+
await this.view();
|
|
36
|
+
await this.emit({
|
|
37
|
+
eventType: ControlEventType.TriggerPaused,
|
|
38
|
+
payload: { triggerId, reason, coordinationKey: this.commandKey(triggerId) },
|
|
39
|
+
}, context, `trigger:pause:${triggerId}:${context.commandId}`);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async resume(triggerId, context) {
|
|
43
|
+
await this.serialise(async () => {
|
|
44
|
+
await this.view();
|
|
45
|
+
await this.emit({
|
|
46
|
+
eventType: ControlEventType.TriggerResumed,
|
|
47
|
+
payload: { triggerId, coordinationKey: this.commandKey(triggerId) },
|
|
48
|
+
}, context, `trigger:resume:${triggerId}:${context.commandId}`);
|
|
49
|
+
});
|
|
13
50
|
}
|
|
14
51
|
async run(config, context) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
52
|
+
return this.serialise(async () => {
|
|
53
|
+
const accepted = [];
|
|
54
|
+
// Recovery finishes committed claims, even when their definition was removed or paused.
|
|
55
|
+
await this.recover(context);
|
|
56
|
+
if (config.on.cron !== undefined) {
|
|
57
|
+
const checkpoint = await this.dependencies.checkpoint.load(config.id);
|
|
58
|
+
for (const slot of this.policy.elapsedSlots({ id: config.id, cron: config.on.cron }, this.dependencies.now(), checkpoint, 64)) {
|
|
59
|
+
if (await this.request(config, { kind: TriggerSourceKind.Cron, slot: slot.at }, context))
|
|
60
|
+
accepted.push(slot.identity);
|
|
61
|
+
await this.dependencies.checkpoint.save(config.id, slot.at);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
await this.advanceCoalesced(context);
|
|
65
|
+
return accepted;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async start(config, source, context, event) {
|
|
69
|
+
return this.serialise(async () => {
|
|
70
|
+
await this.recover(context);
|
|
71
|
+
const accepted = await this.request(config, source, context, event);
|
|
72
|
+
if (source.kind === TriggerSourceKind.Cron) {
|
|
73
|
+
const previous = await this.dependencies.checkpoint.load(config.id);
|
|
74
|
+
if (previous === null || Date.parse(source.slot) > Date.parse(previous))
|
|
75
|
+
await this.dependencies.checkpoint.save(config.id, source.slot);
|
|
76
|
+
}
|
|
77
|
+
await this.advanceCoalesced(context);
|
|
78
|
+
return accepted;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
serialise(operation) {
|
|
82
|
+
return this.dependencies.serialise(triggerCriticalSectionConsumer, new AbortController().signal, operation);
|
|
83
|
+
}
|
|
84
|
+
key(config) {
|
|
85
|
+
return this.dependencies.coordinationKey?.(config) ?? config.id;
|
|
86
|
+
}
|
|
87
|
+
commandKey(triggerId) {
|
|
88
|
+
const config = this.configured(triggerId);
|
|
89
|
+
return config === undefined
|
|
90
|
+
? (this.reader.current.requests.find((request) => request.triggerId === triggerId)
|
|
91
|
+
?.coordinationKey ?? triggerId)
|
|
92
|
+
: this.key(config);
|
|
93
|
+
}
|
|
94
|
+
view() {
|
|
95
|
+
return this.reader.get();
|
|
96
|
+
}
|
|
97
|
+
configured(id) {
|
|
98
|
+
return this.dependencies.configurations().find((config) => config.id === id);
|
|
99
|
+
}
|
|
100
|
+
async emit(input, context, eventId) {
|
|
101
|
+
await this.view();
|
|
102
|
+
await appendTriggerFact(this.dependencies.journal, this.dependencies.now(), input, context, eventId, input.eventType === ControlEventType.TriggerRequestRecorded
|
|
103
|
+
? this.reader.sequence(input.payload.coordinationKey)
|
|
104
|
+
: undefined);
|
|
105
|
+
}
|
|
106
|
+
async request(config, source, context, event) {
|
|
107
|
+
const view = await this.view();
|
|
108
|
+
if (await this.sourceCursor.isConsumed(config.id, source, event))
|
|
109
|
+
return false;
|
|
110
|
+
const duplicate = view.requests.some((request) => request.triggerId === config.id &&
|
|
111
|
+
sourceIdentity(request.source) === sourceIdentity(source));
|
|
112
|
+
const coordinationKey = this.key(config);
|
|
113
|
+
// The reactor checkpoint consumes suppressed sources. Emitting another trigger request
|
|
114
|
+
// here could itself become a source when subscribed to Control Plane events.
|
|
115
|
+
if (duplicate || (await this.suppressed(view, config, coordinationKey, event))) {
|
|
116
|
+
await this.sourceCursor.consume(config.id, source, event);
|
|
40
117
|
return false;
|
|
41
|
-
const item = existing?.workItemId ?? workItemId(this.dependencies.ids.next('work'));
|
|
42
|
-
if (existing === null) {
|
|
43
|
-
await this.dependencies.work.create({ workItemId: item, objective: `Trigger ${config.id}: workflow ${config.workflow}` }, invocationContext);
|
|
44
|
-
await this.dependencies.orchestration.start({
|
|
45
|
-
workflowInstanceId: workflow,
|
|
46
|
-
workItemId: item,
|
|
47
|
-
workflowName: workflowName(config.workflow),
|
|
48
|
-
orchestrationGroupId: orchestrationGroupId(`primary:${item}`),
|
|
49
|
-
}, invocationContext);
|
|
50
118
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
119
|
+
const eligible = view.pauses[config.id] === undefined &&
|
|
120
|
+
this.configured(config.id) !== undefined &&
|
|
121
|
+
!(await this.dependencies.isDispatchPaused()) &&
|
|
122
|
+
(hasActiveInvocation(view, coordinationKey) || (await this.eligible(config)));
|
|
123
|
+
if (eligible && (view.coalesced[coordinationKey]?.sources.length ?? 0) < maximumTriggerSources)
|
|
124
|
+
await this.emit({
|
|
125
|
+
eventType: ControlEventType.TriggerRequestRecorded,
|
|
126
|
+
payload: {
|
|
127
|
+
triggerId: config.id,
|
|
128
|
+
coordinationKey,
|
|
129
|
+
source,
|
|
130
|
+
disposition: TriggerRequestDisposition.Coalesced,
|
|
131
|
+
},
|
|
132
|
+
}, context, `trigger:request:${config.id}:${sourceIdentity(source)}`);
|
|
133
|
+
await this.sourceCursor.consume(config.id, source, event);
|
|
134
|
+
return eligible;
|
|
135
|
+
}
|
|
136
|
+
suppressed(view, config, coordinationKey, event) {
|
|
137
|
+
return isOwnScopeSource(view, { triggerId: config.id, coordinationKey }, event, {
|
|
138
|
+
keyOf: (id) => {
|
|
139
|
+
const other = this.configured(id);
|
|
140
|
+
return other === undefined ? undefined : this.key(other);
|
|
70
141
|
},
|
|
142
|
+
sourceWorkItem: this.dependencies.sourceWorkItem,
|
|
143
|
+
invocationScope: this.dependencies.invocationScope,
|
|
71
144
|
});
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
145
|
+
}
|
|
146
|
+
async eligible(config) {
|
|
147
|
+
return (!(await this.dependencies.isDispatchPaused()) &&
|
|
148
|
+
(config.when?.capacityAvailable !== true ||
|
|
149
|
+
(await this.dependencies.capacityAvailable(config))));
|
|
150
|
+
}
|
|
151
|
+
async advanceCoalesced(context) {
|
|
152
|
+
const view = await this.view();
|
|
153
|
+
for (const request of Object.values(view.coalesced)) {
|
|
154
|
+
if (hasActiveInvocation(view, request.coordinationKey))
|
|
155
|
+
continue;
|
|
156
|
+
const config = this.configured(request.triggerId);
|
|
157
|
+
const identity = `trigger:${request.triggerId}:${sourceIdentity(request.source)}`;
|
|
158
|
+
if (config === undefined ||
|
|
159
|
+
view.pauses[request.triggerId] !== undefined ||
|
|
160
|
+
!(await this.eligible(config))) {
|
|
161
|
+
// This recorded request was reconsidered and consumed; later eligibility is not a source.
|
|
162
|
+
await this.emit({
|
|
163
|
+
eventType: ControlEventType.TriggerRequestRecorded,
|
|
164
|
+
payload: {
|
|
165
|
+
triggerId: request.triggerId,
|
|
166
|
+
coordinationKey: request.coordinationKey,
|
|
167
|
+
source: request.source,
|
|
168
|
+
disposition: TriggerRequestDisposition.Skipped,
|
|
169
|
+
},
|
|
170
|
+
}, context, `trigger:consume:${request.triggerId}:${sourceIdentity(request.source)}`);
|
|
171
|
+
continue;
|
|
88
172
|
}
|
|
173
|
+
await this.emit({
|
|
174
|
+
eventType: ControlEventType.TriggerInvocationClaimed,
|
|
175
|
+
payload: {
|
|
176
|
+
triggerId: config.id,
|
|
177
|
+
coordinationKey: request.coordinationKey,
|
|
178
|
+
source: request.source,
|
|
179
|
+
sources: request.sources,
|
|
180
|
+
workItemId: this.dependencies.ids.next('work'),
|
|
181
|
+
workflowInstanceId: `workflow-${encodeURIComponent(identity)}`,
|
|
182
|
+
workflowName: config.workflow,
|
|
183
|
+
},
|
|
184
|
+
}, context, `${identity}:claim`);
|
|
185
|
+
await this.recover(context);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async recover(context) {
|
|
189
|
+
for (const invocation of (await this.view()).invocations) {
|
|
190
|
+
if (invocation.result === undefined)
|
|
191
|
+
await recoverTriggerInvocation({
|
|
192
|
+
work: this.dependencies.work,
|
|
193
|
+
orchestration: this.dependencies.orchestration,
|
|
194
|
+
emit: (input, command, id) => this.emit(input, command, id),
|
|
195
|
+
}, invocation, context);
|
|
89
196
|
}
|
|
90
197
|
}
|
|
91
|
-
}
|
|
92
|
-
export function createTriggerReactor(service, config, decode) {
|
|
93
|
-
return defineEventProcessor({
|
|
94
|
-
consumer: `reactor:control-plane.trigger:${config.id}`,
|
|
95
|
-
name: 'workflow-triggers',
|
|
96
|
-
owner: ControlStreamKind.Global,
|
|
97
|
-
category: EventProcessorCategory.Reactor,
|
|
98
|
-
replayPolicy: EventProcessorReplayPolicy.Idempotent,
|
|
99
|
-
select(event) {
|
|
100
|
-
if (!config.on.events?.includes(event.event.eventType))
|
|
101
|
-
return null;
|
|
102
|
-
decode(event);
|
|
103
|
-
return event;
|
|
104
|
-
},
|
|
105
|
-
async handle(event) {
|
|
106
|
-
await service.start(config, {
|
|
107
|
-
kind: TriggerSourceKind.Event,
|
|
108
|
-
eventId: event.event.eventId,
|
|
109
|
-
eventType: event.event.eventType,
|
|
110
|
-
}, {
|
|
111
|
-
commandId: `trigger:${config.id}:${event.event.eventId}`,
|
|
112
|
-
correlationId: correlationId(`trigger:${config.id}:${event.event.eventId}`),
|
|
113
|
-
occurredAt: event.event.occurredAt,
|
|
114
|
-
actor: { kind: EventActorKind.System, id: config.id },
|
|
115
|
-
});
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
198
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EventEnvelope, ProcessorStateStore } from '@atolis-hq/eventing';
|
|
2
|
+
import { type TriggerSource } from '../contracts/events.js';
|
|
3
|
+
export interface TriggerCheckpointStore {
|
|
4
|
+
load(triggerId: string): Promise<string | null>;
|
|
5
|
+
save(triggerId: string, slot: string): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
/** Consumed occurrences are processor recovery cursors, not an ever-growing request history. */
|
|
8
|
+
export declare class TriggerSourceCursor {
|
|
9
|
+
private readonly dependencies;
|
|
10
|
+
constructor(dependencies: {
|
|
11
|
+
readonly checkpoint: TriggerCheckpointStore;
|
|
12
|
+
readonly state: ProcessorStateStore;
|
|
13
|
+
});
|
|
14
|
+
isConsumed(triggerId: string, source: TriggerSource, event?: EventEnvelope): Promise<boolean>;
|
|
15
|
+
consume(triggerId: string, source: TriggerSource, event?: EventEnvelope): Promise<void>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { TriggerSourceKind } from '../contracts/events.js';
|
|
3
|
+
const sourceCursorSchema = z.object({ position: z.number().int().nonnegative() }).strict();
|
|
4
|
+
/** Consumed occurrences are processor recovery cursors, not an ever-growing request history. */
|
|
5
|
+
export class TriggerSourceCursor {
|
|
6
|
+
dependencies;
|
|
7
|
+
constructor(dependencies) {
|
|
8
|
+
this.dependencies = dependencies;
|
|
9
|
+
}
|
|
10
|
+
async isConsumed(triggerId, source, event) {
|
|
11
|
+
if (source.kind === TriggerSourceKind.Cron) {
|
|
12
|
+
const previous = await this.dependencies.checkpoint.load(triggerId);
|
|
13
|
+
if (previous !== null && Date.parse(source.slot) <= Date.parse(previous))
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
if (source.kind === TriggerSourceKind.Event) {
|
|
17
|
+
if (event === undefined || event.event.eventId !== source.eventId)
|
|
18
|
+
throw new Error('Event trigger requests require their canonical source envelope');
|
|
19
|
+
const stored = await this.dependencies.state.read(`reactor:control-plane.trigger:${triggerId}`, 'source');
|
|
20
|
+
if (stored !== null &&
|
|
21
|
+
event.globalPosition <= sourceCursorSchema.parse(stored.value).position)
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
async consume(triggerId, source, event) {
|
|
27
|
+
if (source.kind !== TriggerSourceKind.Event || event === undefined)
|
|
28
|
+
return;
|
|
29
|
+
await this.dependencies.state.write({
|
|
30
|
+
consumer: `reactor:control-plane.trigger:${triggerId}`,
|
|
31
|
+
key: 'source',
|
|
32
|
+
value: { position: event.globalPosition },
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EventJournal } from '@atolis-hq/eventing';
|
|
2
|
+
import { type TriggerView } from './trigger-projection.js';
|
|
3
|
+
/** A rebuildable operational cache; every critical section catches up to the journal. */
|
|
4
|
+
export declare class TriggerStateReader {
|
|
5
|
+
private readonly journal;
|
|
6
|
+
private position;
|
|
7
|
+
private value;
|
|
8
|
+
private readonly sequences;
|
|
9
|
+
constructor(journal: EventJournal);
|
|
10
|
+
get current(): TriggerView;
|
|
11
|
+
sequence(coordinationKey: string): number;
|
|
12
|
+
get(): Promise<TriggerView>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ControlStreamKind } from '../contracts/streams.js';
|
|
2
|
+
import { triggerProjection } from './trigger-projection.js';
|
|
3
|
+
/** A rebuildable operational cache; every critical section catches up to the journal. */
|
|
4
|
+
export class TriggerStateReader {
|
|
5
|
+
journal;
|
|
6
|
+
position = 0;
|
|
7
|
+
value = triggerProjection.initial('global');
|
|
8
|
+
sequences = new Map();
|
|
9
|
+
constructor(journal) {
|
|
10
|
+
this.journal = journal;
|
|
11
|
+
}
|
|
12
|
+
get current() {
|
|
13
|
+
return this.value;
|
|
14
|
+
}
|
|
15
|
+
sequence(coordinationKey) {
|
|
16
|
+
return this.sequences.get(coordinationKey) ?? 0;
|
|
17
|
+
}
|
|
18
|
+
async get() {
|
|
19
|
+
const latest = await this.journal.latestGlobalPosition();
|
|
20
|
+
while (this.position < latest) {
|
|
21
|
+
const batch = await this.journal.readAll(this.position, 256);
|
|
22
|
+
if (batch.length === 0)
|
|
23
|
+
throw new Error('Trigger journal cursor did not advance');
|
|
24
|
+
for (const envelope of batch) {
|
|
25
|
+
if (triggerProjection.select(envelope) !== null)
|
|
26
|
+
this.value = triggerProjection.project(this.value, envelope);
|
|
27
|
+
if (envelope.stream.kind === ControlStreamKind.Trigger)
|
|
28
|
+
this.sequences.set(envelope.stream.id, envelope.sequence);
|
|
29
|
+
this.position = envelope.globalPosition;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return this.value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CommandContext } from '@atolis-hq/eventing';
|
|
2
|
+
import { workflowInstanceId, type StartWorkflowInstance, type WorkflowInstanceView } from '../../orchestration/index.js';
|
|
3
|
+
import { type CreateWorkItem } from '../../work/index.js';
|
|
4
|
+
import type { ControlEventPayloads, ControlEventType } from '../contracts/events.js';
|
|
5
|
+
type Claim = ControlEventPayloads[typeof ControlEventType.TriggerInvocationClaimed];
|
|
6
|
+
export interface TriggerWorkflowPorts {
|
|
7
|
+
readonly work: {
|
|
8
|
+
create(command: CreateWorkItem, context: CommandContext): Promise<unknown>;
|
|
9
|
+
};
|
|
10
|
+
readonly orchestration: {
|
|
11
|
+
start(command: StartWorkflowInstance, context: CommandContext): Promise<unknown>;
|
|
12
|
+
get(id: ReturnType<typeof workflowInstanceId>): Promise<WorkflowInstanceView | null>;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/** Creates the top-level invocation WorkItem and workflow once; replays only read. */
|
|
16
|
+
export declare function ensureTriggerWorkflow(ports: TriggerWorkflowPorts, claim: Claim, context: CommandContext): Promise<WorkflowInstanceView | null>;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { orchestrationGroupId, workflowInstanceId, workflowName, } from '../../orchestration/index.js';
|
|
2
|
+
import { workItemId } from '../../work/index.js';
|
|
3
|
+
/** Creates the top-level invocation WorkItem and workflow once; replays only read. */
|
|
4
|
+
export async function ensureTriggerWorkflow(ports, claim, context) {
|
|
5
|
+
const workflow = workflowInstanceId(claim.workflowInstanceId);
|
|
6
|
+
const existing = await ports.orchestration.get(workflow);
|
|
7
|
+
if (existing !== null)
|
|
8
|
+
return existing;
|
|
9
|
+
const item = workItemId(claim.workItemId);
|
|
10
|
+
await ports.work.create({ workItemId: item, objective: `Trigger ${claim.triggerId}: workflow ${claim.workflowName}` }, context);
|
|
11
|
+
await ports.orchestration.start({
|
|
12
|
+
workflowInstanceId: workflow,
|
|
13
|
+
workItemId: item,
|
|
14
|
+
workflowName: workflowName(claim.workflowName),
|
|
15
|
+
orchestrationGroupId: orchestrationGroupId(`primary:${item}`),
|
|
16
|
+
}, context);
|
|
17
|
+
return ports.orchestration.get(workflow);
|
|
18
|
+
}
|