@atolis-hq/wake 0.4.60 → 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/canonical-events.d.ts +3 -0
- package/dist/src/bootstrap/canonical-events.js +32 -0
- package/dist/src/bootstrap/composition-root.d.ts +5 -2
- package/dist/src/bootstrap/composition-root.js +9 -3
- package/dist/src/bootstrap/config/root-schema.d.ts +8 -3
- package/dist/src/bootstrap/config/root-schema.js +29 -1
- package/dist/src/bootstrap/initialise.js +5 -0
- package/dist/src/bootstrap/integration-runtime.d.ts +9 -5
- package/dist/src/bootstrap/integration-runtime.js +113 -10
- package/dist/src/bootstrap/surface-cli-applications.js +1 -1
- package/dist/src/bootstrap/trigger-checkpoint-store.d.ts +9 -0
- package/dist/src/bootstrap/trigger-checkpoint-store.js +24 -0
- package/dist/src/bootstrap/version.js +1 -1
- package/dist/src/control-plane/application/control-plane-projection.js +13 -1
- package/dist/src/control-plane/application/runner-pipeline.d.ts +2 -2
- package/dist/src/control-plane/application/runner-pipeline.js +2 -4
- 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 +49 -0
- package/dist/src/control-plane/application/trigger-service.js +198 -0
- 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/config.d.ts +18 -8
- package/dist/src/control-plane/contracts/config.js +35 -6
- package/dist/src/control-plane/contracts/event-factory.js +12 -0
- package/dist/src/control-plane/contracts/events.d.ts +72 -2
- package/dist/src/control-plane/contracts/events.js +130 -2
- 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 +10 -0
- package/dist/src/control-plane/domain/{schedule-policy.js → cron-trigger-policy.js} +8 -6
- package/dist/src/control-plane/index.d.ts +5 -2
- package/dist/src/control-plane/index.js +5 -2
- package/package.json +1 -1
- package/dist/src/bootstrap/schedule-checkpoint-store.d.ts +0 -9
- package/dist/src/bootstrap/schedule-checkpoint-store.js +0 -49
- package/dist/src/control-plane/application/schedule-service.d.ts +0 -31
- package/dist/src/control-plane/application/schedule-service.js +0 -47
- package/dist/src/control-plane/domain/schedule-policy.d.ts +0 -8
|
@@ -1,27 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export interface TriggerConfig {
|
|
2
3
|
readonly id: string;
|
|
3
4
|
readonly workflow: string;
|
|
4
|
-
readonly
|
|
5
|
-
|
|
5
|
+
readonly on: {
|
|
6
|
+
readonly events?: readonly string[];
|
|
7
|
+
readonly cron?: string;
|
|
8
|
+
};
|
|
9
|
+
readonly when?: {
|
|
10
|
+
readonly capacityAvailable: true;
|
|
11
|
+
};
|
|
6
12
|
}
|
|
7
13
|
export interface ControlPlaneConfig {
|
|
8
14
|
readonly maxDispatches: number;
|
|
9
15
|
readonly maxConcurrentRuns: number;
|
|
10
|
-
readonly
|
|
16
|
+
readonly triggers: readonly TriggerConfig[];
|
|
11
17
|
readonly resident?: {
|
|
12
18
|
readonly pollBackoffMs: number;
|
|
13
19
|
readonly maxPollBackoffMs?: number;
|
|
14
20
|
};
|
|
15
21
|
}
|
|
16
|
-
import { z } from 'zod';
|
|
17
22
|
export declare const controlPlaneConfigSchema: z.ZodDefault<z.ZodObject<{
|
|
18
23
|
maxDispatches: z.ZodDefault<z.ZodNumber>;
|
|
19
24
|
maxConcurrentRuns: z.ZodDefault<z.ZodNumber>;
|
|
20
|
-
|
|
25
|
+
triggers: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
21
26
|
id: z.ZodString;
|
|
22
27
|
workflow: z.ZodString;
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
on: z.ZodObject<{
|
|
29
|
+
events: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
30
|
+
cron: z.ZodOptional<z.ZodString>;
|
|
31
|
+
}, z.core.$strict>;
|
|
32
|
+
when: z.ZodOptional<z.ZodObject<{
|
|
33
|
+
capacityAvailable: z.ZodLiteral<true>;
|
|
34
|
+
}, z.core.$strict>>;
|
|
25
35
|
}, z.core.$strict>>>;
|
|
26
36
|
resident: z.ZodDefault<z.ZodObject<{
|
|
27
37
|
pollBackoffMs: z.ZodDefault<z.ZodNumber>;
|
|
@@ -1,17 +1,46 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
|
|
2
|
+
import { CronTriggerPolicy } from '../domain/cron-trigger-policy.js';
|
|
3
|
+
const triggerSchema = z
|
|
3
4
|
.object({
|
|
4
|
-
id: z
|
|
5
|
+
id: z
|
|
6
|
+
.string()
|
|
7
|
+
.trim()
|
|
8
|
+
.min(1)
|
|
9
|
+
.refine((id) => !/[\\/]/.test(id), 'Trigger id must not contain path separators'),
|
|
5
10
|
workflow: z.string().trim().min(1),
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
on: z
|
|
12
|
+
.object({
|
|
13
|
+
events: z.array(z.string().trim().min(1)).min(1).optional(),
|
|
14
|
+
cron: z
|
|
15
|
+
.string()
|
|
16
|
+
.trim()
|
|
17
|
+
.min(1)
|
|
18
|
+
.superRefine((cron, context) => {
|
|
19
|
+
try {
|
|
20
|
+
new CronTriggerPolicy().elapsedSlots({ id: 'validation', cron }, '2026-01-01T00:00:00.000Z', null);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
context.addIssue({
|
|
24
|
+
code: 'custom',
|
|
25
|
+
message: error instanceof Error ? error.message : 'Invalid cron',
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
.optional(),
|
|
30
|
+
})
|
|
31
|
+
.strict()
|
|
32
|
+
.refine((on) => on.events !== undefined || on.cron !== undefined, 'At least one trigger source is required'),
|
|
33
|
+
when: z
|
|
34
|
+
.object({ capacityAvailable: z.literal(true) })
|
|
35
|
+
.strict()
|
|
36
|
+
.optional(),
|
|
8
37
|
})
|
|
9
38
|
.strict();
|
|
10
39
|
export const controlPlaneConfigSchema = z
|
|
11
40
|
.object({
|
|
12
41
|
maxDispatches: z.number().int().positive().default(1),
|
|
13
42
|
maxConcurrentRuns: z.number().int().positive().default(1),
|
|
14
|
-
|
|
43
|
+
triggers: z.array(triggerSchema).default([]),
|
|
15
44
|
resident: z
|
|
16
45
|
.object({
|
|
17
46
|
// Backoff for the resident loop's own retry cadence when idle or
|
|
@@ -27,6 +56,6 @@ export const controlPlaneConfigSchema = z
|
|
|
27
56
|
.default({
|
|
28
57
|
maxDispatches: 1,
|
|
29
58
|
maxConcurrentRuns: 1,
|
|
30
|
-
|
|
59
|
+
triggers: [],
|
|
31
60
|
resident: { pollBackoffMs: 1000 },
|
|
32
61
|
});
|
|
@@ -2,6 +2,18 @@ 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);
|
|
15
|
+
case ControlEventType.TriggerInvocationStarted:
|
|
16
|
+
return createEventData(input);
|
|
5
17
|
case ControlEventType.DispatchPaused:
|
|
6
18
|
return createEventData(input);
|
|
7
19
|
case ControlEventType.DispatchResumed:
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { type EventDataUnion, type EventEnvelope, type EventUnion } from '@atolis-hq/eventing';
|
|
2
|
-
import {
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { WorkflowStatus } from '../../orchestration/index.js';
|
|
4
|
+
import { type GlobalControlStreamRef, type TriggerControlStreamRef, type TriggerInvocationStreamRef } from './streams.js';
|
|
3
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";
|
|
11
|
+
readonly TriggerInvocationStarted: "control-plane.trigger-invocation-started";
|
|
4
12
|
readonly DispatchPaused: "control-plane.dispatch-paused";
|
|
5
13
|
readonly DispatchResumed: "control-plane.dispatch-resumed";
|
|
6
14
|
readonly RunnerPaused: "control-plane.runner-paused";
|
|
@@ -11,7 +19,66 @@ export declare const RunnerPauseCause: {
|
|
|
11
19
|
readonly Quota: "quota";
|
|
12
20
|
};
|
|
13
21
|
export declare const ControlEventNamespace: "control-plane.";
|
|
22
|
+
export declare const TriggerRequestDisposition: {
|
|
23
|
+
readonly Coalesced: "coalesced";
|
|
24
|
+
readonly Skipped: "skipped";
|
|
25
|
+
};
|
|
26
|
+
export declare const TriggerSourceKind: {
|
|
27
|
+
readonly Event: "event";
|
|
28
|
+
readonly Cron: "cron";
|
|
29
|
+
};
|
|
30
|
+
export declare const triggerSourceSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
31
|
+
kind: z.ZodLiteral<"event">;
|
|
32
|
+
eventId: z.ZodString;
|
|
33
|
+
eventType: z.ZodString;
|
|
34
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
35
|
+
kind: z.ZodLiteral<"cron">;
|
|
36
|
+
slot: z.ZodString;
|
|
37
|
+
}, z.core.$strict>], "kind">;
|
|
38
|
+
export type TriggerSource = z.infer<typeof triggerSourceSchema>;
|
|
14
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
|
+
};
|
|
74
|
+
readonly [ControlEventType.TriggerInvocationStarted]: {
|
|
75
|
+
readonly triggerId: string;
|
|
76
|
+
readonly coordinationKey: string;
|
|
77
|
+
readonly source: TriggerSource;
|
|
78
|
+
readonly workItemId: string;
|
|
79
|
+
readonly workflowInstanceId: string;
|
|
80
|
+
readonly workflowName: string;
|
|
81
|
+
};
|
|
15
82
|
readonly [ControlEventType.DispatchPaused]: {
|
|
16
83
|
readonly resumeAt: string;
|
|
17
84
|
readonly reason: string;
|
|
@@ -30,7 +97,10 @@ export interface ControlEventPayloads {
|
|
|
30
97
|
readonly resumedAt: string;
|
|
31
98
|
};
|
|
32
99
|
}
|
|
33
|
-
|
|
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>;
|
|
34
103
|
export type ControlEventData = EventDataUnion<ControlEventPayloads>;
|
|
35
104
|
export declare function decodeControlEvent(event: EventEnvelope): ControlEvent;
|
|
36
105
|
export declare function selectControlEvent(event: EventEnvelope): ControlEvent | null;
|
|
106
|
+
export {};
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { eventDataSchema, eventEnvelopeSchema, } from '@atolis-hq/eventing';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import {
|
|
3
|
+
import { WorkflowStatus } from '../../orchestration/index.js';
|
|
4
|
+
import { ControlStreamKind, } from './streams.js';
|
|
4
5
|
export const ControlEventType = {
|
|
6
|
+
TriggerRequestRecorded: 'control-plane.trigger-request-recorded',
|
|
7
|
+
TriggerInvocationClaimed: 'control-plane.trigger-invocation-claimed',
|
|
8
|
+
TriggerPaused: 'control-plane.trigger-paused',
|
|
9
|
+
TriggerResumed: 'control-plane.trigger-resumed',
|
|
10
|
+
TriggerInvocationFinished: 'control-plane.trigger-invocation-finished',
|
|
11
|
+
TriggerInvocationStarted: 'control-plane.trigger-invocation-started',
|
|
5
12
|
DispatchPaused: 'control-plane.dispatch-paused',
|
|
6
13
|
DispatchResumed: 'control-plane.dispatch-resumed',
|
|
7
14
|
RunnerPaused: 'control-plane.runner-paused',
|
|
@@ -9,10 +16,123 @@ export const ControlEventType = {
|
|
|
9
16
|
};
|
|
10
17
|
export const RunnerPauseCause = { Manual: 'manual', Quota: 'quota' };
|
|
11
18
|
export const ControlEventNamespace = 'control-plane.';
|
|
19
|
+
export const TriggerRequestDisposition = { Coalesced: 'coalesced', Skipped: 'skipped' };
|
|
20
|
+
export const TriggerSourceKind = { Event: 'event', Cron: 'cron' };
|
|
21
|
+
export const triggerSourceSchema = z.discriminatedUnion('kind', [
|
|
22
|
+
z
|
|
23
|
+
.object({
|
|
24
|
+
kind: z.literal(TriggerSourceKind.Event),
|
|
25
|
+
eventId: z.string().min(1),
|
|
26
|
+
eventType: z.string().min(1),
|
|
27
|
+
})
|
|
28
|
+
.strict(),
|
|
29
|
+
z
|
|
30
|
+
.object({
|
|
31
|
+
kind: z.literal(TriggerSourceKind.Cron),
|
|
32
|
+
slot: z.string().datetime({ offset: true }),
|
|
33
|
+
})
|
|
34
|
+
.strict(),
|
|
35
|
+
]);
|
|
12
36
|
const streamSchema = z
|
|
13
37
|
.object({ kind: z.literal(ControlStreamKind.Global), id: z.literal('global') })
|
|
14
38
|
.strict();
|
|
39
|
+
const triggerStreamSchema = z
|
|
40
|
+
.object({ kind: z.literal(ControlStreamKind.Trigger), id: z.string().min(1) })
|
|
41
|
+
.strict();
|
|
42
|
+
const invocationStreamSchema = z
|
|
43
|
+
.object({ kind: z.literal(ControlStreamKind.TriggerInvocation), id: z.string().min(1) })
|
|
44
|
+
.strict();
|
|
15
45
|
const eventSchema = z.union([
|
|
46
|
+
eventEnvelopeSchema.extend({
|
|
47
|
+
event: eventDataSchema.extend({
|
|
48
|
+
eventType: z.literal(ControlEventType.TriggerRequestRecorded),
|
|
49
|
+
payload: z
|
|
50
|
+
.object({
|
|
51
|
+
triggerId: z.string().min(1),
|
|
52
|
+
coordinationKey: z.string().min(1),
|
|
53
|
+
source: triggerSourceSchema,
|
|
54
|
+
disposition: z.enum(TriggerRequestDisposition),
|
|
55
|
+
})
|
|
56
|
+
.strict(),
|
|
57
|
+
}),
|
|
58
|
+
stream: triggerStreamSchema,
|
|
59
|
+
}),
|
|
60
|
+
eventEnvelopeSchema.extend({
|
|
61
|
+
event: eventDataSchema.extend({
|
|
62
|
+
eventType: z.literal(ControlEventType.TriggerInvocationClaimed),
|
|
63
|
+
payload: z
|
|
64
|
+
.object({
|
|
65
|
+
triggerId: z.string().min(1),
|
|
66
|
+
coordinationKey: z.string().min(1),
|
|
67
|
+
source: triggerSourceSchema,
|
|
68
|
+
sources: z.array(triggerSourceSchema).min(1),
|
|
69
|
+
workItemId: z.string().min(1),
|
|
70
|
+
workflowInstanceId: z.string().min(1),
|
|
71
|
+
workflowName: z.string().min(1),
|
|
72
|
+
})
|
|
73
|
+
.strict(),
|
|
74
|
+
}),
|
|
75
|
+
stream: invocationStreamSchema,
|
|
76
|
+
}),
|
|
77
|
+
eventEnvelopeSchema.extend({
|
|
78
|
+
event: eventDataSchema.extend({
|
|
79
|
+
eventType: z.literal(ControlEventType.TriggerPaused),
|
|
80
|
+
payload: z
|
|
81
|
+
.object({
|
|
82
|
+
triggerId: z.string().min(1),
|
|
83
|
+
coordinationKey: z.string().min(1),
|
|
84
|
+
reason: z.string().min(1),
|
|
85
|
+
workflowInstanceId: z.string().min(1).optional(),
|
|
86
|
+
outcomeCount: z.number().int().nonnegative().optional(),
|
|
87
|
+
})
|
|
88
|
+
.strict()
|
|
89
|
+
.refine((value) => (value.workflowInstanceId === undefined) === (value.outcomeCount === undefined), {
|
|
90
|
+
message: 'Failure pause requires both workflow identity and outcome count',
|
|
91
|
+
}),
|
|
92
|
+
}),
|
|
93
|
+
stream: triggerStreamSchema,
|
|
94
|
+
}),
|
|
95
|
+
eventEnvelopeSchema.extend({
|
|
96
|
+
event: eventDataSchema.extend({
|
|
97
|
+
eventType: z.literal(ControlEventType.TriggerResumed),
|
|
98
|
+
payload: z
|
|
99
|
+
.object({ triggerId: z.string().min(1), coordinationKey: z.string().min(1) })
|
|
100
|
+
.strict(),
|
|
101
|
+
}),
|
|
102
|
+
stream: triggerStreamSchema,
|
|
103
|
+
}),
|
|
104
|
+
eventEnvelopeSchema.extend({
|
|
105
|
+
event: eventDataSchema.extend({
|
|
106
|
+
eventType: z.literal(ControlEventType.TriggerInvocationFinished),
|
|
107
|
+
payload: z
|
|
108
|
+
.object({
|
|
109
|
+
triggerId: z.string().min(1),
|
|
110
|
+
workflowInstanceId: z.string().min(1),
|
|
111
|
+
workItemId: z.string().min(1),
|
|
112
|
+
coordinationKey: z.string().min(1),
|
|
113
|
+
status: z.enum([WorkflowStatus.Completed, WorkflowStatus.Superseded]),
|
|
114
|
+
reason: z.string().optional(),
|
|
115
|
+
})
|
|
116
|
+
.strict(),
|
|
117
|
+
}),
|
|
118
|
+
stream: invocationStreamSchema,
|
|
119
|
+
}),
|
|
120
|
+
eventEnvelopeSchema.extend({
|
|
121
|
+
event: eventDataSchema.extend({
|
|
122
|
+
eventType: z.literal(ControlEventType.TriggerInvocationStarted),
|
|
123
|
+
payload: z
|
|
124
|
+
.object({
|
|
125
|
+
triggerId: z.string().min(1),
|
|
126
|
+
coordinationKey: z.string().min(1),
|
|
127
|
+
source: triggerSourceSchema,
|
|
128
|
+
workItemId: z.string().min(1),
|
|
129
|
+
workflowInstanceId: z.string().min(1),
|
|
130
|
+
workflowName: z.string().min(1),
|
|
131
|
+
})
|
|
132
|
+
.strict(),
|
|
133
|
+
}),
|
|
134
|
+
stream: invocationStreamSchema,
|
|
135
|
+
}),
|
|
16
136
|
eventEnvelopeSchema.extend({
|
|
17
137
|
event: eventDataSchema.extend({
|
|
18
138
|
eventType: z.literal(ControlEventType.DispatchPaused),
|
|
@@ -69,7 +189,15 @@ export function decodeControlEvent(event) {
|
|
|
69
189
|
throw new Error(`Invalid Control Plane event ${event.event.eventId}: ${result.error.message}`, {
|
|
70
190
|
cause: result.error,
|
|
71
191
|
});
|
|
72
|
-
|
|
192
|
+
const decoded = result.data;
|
|
193
|
+
if ('workItemId' in decoded.event.payload &&
|
|
194
|
+
decoded.stream.id !== decoded.event.payload.workItemId)
|
|
195
|
+
throw new Error(`Invalid Control Plane event ${event.event.eventId}: invocation stream identity mismatch`);
|
|
196
|
+
if (decoded.stream.kind === ControlStreamKind.Trigger &&
|
|
197
|
+
'coordinationKey' in decoded.event.payload &&
|
|
198
|
+
decoded.stream.id !== decoded.event.payload.coordinationKey)
|
|
199
|
+
throw new Error(`Invalid Control Plane event ${event.event.eventId}: coordination stream identity mismatch`);
|
|
200
|
+
return decoded;
|
|
73
201
|
}
|
|
74
202
|
export function selectControlEvent(event) {
|
|
75
203
|
return event.event.eventType.startsWith(ControlEventNamespace) ? decodeControlEvent(event) : null;
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import type { EntityRef } from '../../kernel/index.js';
|
|
2
2
|
export declare const ControlStreamKind: {
|
|
3
3
|
readonly Global: "control-plane";
|
|
4
|
+
readonly Trigger: "control-plane-trigger";
|
|
5
|
+
readonly TriggerInvocation: "control-plane-trigger-invocation";
|
|
4
6
|
};
|
|
5
|
-
export type
|
|
6
|
-
export
|
|
7
|
+
export type GlobalControlStreamRef = EntityRef<typeof ControlStreamKind.Global, 'global'>;
|
|
8
|
+
export type TriggerControlStreamRef = EntityRef<typeof ControlStreamKind.Trigger>;
|
|
9
|
+
export type TriggerInvocationStreamRef = EntityRef<typeof ControlStreamKind.TriggerInvocation>;
|
|
10
|
+
export type ControlStreamRef = GlobalControlStreamRef | TriggerControlStreamRef | TriggerInvocationStreamRef;
|
|
11
|
+
export declare const controlPlaneStream: () => GlobalControlStreamRef;
|
|
12
|
+
export declare const triggerControlStream: (coordinationKey: string) => TriggerControlStreamRef;
|
|
13
|
+
export declare const triggerInvocationStream: (workItemId: string) => TriggerInvocationStreamRef;
|
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
export const ControlStreamKind = {
|
|
1
|
+
export const ControlStreamKind = {
|
|
2
|
+
Global: 'control-plane',
|
|
3
|
+
Trigger: 'control-plane-trigger',
|
|
4
|
+
TriggerInvocation: 'control-plane-trigger-invocation',
|
|
5
|
+
};
|
|
2
6
|
export const controlPlaneStream = () => ({
|
|
3
7
|
kind: ControlStreamKind.Global,
|
|
4
8
|
id: 'global',
|
|
5
9
|
});
|
|
10
|
+
export const triggerControlStream = (coordinationKey) => ({
|
|
11
|
+
kind: ControlStreamKind.Trigger,
|
|
12
|
+
id: coordinationKey,
|
|
13
|
+
});
|
|
14
|
+
export const triggerInvocationStream = (workItemId) => ({
|
|
15
|
+
kind: ControlStreamKind.TriggerInvocation,
|
|
16
|
+
id: workItemId,
|
|
17
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface CronTriggerSlot {
|
|
2
|
+
readonly identity: string;
|
|
3
|
+
readonly at: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class CronTriggerPolicy {
|
|
6
|
+
elapsedSlots(config: {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly cron: string;
|
|
9
|
+
}, now: string, checkpoint: string | null, maximumSlots?: number): readonly CronTriggerSlot[];
|
|
10
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { CronExpressionParser } from 'cron-parser';
|
|
2
|
-
export class
|
|
3
|
-
elapsedSlots(config, now, checkpoint) {
|
|
2
|
+
export class CronTriggerPolicy {
|
|
3
|
+
elapsedSlots(config, now, checkpoint, maximumSlots = Number.POSITIVE_INFINITY) {
|
|
4
4
|
const end = minute(new Date(now));
|
|
5
5
|
const start = checkpoint === null ? end : minute(new Date(new Date(checkpoint).getTime() + 60_000));
|
|
6
6
|
if (start > end)
|
|
7
7
|
return [];
|
|
8
8
|
const fields = config.cron.trim().split(/\s+/);
|
|
9
9
|
if (fields.length !== 5)
|
|
10
|
-
throw new Error(`
|
|
10
|
+
throw new Error(`Trigger ${config.id} must use a five-field cron expression`);
|
|
11
11
|
if (fields.some(hasUnsupportedSyntax))
|
|
12
|
-
throw new Error(`
|
|
12
|
+
throw new Error(`Trigger ${config.id} uses unsupported cron syntax`);
|
|
13
13
|
const expression = CronExpressionParser.parse(config.cron, {
|
|
14
14
|
currentDate: new Date(start - 1),
|
|
15
15
|
endDate: new Date(end),
|
|
@@ -23,14 +23,16 @@ export class SchedulePolicy {
|
|
|
23
23
|
if (!matchesDays(date, dayOfMonth, dayOfWeek))
|
|
24
24
|
continue;
|
|
25
25
|
const at = date.toISOString();
|
|
26
|
-
slots.push({ identity: `
|
|
26
|
+
slots.push({ identity: `trigger:${config.id}:cron:${at}`, at });
|
|
27
|
+
if (slots.length >= maximumSlots)
|
|
28
|
+
break;
|
|
27
29
|
}
|
|
28
30
|
return slots;
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
function minute(value) {
|
|
32
34
|
if (Number.isNaN(value.getTime()))
|
|
33
|
-
throw new Error('
|
|
35
|
+
throw new Error('Trigger timestamps must be valid dates');
|
|
34
36
|
return Math.floor(value.getTime() / 60_000) * 60_000;
|
|
35
37
|
}
|
|
36
38
|
function hasUnsupportedSyntax(field) {
|
|
@@ -5,7 +5,8 @@ export type * from './application/activation-scheduler-ports.js';
|
|
|
5
5
|
export * from './application/control-plane-projection.js';
|
|
6
6
|
export * from './application/control-plane-service.js';
|
|
7
7
|
export * from './application/runner-control-service.js';
|
|
8
|
-
export * from './application/
|
|
8
|
+
export * from './application/trigger-reactor.js';
|
|
9
|
+
export * from './application/trigger-service.js';
|
|
9
10
|
export * from './application/intake-pipeline.js';
|
|
10
11
|
export * from './application/runner-pipeline.js';
|
|
11
12
|
export * from './application/work-cancellation-policy.js';
|
|
@@ -18,7 +19,9 @@ export * from './contracts/views.js';
|
|
|
18
19
|
export * from './domain/dispatch-policy.js';
|
|
19
20
|
export * from './domain/quota-policy.js';
|
|
20
21
|
export * from './domain/runner-quota.js';
|
|
21
|
-
export * from './domain/
|
|
22
|
+
export * from './domain/cron-trigger-policy.js';
|
|
22
23
|
export * from './infrastructure/intake-host.js';
|
|
23
24
|
export * from './infrastructure/resident-host.js';
|
|
24
25
|
export * from './infrastructure/tick-host.js';
|
|
26
|
+
export * from './application/trigger-invocation-projection.js';
|
|
27
|
+
export * from './application/trigger-projection.js';
|
|
@@ -4,7 +4,8 @@ export * from './application/activation-scheduler-subscriber.js';
|
|
|
4
4
|
export * from './application/control-plane-projection.js';
|
|
5
5
|
export * from './application/control-plane-service.js';
|
|
6
6
|
export * from './application/runner-control-service.js';
|
|
7
|
-
export * from './application/
|
|
7
|
+
export * from './application/trigger-reactor.js';
|
|
8
|
+
export * from './application/trigger-service.js';
|
|
8
9
|
export * from './application/intake-pipeline.js';
|
|
9
10
|
export * from './application/runner-pipeline.js';
|
|
10
11
|
export * from './application/work-cancellation-policy.js';
|
|
@@ -17,7 +18,9 @@ export * from './contracts/views.js';
|
|
|
17
18
|
export * from './domain/dispatch-policy.js';
|
|
18
19
|
export * from './domain/quota-policy.js';
|
|
19
20
|
export * from './domain/runner-quota.js';
|
|
20
|
-
export * from './domain/
|
|
21
|
+
export * from './domain/cron-trigger-policy.js';
|
|
21
22
|
export * from './infrastructure/intake-host.js';
|
|
22
23
|
export * from './infrastructure/resident-host.js';
|
|
23
24
|
export * from './infrastructure/tick-host.js';
|
|
25
|
+
export * from './application/trigger-invocation-projection.js';
|
|
26
|
+
export * from './application/trigger-projection.js';
|
package/package.json
CHANGED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ScheduleCheckpointStore } from '../control-plane/index.js';
|
|
2
|
-
/** Durable timestamp cursor for ScheduleService; journal checkpoints only store numeric offsets. */
|
|
3
|
-
export declare class FileScheduleCheckpointStore implements ScheduleCheckpointStore {
|
|
4
|
-
private readonly dataRoot;
|
|
5
|
-
constructor(dataRoot: string);
|
|
6
|
-
load(scheduleId: string): Promise<string | null>;
|
|
7
|
-
save(scheduleId: string, slot: string): Promise<void>;
|
|
8
|
-
private path;
|
|
9
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { mkdir, open, readFile, rename, rm } from 'node:fs/promises';
|
|
2
|
-
import { dirname, join } from 'node:path';
|
|
3
|
-
/** Durable timestamp cursor for ScheduleService; journal checkpoints only store numeric offsets. */
|
|
4
|
-
export class FileScheduleCheckpointStore {
|
|
5
|
-
dataRoot;
|
|
6
|
-
constructor(dataRoot) {
|
|
7
|
-
this.dataRoot = dataRoot;
|
|
8
|
-
}
|
|
9
|
-
async load(scheduleId) {
|
|
10
|
-
try {
|
|
11
|
-
const parsed = JSON.parse(await readFile(this.path(scheduleId), 'utf8'));
|
|
12
|
-
if (parsed.scheduleId !== scheduleId || Number.isNaN(Date.parse(parsed.slot)))
|
|
13
|
-
throw new Error(`Invalid schedule checkpoint: ${scheduleId}`);
|
|
14
|
-
return parsed.slot;
|
|
15
|
-
}
|
|
16
|
-
catch (error) {
|
|
17
|
-
if (error.code === 'ENOENT')
|
|
18
|
-
return null;
|
|
19
|
-
throw error;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
async save(scheduleId, slot) {
|
|
23
|
-
const existing = await this.load(scheduleId);
|
|
24
|
-
if (existing !== null && Date.parse(slot) < Date.parse(existing))
|
|
25
|
-
throw new Error(`Schedule checkpoint regression: ${scheduleId}`);
|
|
26
|
-
const path = this.path(scheduleId);
|
|
27
|
-
await mkdir(dirname(path), { recursive: true });
|
|
28
|
-
const temporary = `${path}.${process.pid}.${Date.now()}.tmp`;
|
|
29
|
-
try {
|
|
30
|
-
const handle = await open(temporary, 'wx');
|
|
31
|
-
try {
|
|
32
|
-
await handle.writeFile(`${JSON.stringify({ scheduleId, slot })}\n`, 'utf8');
|
|
33
|
-
await handle.sync();
|
|
34
|
-
}
|
|
35
|
-
finally {
|
|
36
|
-
await handle.close();
|
|
37
|
-
}
|
|
38
|
-
await rename(temporary, path);
|
|
39
|
-
}
|
|
40
|
-
finally {
|
|
41
|
-
await rm(temporary, { force: true });
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
path(scheduleId) {
|
|
45
|
-
if (scheduleId.length === 0 || /[\\/]/.test(scheduleId))
|
|
46
|
-
throw new Error('Schedule id must not contain path separators');
|
|
47
|
-
return join(this.dataRoot, 'schedule-checkpoints', `${encodeURIComponent(scheduleId)}.json`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { type CommandContext } from '@atolis-hq/eventing';
|
|
2
|
-
import { type IdGenerator } from '../../kernel/index.js';
|
|
3
|
-
import type { StartWorkflowInstance } from '../../orchestration/index.js';
|
|
4
|
-
import { workflowInstanceId } from '../../orchestration/index.js';
|
|
5
|
-
import type { CreateWorkItem } from '../../work/index.js';
|
|
6
|
-
import { workItemId } from '../../work/index.js';
|
|
7
|
-
import type { ScheduleConfig } from '../contracts/config.js';
|
|
8
|
-
export interface ScheduleCheckpointStore {
|
|
9
|
-
load(scheduleId: string): Promise<string | null>;
|
|
10
|
-
save(scheduleId: string, slot: string): Promise<void>;
|
|
11
|
-
}
|
|
12
|
-
export interface ScheduleServiceDependencies {
|
|
13
|
-
readonly checkpoint: ScheduleCheckpointStore;
|
|
14
|
-
readonly ids: IdGenerator;
|
|
15
|
-
readonly work: {
|
|
16
|
-
create(command: CreateWorkItem, context: CommandContext): Promise<unknown>;
|
|
17
|
-
};
|
|
18
|
-
readonly orchestration: Pick<{
|
|
19
|
-
start(command: StartWorkflowInstance, context: CommandContext): Promise<unknown>;
|
|
20
|
-
get(id: ReturnType<typeof workflowInstanceId>): Promise<{
|
|
21
|
-
readonly workItemId: ReturnType<typeof workItemId>;
|
|
22
|
-
} | null>;
|
|
23
|
-
}, 'start' | 'get'>;
|
|
24
|
-
readonly now: () => string;
|
|
25
|
-
}
|
|
26
|
-
export declare class ScheduleService {
|
|
27
|
-
private readonly dependencies;
|
|
28
|
-
private readonly policy;
|
|
29
|
-
constructor(dependencies: ScheduleServiceDependencies);
|
|
30
|
-
run(config: ScheduleConfig, context: CommandContext): Promise<readonly string[]>;
|
|
31
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { correlationId } from '@atolis-hq/eventing';
|
|
2
|
-
import { orchestrationGroupId, workflowInstanceId, workflowName, } from '../../orchestration/index.js';
|
|
3
|
-
import { workItemId } from '../../work/index.js';
|
|
4
|
-
import { SchedulePolicy } from '../domain/schedule-policy.js';
|
|
5
|
-
export class ScheduleService {
|
|
6
|
-
dependencies;
|
|
7
|
-
policy = new SchedulePolicy();
|
|
8
|
-
constructor(dependencies) {
|
|
9
|
-
this.dependencies = dependencies;
|
|
10
|
-
}
|
|
11
|
-
async run(config, context) {
|
|
12
|
-
const checkpoint = await this.dependencies.checkpoint.load(config.id);
|
|
13
|
-
const slots = this.policy.elapsedSlots(config, this.dependencies.now(), checkpoint);
|
|
14
|
-
const accepted = [];
|
|
15
|
-
for (const slot of slots) {
|
|
16
|
-
const workflow = workflowInstanceId(`workflow-${safe(slot.at)}`);
|
|
17
|
-
const slotContext = slotCommandContext(context, slot.identity);
|
|
18
|
-
const existing = await this.dependencies.orchestration.get(workflow);
|
|
19
|
-
const item = existing?.workItemId ?? workItemId(this.dependencies.ids.next('work'));
|
|
20
|
-
if (existing === null) {
|
|
21
|
-
const workCommand = { workItemId: item, objective: config.objective };
|
|
22
|
-
await this.dependencies.work.create(workCommand, slotContext);
|
|
23
|
-
}
|
|
24
|
-
const start = {
|
|
25
|
-
workflowInstanceId: workflow,
|
|
26
|
-
workItemId: item,
|
|
27
|
-
workflowName: workflowName(config.workflow),
|
|
28
|
-
orchestrationGroupId: orchestrationGroupId(`primary:${item}`),
|
|
29
|
-
};
|
|
30
|
-
await this.dependencies.orchestration.start(start, slotContext);
|
|
31
|
-
await this.dependencies.checkpoint.save(config.id, slot.at);
|
|
32
|
-
accepted.push(slot.identity);
|
|
33
|
-
}
|
|
34
|
-
return accepted;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
function slotCommandContext(context, identity) {
|
|
38
|
-
const suffix = safe(identity);
|
|
39
|
-
return {
|
|
40
|
-
...context,
|
|
41
|
-
commandId: `${context.commandId}:${suffix}`,
|
|
42
|
-
correlationId: correlationId(`${context.correlationId}:${suffix}`),
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
function safe(value) {
|
|
46
|
-
return value.toLowerCase().replace(/[^a-z0-9-]/g, '-');
|
|
47
|
-
}
|