@atolis-hq/wake 0.4.61 → 0.4.63
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/config/root-schema.d.ts +2 -2
- package/dist/src/bootstrap/integration-runtime.d.ts +4 -2
- package/dist/src/bootstrap/integration-runtime.js +74 -13
- package/dist/src/bootstrap/surface-api-applications.js +2 -0
- package/dist/src/bootstrap/surface-api-trigger-applications.d.ts +3 -0
- package/dist/src/bootstrap/surface-api-trigger-applications.js +79 -0
- package/dist/src/bootstrap/surface-api-work-applications.js +8 -0
- 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-diagnostics.d.ts +33 -0
- package/dist/src/control-plane/application/trigger-diagnostics.js +38 -0
- 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-ports.d.ts +21 -0
- package/dist/src/control-plane/application/trigger-service-ports.js +1 -0
- package/dist/src/control-plane/application/trigger-service.d.ts +29 -27
- package/dist/src/control-plane/application/trigger-service.js +188 -101
- 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/contracts/trigger-diagnostics.d.ts +53 -0
- package/dist/src/control-plane/contracts/trigger-diagnostics.js +23 -0
- package/dist/src/control-plane/domain/cron-trigger-policy.d.ts +2 -1
- package/dist/src/control-plane/domain/cron-trigger-policy.js +14 -1
- package/dist/src/control-plane/index.d.ts +4 -0
- package/dist/src/control-plane/index.js +4 -0
- package/dist/src/orchestration/contracts/config.d.ts +2 -2
- package/dist/src/surfaces/api/contracts/index.d.ts +1 -0
- package/dist/src/surfaces/api/contracts/index.js +1 -0
- package/dist/src/surfaces/api/contracts/transport-values.d.ts +8 -0
- package/dist/src/surfaces/api/contracts/transport-values.js +23 -0
- package/dist/src/surfaces/api/contracts/triggers.d.ts +22 -0
- package/dist/src/surfaces/api/contracts/triggers.js +8 -0
- package/dist/src/surfaces/api/contracts/work.d.ts +1 -0
- package/dist/src/surfaces/api/contracts/workflow-diagram.d.ts +4 -4
- package/dist/src/surfaces/api/routes/applications.d.ts +7 -1
- package/dist/src/surfaces/api/routes/commands.js +10 -0
- package/dist/src/surfaces/api/routes/read.js +16 -0
- package/dist/src/surfaces/web-assets/assets/index-9IfrHvBo.js +1192 -0
- package/dist/src/surfaces/web-assets/index.html +1 -1
- package/package.json +1 -1
- package/dist/src/surfaces/web-assets/assets/index-CCvwzTeC.js +0 -1131
|
@@ -1,118 +1,205 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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';
|
|
2
|
+
import { TriggerSkipReason } from '../contracts/trigger-diagnostics.js';
|
|
7
3
|
import { CronTriggerPolicy } from '../domain/cron-trigger-policy.js';
|
|
4
|
+
import { TriggerDiagnostics } from './trigger-diagnostics.js';
|
|
5
|
+
import { appendTriggerFact, sourceIdentity } from './trigger-facts.js';
|
|
6
|
+
import { maximumTriggerSources } from './trigger-projection.js';
|
|
7
|
+
import { recoverTriggerInvocation } from './trigger-recovery.js';
|
|
8
|
+
import { hasActiveInvocation, isOwnScopeSource } from './trigger-rules.js';
|
|
9
|
+
import { TriggerSourceCursor } from './trigger-source-cursor.js';
|
|
10
|
+
import { TriggerStateReader } from './trigger-state-reader.js';
|
|
11
|
+
export const triggerCriticalSectionConsumer = 'control-plane:trigger-coordination';
|
|
12
|
+
/** Requests and claims are journal facts; the cron cursor is processor recovery state. */
|
|
8
13
|
export class TriggerService {
|
|
9
14
|
dependencies;
|
|
10
15
|
policy = new CronTriggerPolicy();
|
|
16
|
+
reader;
|
|
17
|
+
sourceCursor;
|
|
18
|
+
diagnostics;
|
|
11
19
|
constructor(dependencies) {
|
|
12
20
|
this.dependencies = dependencies;
|
|
21
|
+
this.diagnostics = new TriggerDiagnostics(dependencies, triggerCriticalSectionConsumer);
|
|
22
|
+
this.reader = new TriggerStateReader(dependencies.journal);
|
|
23
|
+
this.sourceCursor = new TriggerSourceCursor(dependencies);
|
|
24
|
+
}
|
|
25
|
+
async get() {
|
|
26
|
+
return this.serialise(async () => ({
|
|
27
|
+
...(await this.view()),
|
|
28
|
+
invocations: await this.dependencies.history(),
|
|
29
|
+
diagnostics: await this.diagnostics.get(),
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
async reconcile(context) {
|
|
33
|
+
await this.serialise(async () => {
|
|
34
|
+
await this.recover(context);
|
|
35
|
+
await this.advanceCoalesced(context);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async pause(triggerId, reason, context) {
|
|
39
|
+
await this.serialise(async () => {
|
|
40
|
+
await this.view();
|
|
41
|
+
await this.emit({
|
|
42
|
+
eventType: ControlEventType.TriggerPaused,
|
|
43
|
+
payload: { triggerId, reason, coordinationKey: this.commandKey(triggerId) },
|
|
44
|
+
}, context, `trigger:pause:${triggerId}:${context.commandId}`);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
async resume(triggerId, context) {
|
|
48
|
+
await this.serialise(async () => {
|
|
49
|
+
await this.view();
|
|
50
|
+
await this.emit({
|
|
51
|
+
eventType: ControlEventType.TriggerResumed,
|
|
52
|
+
payload: { triggerId, coordinationKey: this.commandKey(triggerId) },
|
|
53
|
+
}, context, `trigger:resume:${triggerId}:${context.commandId}`);
|
|
54
|
+
});
|
|
13
55
|
}
|
|
14
56
|
async run(config, context) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
57
|
+
return this.serialise(async () => {
|
|
58
|
+
const accepted = [];
|
|
59
|
+
// Recovery finishes committed claims, even when their definition was removed or paused.
|
|
60
|
+
await this.recover(context);
|
|
61
|
+
if (config.on.cron !== undefined) {
|
|
62
|
+
const checkpoint = await this.dependencies.checkpoint.load(config.id);
|
|
63
|
+
for (const slot of this.policy.elapsedSlots({ id: config.id, cron: config.on.cron }, this.dependencies.now(), checkpoint, 64)) {
|
|
64
|
+
if (await this.request(config, { kind: TriggerSourceKind.Cron, slot: slot.at }, context))
|
|
65
|
+
accepted.push(slot.identity);
|
|
66
|
+
await this.dependencies.checkpoint.save(config.id, slot.at);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
await this.advanceCoalesced(context);
|
|
70
|
+
return accepted;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
async start(config, source, context, event) {
|
|
74
|
+
return this.serialise(async () => {
|
|
75
|
+
await this.recover(context);
|
|
76
|
+
const accepted = await this.request(config, source, context, event);
|
|
77
|
+
if (source.kind === TriggerSourceKind.Cron) {
|
|
78
|
+
const previous = await this.dependencies.checkpoint.load(config.id);
|
|
79
|
+
if (previous === null || Date.parse(source.slot) > Date.parse(previous))
|
|
80
|
+
await this.dependencies.checkpoint.save(config.id, source.slot);
|
|
81
|
+
}
|
|
82
|
+
await this.advanceCoalesced(context);
|
|
83
|
+
return accepted;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
serialise(operation) {
|
|
87
|
+
return this.dependencies.serialise(triggerCriticalSectionConsumer, new AbortController().signal, operation);
|
|
88
|
+
}
|
|
89
|
+
key(config) {
|
|
90
|
+
return this.dependencies.coordinationKey?.(config) ?? config.id;
|
|
91
|
+
}
|
|
92
|
+
commandKey(triggerId) {
|
|
93
|
+
const config = this.configured(triggerId);
|
|
94
|
+
return config === undefined
|
|
95
|
+
? (this.reader.current.requests.find((request) => request.triggerId === triggerId)
|
|
96
|
+
?.coordinationKey ?? triggerId)
|
|
97
|
+
: this.key(config);
|
|
98
|
+
}
|
|
99
|
+
view() {
|
|
100
|
+
return this.reader.get();
|
|
101
|
+
}
|
|
102
|
+
configured(id) {
|
|
103
|
+
return this.dependencies.configurations().find((config) => config.id === id);
|
|
104
|
+
}
|
|
105
|
+
async emit(input, context, eventId) {
|
|
106
|
+
await this.view();
|
|
107
|
+
await appendTriggerFact(this.dependencies.journal, this.dependencies.now(), input, context, eventId, input.eventType === ControlEventType.TriggerRequestRecorded
|
|
108
|
+
? this.reader.sequence(input.payload.coordinationKey)
|
|
109
|
+
: undefined);
|
|
110
|
+
}
|
|
111
|
+
async request(config, source, context, event) {
|
|
112
|
+
const view = await this.view();
|
|
113
|
+
if (await this.sourceCursor.isConsumed(config.id, source, event))
|
|
114
|
+
return false;
|
|
115
|
+
const duplicate = view.requests.some((request) => request.triggerId === config.id &&
|
|
116
|
+
sourceIdentity(request.source) === sourceIdentity(source));
|
|
117
|
+
const coordinationKey = this.key(config);
|
|
118
|
+
// The reactor checkpoint consumes suppressed sources. Emitting another trigger request
|
|
119
|
+
// here could itself become a source when subscribed to Control Plane events.
|
|
120
|
+
if (duplicate) {
|
|
121
|
+
await this.sourceCursor.consume(config.id, source, event);
|
|
122
|
+
return false;
|
|
25
123
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const identity = `trigger:${config.id}:${source.kind}:${source.kind === TriggerSourceKind.Cron ? source.slot : source.eventId}`;
|
|
30
|
-
const workflow = workflowInstanceId(`workflow-${encodeURIComponent(identity)}`);
|
|
31
|
-
const invocationContext = {
|
|
32
|
-
...context,
|
|
33
|
-
commandId: `${context.commandId}:${identity}`,
|
|
34
|
-
correlationId: correlationId(identity),
|
|
35
|
-
};
|
|
36
|
-
const existing = await this.dependencies.orchestration.get(workflow);
|
|
37
|
-
if (existing === null &&
|
|
38
|
-
config.when?.capacityAvailable === true &&
|
|
39
|
-
!(await this.dependencies.capacityAvailable(config)))
|
|
124
|
+
if (await this.suppressed(view, config, coordinationKey, event)) {
|
|
125
|
+
await this.diagnostics.record(config.id, source, TriggerSkipReason.OwnScope);
|
|
126
|
+
await this.sourceCursor.consume(config.id, source, event);
|
|
40
127
|
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
128
|
}
|
|
51
|
-
await this.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
129
|
+
const reason = await this.diagnostics.ineligibleReason(view, config.id, config, this.configured(config.id) !== undefined, hasActiveInvocation(view, coordinationKey));
|
|
130
|
+
const eligible = reason === undefined;
|
|
131
|
+
if (reason !== undefined)
|
|
132
|
+
await this.diagnostics.record(config.id, source, reason);
|
|
133
|
+
else if ((view.coalesced[coordinationKey]?.sources.length ?? 0) >= maximumTriggerSources)
|
|
134
|
+
await this.diagnostics.record(config.id, source, TriggerSkipReason.PendingLimit);
|
|
135
|
+
if (eligible && (view.coalesced[coordinationKey]?.sources.length ?? 0) < maximumTriggerSources)
|
|
136
|
+
await this.emit({
|
|
137
|
+
eventType: ControlEventType.TriggerRequestRecorded,
|
|
138
|
+
payload: {
|
|
139
|
+
triggerId: config.id,
|
|
140
|
+
coordinationKey,
|
|
141
|
+
source,
|
|
142
|
+
disposition: TriggerRequestDisposition.Coalesced,
|
|
143
|
+
},
|
|
144
|
+
}, context, `trigger:request:${config.id}:${sourceIdentity(source)}`);
|
|
145
|
+
await this.sourceCursor.consume(config.id, source, event);
|
|
146
|
+
return eligible;
|
|
147
|
+
}
|
|
148
|
+
suppressed(view, config, coordinationKey, event) {
|
|
149
|
+
return isOwnScopeSource(view, { triggerId: config.id, coordinationKey }, event, {
|
|
150
|
+
keyOf: (id) => {
|
|
151
|
+
const other = this.configured(id);
|
|
152
|
+
return other === undefined ? undefined : this.key(other);
|
|
70
153
|
},
|
|
154
|
+
sourceWorkItem: this.dependencies.sourceWorkItem,
|
|
155
|
+
invocationScope: this.dependencies.invocationScope,
|
|
71
156
|
});
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
157
|
+
}
|
|
158
|
+
async advanceCoalesced(context) {
|
|
159
|
+
const view = await this.view();
|
|
160
|
+
for (const request of Object.values(view.coalesced)) {
|
|
161
|
+
if (hasActiveInvocation(view, request.coordinationKey))
|
|
162
|
+
continue;
|
|
163
|
+
const config = this.configured(request.triggerId);
|
|
164
|
+
const identity = `trigger:${request.triggerId}:${sourceIdentity(request.source)}`;
|
|
165
|
+
const reason = await this.diagnostics.ineligibleReason(view, request.triggerId, config, config !== undefined);
|
|
166
|
+
if (reason !== undefined || config === undefined) {
|
|
167
|
+
await this.diagnostics.record(request.triggerId, request.source, reason ?? TriggerSkipReason.Removed);
|
|
168
|
+
// This recorded request was reconsidered and consumed; later eligibility is not a source.
|
|
169
|
+
await this.emit({
|
|
170
|
+
eventType: ControlEventType.TriggerRequestRecorded,
|
|
171
|
+
payload: {
|
|
172
|
+
triggerId: request.triggerId,
|
|
173
|
+
coordinationKey: request.coordinationKey,
|
|
174
|
+
source: request.source,
|
|
175
|
+
disposition: TriggerRequestDisposition.Skipped,
|
|
176
|
+
},
|
|
177
|
+
}, context, `trigger:consume:${request.triggerId}:${sourceIdentity(request.source)}`);
|
|
178
|
+
continue;
|
|
88
179
|
}
|
|
180
|
+
await this.emit({
|
|
181
|
+
eventType: ControlEventType.TriggerInvocationClaimed,
|
|
182
|
+
payload: {
|
|
183
|
+
triggerId: config.id,
|
|
184
|
+
coordinationKey: request.coordinationKey,
|
|
185
|
+
source: request.source,
|
|
186
|
+
sources: request.sources,
|
|
187
|
+
workItemId: this.dependencies.ids.next('work'),
|
|
188
|
+
workflowInstanceId: `workflow-${encodeURIComponent(identity)}`,
|
|
189
|
+
workflowName: config.workflow,
|
|
190
|
+
},
|
|
191
|
+
}, context, `${identity}:claim`);
|
|
192
|
+
await this.recover(context);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async recover(context) {
|
|
196
|
+
for (const invocation of (await this.view()).invocations) {
|
|
197
|
+
if (invocation.result === undefined)
|
|
198
|
+
await recoverTriggerInvocation({
|
|
199
|
+
work: this.dependencies.work,
|
|
200
|
+
orchestration: this.dependencies.orchestration,
|
|
201
|
+
emit: (input, command, id) => this.emit(input, command, id),
|
|
202
|
+
}, invocation, context);
|
|
89
203
|
}
|
|
90
204
|
}
|
|
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
205
|
}
|
|
@@ -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
|
+
}
|
|
@@ -2,6 +2,16 @@ import { createEventData } from '@atolis-hq/eventing';
|
|
|
2
2
|
import { ControlEventType } from './events.js';
|
|
3
3
|
export function createControlPlaneEventData(input) {
|
|
4
4
|
switch (input.eventType) {
|
|
5
|
+
case ControlEventType.TriggerRequestRecorded:
|
|
6
|
+
return createEventData(input);
|
|
7
|
+
case ControlEventType.TriggerInvocationClaimed:
|
|
8
|
+
return createEventData(input);
|
|
9
|
+
case ControlEventType.TriggerPaused:
|
|
10
|
+
return createEventData(input);
|
|
11
|
+
case ControlEventType.TriggerResumed:
|
|
12
|
+
return createEventData(input);
|
|
13
|
+
case ControlEventType.TriggerInvocationFinished:
|
|
14
|
+
return createEventData(input);
|
|
5
15
|
case ControlEventType.TriggerInvocationStarted:
|
|
6
16
|
return createEventData(input);
|
|
7
17
|
case ControlEventType.DispatchPaused:
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { type EventDataUnion, type EventEnvelope, type EventUnion } from '@atolis-hq/eventing';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import {
|
|
3
|
+
import { WorkflowStatus } from '../../orchestration/index.js';
|
|
4
|
+
import { type GlobalControlStreamRef, type TriggerControlStreamRef, type TriggerInvocationStreamRef } from './streams.js';
|
|
4
5
|
export declare const ControlEventType: {
|
|
6
|
+
readonly TriggerRequestRecorded: "control-plane.trigger-request-recorded";
|
|
7
|
+
readonly TriggerInvocationClaimed: "control-plane.trigger-invocation-claimed";
|
|
8
|
+
readonly TriggerPaused: "control-plane.trigger-paused";
|
|
9
|
+
readonly TriggerResumed: "control-plane.trigger-resumed";
|
|
10
|
+
readonly TriggerInvocationFinished: "control-plane.trigger-invocation-finished";
|
|
5
11
|
readonly TriggerInvocationStarted: "control-plane.trigger-invocation-started";
|
|
6
12
|
readonly DispatchPaused: "control-plane.dispatch-paused";
|
|
7
13
|
readonly DispatchResumed: "control-plane.dispatch-resumed";
|
|
@@ -13,11 +19,15 @@ export declare const RunnerPauseCause: {
|
|
|
13
19
|
readonly Quota: "quota";
|
|
14
20
|
};
|
|
15
21
|
export declare const ControlEventNamespace: "control-plane.";
|
|
22
|
+
export declare const TriggerRequestDisposition: {
|
|
23
|
+
readonly Coalesced: "coalesced";
|
|
24
|
+
readonly Skipped: "skipped";
|
|
25
|
+
};
|
|
16
26
|
export declare const TriggerSourceKind: {
|
|
17
27
|
readonly Event: "event";
|
|
18
28
|
readonly Cron: "cron";
|
|
19
29
|
};
|
|
20
|
-
declare const triggerSourceSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
30
|
+
export declare const triggerSourceSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
21
31
|
kind: z.ZodLiteral<"event">;
|
|
22
32
|
eventId: z.ZodString;
|
|
23
33
|
eventType: z.ZodString;
|
|
@@ -27,8 +37,43 @@ declare const triggerSourceSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
27
37
|
}, z.core.$strict>], "kind">;
|
|
28
38
|
export type TriggerSource = z.infer<typeof triggerSourceSchema>;
|
|
29
39
|
export interface ControlEventPayloads {
|
|
40
|
+
readonly [ControlEventType.TriggerRequestRecorded]: {
|
|
41
|
+
readonly triggerId: string;
|
|
42
|
+
readonly coordinationKey: string;
|
|
43
|
+
readonly source: TriggerSource;
|
|
44
|
+
readonly disposition: (typeof TriggerRequestDisposition)[keyof typeof TriggerRequestDisposition];
|
|
45
|
+
};
|
|
46
|
+
readonly [ControlEventType.TriggerInvocationClaimed]: {
|
|
47
|
+
readonly triggerId: string;
|
|
48
|
+
readonly coordinationKey: string;
|
|
49
|
+
readonly source: TriggerSource;
|
|
50
|
+
readonly sources: readonly TriggerSource[];
|
|
51
|
+
readonly workItemId: string;
|
|
52
|
+
readonly workflowInstanceId: string;
|
|
53
|
+
readonly workflowName: string;
|
|
54
|
+
};
|
|
55
|
+
readonly [ControlEventType.TriggerPaused]: {
|
|
56
|
+
readonly triggerId: string;
|
|
57
|
+
readonly reason: string;
|
|
58
|
+
readonly coordinationKey: string;
|
|
59
|
+
readonly workflowInstanceId?: string | undefined;
|
|
60
|
+
readonly outcomeCount?: number | undefined;
|
|
61
|
+
};
|
|
62
|
+
readonly [ControlEventType.TriggerResumed]: {
|
|
63
|
+
readonly triggerId: string;
|
|
64
|
+
readonly coordinationKey: string;
|
|
65
|
+
};
|
|
66
|
+
readonly [ControlEventType.TriggerInvocationFinished]: {
|
|
67
|
+
readonly triggerId: string;
|
|
68
|
+
readonly workflowInstanceId: string;
|
|
69
|
+
readonly workItemId: string;
|
|
70
|
+
readonly coordinationKey: string;
|
|
71
|
+
readonly status: typeof WorkflowStatus.Completed | typeof WorkflowStatus.Superseded;
|
|
72
|
+
readonly reason?: string | undefined;
|
|
73
|
+
};
|
|
30
74
|
readonly [ControlEventType.TriggerInvocationStarted]: {
|
|
31
75
|
readonly triggerId: string;
|
|
76
|
+
readonly coordinationKey: string;
|
|
32
77
|
readonly source: TriggerSource;
|
|
33
78
|
readonly workItemId: string;
|
|
34
79
|
readonly workflowInstanceId: string;
|
|
@@ -52,7 +97,9 @@ export interface ControlEventPayloads {
|
|
|
52
97
|
readonly resumedAt: string;
|
|
53
98
|
};
|
|
54
99
|
}
|
|
55
|
-
|
|
100
|
+
type InvocationEventType = typeof ControlEventType.TriggerInvocationClaimed | typeof ControlEventType.TriggerInvocationStarted | typeof ControlEventType.TriggerInvocationFinished;
|
|
101
|
+
type TriggerEventType = typeof ControlEventType.TriggerRequestRecorded | typeof ControlEventType.TriggerPaused | typeof ControlEventType.TriggerResumed;
|
|
102
|
+
export type ControlEvent = EventUnion<Pick<ControlEventPayloads, InvocationEventType>, TriggerInvocationStreamRef> | EventUnion<Pick<ControlEventPayloads, TriggerEventType>, TriggerControlStreamRef> | EventUnion<Omit<ControlEventPayloads, InvocationEventType | TriggerEventType>, GlobalControlStreamRef>;
|
|
56
103
|
export type ControlEventData = EventDataUnion<ControlEventPayloads>;
|
|
57
104
|
export declare function decodeControlEvent(event: EventEnvelope): ControlEvent;
|
|
58
105
|
export declare function selectControlEvent(event: EventEnvelope): ControlEvent | null;
|