@bratsos/workflow-engine-host-serverless 0.2.2
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/README.md +190 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.js +114 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# @bratsos/workflow-engine-host-serverless
|
|
2
|
+
|
|
3
|
+
Platform-agnostic serverless host for the [`@bratsos/workflow-engine`](../workflow-engine) command kernel. Works with Cloudflare Workers, AWS Lambda, Vercel Edge, Deno Deploy, and any stateless runtime.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @bratsos/workflow-engine-host-serverless
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createKernel } from "@bratsos/workflow-engine/kernel";
|
|
15
|
+
import { createServerlessHost } from "@bratsos/workflow-engine-host-serverless";
|
|
16
|
+
|
|
17
|
+
const kernel = createKernel({ /* ... */ });
|
|
18
|
+
|
|
19
|
+
const host = createServerlessHost({
|
|
20
|
+
kernel,
|
|
21
|
+
jobTransport,
|
|
22
|
+
workerId: "my-worker",
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Handle a job from a queue message
|
|
26
|
+
const result = await host.handleJob(msg);
|
|
27
|
+
|
|
28
|
+
// Run maintenance from a cron trigger
|
|
29
|
+
const tick = await host.runMaintenanceTick();
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
### `createServerlessHost(config): ServerlessHost`
|
|
35
|
+
|
|
36
|
+
Creates a new serverless host instance.
|
|
37
|
+
|
|
38
|
+
### `ServerlessHostConfig`
|
|
39
|
+
|
|
40
|
+
| Option | Type | Default | Description |
|
|
41
|
+
|--------|------|---------|-------------|
|
|
42
|
+
| `kernel` | `Kernel` | required | Kernel instance to dispatch commands to |
|
|
43
|
+
| `jobTransport` | `JobTransport` | required | Job transport for complete/suspend/fail lifecycle |
|
|
44
|
+
| `workerId` | `string` | required | Unique worker identifier (e.g. function name) |
|
|
45
|
+
| `staleLeaseThresholdMs` | `number` | `60_000` | Time before a job lease is considered stale |
|
|
46
|
+
| `maxClaimsPerTick` | `number` | `10` | Max pending runs to claim per maintenance tick |
|
|
47
|
+
| `maxSuspendedChecksPerTick` | `number` | `10` | Max suspended stages to poll per tick |
|
|
48
|
+
| `maxOutboxFlushPerTick` | `number` | `100` | Max outbox events to flush per tick |
|
|
49
|
+
|
|
50
|
+
### `ServerlessHost`
|
|
51
|
+
|
|
52
|
+
| Method | Returns | Description |
|
|
53
|
+
|--------|---------|-------------|
|
|
54
|
+
| `handleJob(msg)` | `Promise<JobResult>` | Execute a single pre-dequeued job |
|
|
55
|
+
| `processAvailableJobs(opts?)` | `Promise<ProcessJobsResult>` | Dequeue and process jobs |
|
|
56
|
+
| `runMaintenanceTick()` | `Promise<MaintenanceTickResult>` | Run one bounded maintenance cycle |
|
|
57
|
+
|
|
58
|
+
### `JobMessage`
|
|
59
|
+
|
|
60
|
+
The shape of a job message passed to `handleJob()`:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
interface JobMessage {
|
|
64
|
+
jobId: string;
|
|
65
|
+
workflowRunId: string;
|
|
66
|
+
workflowId: string;
|
|
67
|
+
stageId: string;
|
|
68
|
+
attempt: number;
|
|
69
|
+
payload: Record<string, unknown>;
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### `JobResult`
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
interface JobResult {
|
|
77
|
+
outcome: "completed" | "suspended" | "failed";
|
|
78
|
+
error?: string;
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### `ProcessJobsResult`
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
interface ProcessJobsResult {
|
|
86
|
+
processed: number;
|
|
87
|
+
succeeded: number;
|
|
88
|
+
failed: number;
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### `MaintenanceTickResult`
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
interface MaintenanceTickResult {
|
|
96
|
+
claimed: number; // Pending runs claimed
|
|
97
|
+
suspendedChecked: number; // Suspended stages polled
|
|
98
|
+
staleReleased: number; // Stale leases released
|
|
99
|
+
eventsFlushed: number; // Outbox events published
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Platform Integration Examples
|
|
104
|
+
|
|
105
|
+
### Cloudflare Workers (Queue + Cron)
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { createServerlessHost } from "@bratsos/workflow-engine-host-serverless";
|
|
109
|
+
|
|
110
|
+
export default {
|
|
111
|
+
// Queue consumer -- process jobs from Cloudflare Queue
|
|
112
|
+
async queue(batch, env, ctx) {
|
|
113
|
+
const host = createServerlessHost({ kernel, jobTransport, workerId: "cf-worker" });
|
|
114
|
+
|
|
115
|
+
for (const msg of batch.messages) {
|
|
116
|
+
const result = await host.handleJob(msg.body);
|
|
117
|
+
|
|
118
|
+
if (result.outcome === "failed") {
|
|
119
|
+
msg.retry();
|
|
120
|
+
} else {
|
|
121
|
+
msg.ack();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
// Cron trigger -- run maintenance
|
|
127
|
+
async scheduled(event, env, ctx) {
|
|
128
|
+
const host = createServerlessHost({ kernel, jobTransport, workerId: "cf-worker" });
|
|
129
|
+
ctx.waitUntil(host.runMaintenanceTick());
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### AWS Lambda (SQS + EventBridge)
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { createServerlessHost } from "@bratsos/workflow-engine-host-serverless";
|
|
138
|
+
|
|
139
|
+
// SQS handler -- process jobs
|
|
140
|
+
export async function jobHandler(event) {
|
|
141
|
+
const host = createServerlessHost({ kernel, jobTransport, workerId: "lambda-worker" });
|
|
142
|
+
|
|
143
|
+
for (const record of event.Records) {
|
|
144
|
+
const msg = JSON.parse(record.body);
|
|
145
|
+
await host.handleJob(msg);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// EventBridge handler -- maintenance cron
|
|
150
|
+
export async function maintenanceHandler() {
|
|
151
|
+
const host = createServerlessHost({ kernel, jobTransport, workerId: "lambda-worker" });
|
|
152
|
+
return host.runMaintenanceTick();
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Vercel Edge (API Route + Cron)
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { createServerlessHost } from "@bratsos/workflow-engine-host-serverless";
|
|
160
|
+
|
|
161
|
+
// POST /api/process-job
|
|
162
|
+
export async function POST(request: Request) {
|
|
163
|
+
const host = createServerlessHost({ kernel, jobTransport, workerId: "vercel-worker" });
|
|
164
|
+
const result = await host.processAvailableJobs({ maxJobs: 5 });
|
|
165
|
+
return Response.json(result);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// GET /api/cron/maintenance (Vercel Cron)
|
|
169
|
+
export async function GET() {
|
|
170
|
+
const host = createServerlessHost({ kernel, jobTransport, workerId: "vercel-worker" });
|
|
171
|
+
const tick = await host.runMaintenanceTick();
|
|
172
|
+
return Response.json(tick);
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## How It Works
|
|
177
|
+
|
|
178
|
+
Unlike the Node host, the serverless host has **no loops, timers, or signal handlers**. Every method is a single stateless invocation:
|
|
179
|
+
|
|
180
|
+
- **`handleJob(msg)`** -- Dispatches `job.execute` to the kernel, then marks the job complete/suspended/failed via `jobTransport`. On completion, also dispatches `run.transition` to advance the workflow.
|
|
181
|
+
|
|
182
|
+
- **`processAvailableJobs(opts?)`** -- Dequeues up to `maxJobs` (default: 1) from the job transport and processes each via `handleJob`. Safe for edge runtimes with CPU limits.
|
|
183
|
+
|
|
184
|
+
- **`runMaintenanceTick()`** -- Runs one bounded pass of all orchestration duties: `run.claimPending`, `stage.pollSuspended`, `lease.reapStale`, `outbox.flush`.
|
|
185
|
+
|
|
186
|
+
Consumers wire platform-specific glue (queue ack/retry, `waitUntil`, cron triggers) around these methods.
|
|
187
|
+
|
|
188
|
+
## License
|
|
189
|
+
|
|
190
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Kernel, JobTransport } from '@bratsos/workflow-engine/kernel';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Serverless Host for Workflow Engine Command Kernel
|
|
5
|
+
*
|
|
6
|
+
* Platform-agnostic host for serverless environments (Cloudflare Workers,
|
|
7
|
+
* AWS Lambda, Vercel Edge, Deno Deploy, etc.). Unlike the Node host, there
|
|
8
|
+
* are no timers, loops, or signal handlers — every method is a single
|
|
9
|
+
* stateless invocation.
|
|
10
|
+
*
|
|
11
|
+
* Consumers wire platform-specific glue (queue ack/retry, waitUntil,
|
|
12
|
+
* cron triggers) around these methods.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
interface ServerlessHostConfig {
|
|
16
|
+
/** Kernel instance to dispatch commands to. */
|
|
17
|
+
kernel: Kernel;
|
|
18
|
+
/** Job transport for complete/suspend/fail lifecycle. */
|
|
19
|
+
jobTransport: JobTransport;
|
|
20
|
+
/** Unique worker identifier (e.g. function name, worker name). */
|
|
21
|
+
workerId: string;
|
|
22
|
+
/** Stale lease threshold in milliseconds (default: 60_000). */
|
|
23
|
+
staleLeaseThresholdMs?: number;
|
|
24
|
+
/** Max pending runs to claim per maintenance tick (default: 10). */
|
|
25
|
+
maxClaimsPerTick?: number;
|
|
26
|
+
/** Max suspended stages to check per tick (default: 10). */
|
|
27
|
+
maxSuspendedChecksPerTick?: number;
|
|
28
|
+
/** Max outbox events to flush per tick (default: 100). */
|
|
29
|
+
maxOutboxFlushPerTick?: number;
|
|
30
|
+
}
|
|
31
|
+
/** Message shape representing a job to execute. Matches DequeueResult fields. */
|
|
32
|
+
interface JobMessage {
|
|
33
|
+
jobId: string;
|
|
34
|
+
workflowRunId: string;
|
|
35
|
+
workflowId: string;
|
|
36
|
+
stageId: string;
|
|
37
|
+
attempt: number;
|
|
38
|
+
maxAttempts?: number;
|
|
39
|
+
payload: Record<string, unknown>;
|
|
40
|
+
}
|
|
41
|
+
interface JobResult {
|
|
42
|
+
outcome: "completed" | "suspended" | "failed";
|
|
43
|
+
error?: string;
|
|
44
|
+
}
|
|
45
|
+
interface ProcessJobsResult {
|
|
46
|
+
processed: number;
|
|
47
|
+
succeeded: number;
|
|
48
|
+
failed: number;
|
|
49
|
+
}
|
|
50
|
+
interface MaintenanceTickResult {
|
|
51
|
+
claimed: number;
|
|
52
|
+
suspendedChecked: number;
|
|
53
|
+
staleReleased: number;
|
|
54
|
+
eventsFlushed: number;
|
|
55
|
+
}
|
|
56
|
+
interface ServerlessHost {
|
|
57
|
+
/** Execute a single pre-dequeued job. Returns outcome so consumer can ack/retry. */
|
|
58
|
+
handleJob(msg: JobMessage): Promise<JobResult>;
|
|
59
|
+
/**
|
|
60
|
+
* Dequeue and process jobs from the jobTransport.
|
|
61
|
+
* Defaults to 1 job per call (safe for edge runtimes with CPU limits).
|
|
62
|
+
* Pass maxJobs for longer-running environments like Lambda.
|
|
63
|
+
*/
|
|
64
|
+
processAvailableJobs(opts?: {
|
|
65
|
+
maxJobs?: number;
|
|
66
|
+
}): Promise<ProcessJobsResult>;
|
|
67
|
+
/** Run one bounded maintenance tick (claim, poll, reap, flush). */
|
|
68
|
+
runMaintenanceTick(): Promise<MaintenanceTickResult>;
|
|
69
|
+
}
|
|
70
|
+
declare function createServerlessHost(config: ServerlessHostConfig): ServerlessHost;
|
|
71
|
+
|
|
72
|
+
export { type JobMessage, type JobResult, type MaintenanceTickResult, type ProcessJobsResult, type ServerlessHost, type ServerlessHostConfig, createServerlessHost };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// src/host.ts
|
|
2
|
+
var ServerlessHostImpl = class {
|
|
3
|
+
kernel;
|
|
4
|
+
jobTransport;
|
|
5
|
+
workerId;
|
|
6
|
+
staleLeaseThresholdMs;
|
|
7
|
+
maxClaimsPerTick;
|
|
8
|
+
maxSuspendedChecksPerTick;
|
|
9
|
+
maxOutboxFlushPerTick;
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.kernel = config.kernel;
|
|
12
|
+
this.jobTransport = config.jobTransport;
|
|
13
|
+
this.workerId = config.workerId;
|
|
14
|
+
this.staleLeaseThresholdMs = config.staleLeaseThresholdMs ?? 6e4;
|
|
15
|
+
this.maxClaimsPerTick = config.maxClaimsPerTick ?? 10;
|
|
16
|
+
this.maxSuspendedChecksPerTick = config.maxSuspendedChecksPerTick ?? 10;
|
|
17
|
+
this.maxOutboxFlushPerTick = config.maxOutboxFlushPerTick ?? 100;
|
|
18
|
+
}
|
|
19
|
+
async handleJob(msg) {
|
|
20
|
+
const config = msg.payload.config || {};
|
|
21
|
+
const result = await this.kernel.dispatch({
|
|
22
|
+
type: "job.execute",
|
|
23
|
+
idempotencyKey: `job:${msg.jobId}:attempt:${msg.attempt}`,
|
|
24
|
+
workflowRunId: msg.workflowRunId,
|
|
25
|
+
workflowId: msg.workflowId,
|
|
26
|
+
stageId: msg.stageId,
|
|
27
|
+
config
|
|
28
|
+
});
|
|
29
|
+
if (result.outcome === "completed") {
|
|
30
|
+
await this.jobTransport.complete(msg.jobId);
|
|
31
|
+
await this.kernel.dispatch({
|
|
32
|
+
type: "run.transition",
|
|
33
|
+
workflowRunId: msg.workflowRunId
|
|
34
|
+
});
|
|
35
|
+
return { outcome: "completed" };
|
|
36
|
+
}
|
|
37
|
+
if (result.outcome === "suspended") {
|
|
38
|
+
const nextPollAt = result.nextPollAt ?? new Date(Date.now() + 6e4);
|
|
39
|
+
await this.jobTransport.suspend(msg.jobId, nextPollAt);
|
|
40
|
+
return { outcome: "suspended" };
|
|
41
|
+
}
|
|
42
|
+
const canRetry = msg.attempt < (msg.maxAttempts ?? 3);
|
|
43
|
+
await this.jobTransport.fail(
|
|
44
|
+
msg.jobId,
|
|
45
|
+
result.error ?? "Unknown error",
|
|
46
|
+
canRetry
|
|
47
|
+
);
|
|
48
|
+
return { outcome: "failed", error: result.error };
|
|
49
|
+
}
|
|
50
|
+
async processAvailableJobs(opts) {
|
|
51
|
+
const maxJobs = opts?.maxJobs ?? 1;
|
|
52
|
+
let processed = 0;
|
|
53
|
+
let succeeded = 0;
|
|
54
|
+
let failed = 0;
|
|
55
|
+
while (processed < maxJobs) {
|
|
56
|
+
const job = await this.jobTransport.dequeue();
|
|
57
|
+
if (!job) break;
|
|
58
|
+
const result = await this.handleJob({
|
|
59
|
+
jobId: job.jobId,
|
|
60
|
+
workflowRunId: job.workflowRunId,
|
|
61
|
+
workflowId: job.workflowId,
|
|
62
|
+
stageId: job.stageId,
|
|
63
|
+
attempt: job.attempt,
|
|
64
|
+
maxAttempts: job.maxAttempts,
|
|
65
|
+
payload: job.payload
|
|
66
|
+
});
|
|
67
|
+
processed++;
|
|
68
|
+
if (result.outcome === "failed") {
|
|
69
|
+
failed++;
|
|
70
|
+
} else {
|
|
71
|
+
succeeded++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { processed, succeeded, failed };
|
|
75
|
+
}
|
|
76
|
+
async runMaintenanceTick() {
|
|
77
|
+
const claimResult = await this.kernel.dispatch({
|
|
78
|
+
type: "run.claimPending",
|
|
79
|
+
workerId: this.workerId,
|
|
80
|
+
maxClaims: this.maxClaimsPerTick
|
|
81
|
+
});
|
|
82
|
+
const pollResult = await this.kernel.dispatch({
|
|
83
|
+
type: "stage.pollSuspended",
|
|
84
|
+
maxChecks: this.maxSuspendedChecksPerTick
|
|
85
|
+
});
|
|
86
|
+
for (const workflowRunId of pollResult.resumedWorkflowRunIds) {
|
|
87
|
+
await this.kernel.dispatch({
|
|
88
|
+
type: "run.transition",
|
|
89
|
+
workflowRunId
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const reapResult = await this.kernel.dispatch({
|
|
93
|
+
type: "lease.reapStale",
|
|
94
|
+
staleThresholdMs: this.staleLeaseThresholdMs
|
|
95
|
+
});
|
|
96
|
+
const flushResult = await this.kernel.dispatch({
|
|
97
|
+
type: "outbox.flush",
|
|
98
|
+
maxEvents: this.maxOutboxFlushPerTick
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
claimed: claimResult.claimed.length,
|
|
102
|
+
suspendedChecked: pollResult.checked,
|
|
103
|
+
staleReleased: reapResult.released,
|
|
104
|
+
eventsFlushed: flushResult.published
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
function createServerlessHost(config) {
|
|
109
|
+
return new ServerlessHostImpl(config);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export { createServerlessHost };
|
|
113
|
+
//# sourceMappingURL=index.js.map
|
|
114
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/host.ts"],"names":[],"mappings":";AAyFA,IAAM,qBAAN,MAAmD;AAAA,EAChC,MAAA;AAAA,EACA,YAAA;AAAA,EACA,QAAA;AAAA,EACA,qBAAA;AAAA,EACA,gBAAA;AAAA,EACA,yBAAA;AAAA,EACA,qBAAA;AAAA,EAEjB,YAAY,MAAA,EAA8B;AACxC,IAAA,IAAA,CAAK,SAAS,MAAA,CAAO,MAAA;AACrB,IAAA,IAAA,CAAK,eAAe,MAAA,CAAO,YAAA;AAC3B,IAAA,IAAA,CAAK,WAAW,MAAA,CAAO,QAAA;AACvB,IAAA,IAAA,CAAK,qBAAA,GAAwB,OAAO,qBAAA,IAAyB,GAAA;AAC7D,IAAA,IAAA,C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A,GAAqD;AAEzD,IAAA,MAAM,WAAA,GAAc,MAAM,IAAA,CAAK,MAAA,CAAO,QAAA,CAAS;AAAA,MAC7C,IAAA,EAAM,kBAAA;AAAA,MACN,UAAU,IAAA,CAAK,QAAA;AAAA,MACf,WAAW,IAAA,CAAK;AAAA,KACjB,CAAA;AAGD,IAAA,MAAM,UAAA,GAAa,MAAM,IAAA,CAAK,MAAA,CAAO,QAAA,CAAS;AAAA,MAC5C,IAAA,EAAM,qBAAA;AAAA,MACN,WAAW,IAAA,CAAK;AAAA,KACjB,CAAA;AACD,IAAA,KAAA,MAAW,aAAA,IAAiB,WAAW,qBAAA,EAAuB;AAC5D,MAAA,MAAM,IAAA,CAAK,OAAO,QAAA,CAAS;AAAA,QACzB,IAAA,EAAM,gBAAA;AAAA,QACN;AAAA,OACD,CAAA;AAAA,IACH;AAGA,IAAA,MAAM,UAAA,GAAa,MAAM,IAAA,CAAK,MAAA,CAAO,QAAA,CAAS;AAAA,MAC5C,IAAA,EAAM,iBAAA;AAAA,MACN,kBAAkB,IAAA,CAAK;AAAA,KACxB,CAAA;AAGD,IAAA,MAAM,WAAA,GAAc,MAAM,IAAA,CAAK,MAAA,CAAO,QAAA,CAAS;AAAA,MAC7C,IAAA,EAAM,cAAA;AAAA,MACN,WAAW,IAAA,CAAK;AAAA,KACjB,CAAA;AAED,IAAA,OAAO;AAAA,MACL,OAAA,EAAS,YAAY,OAAA,CAAQ,MAAA;AAAA,MAC7B,kBAAkB,UAAA,CAAW,OAAA;AAAA,MAC7B,eAAe,UAAA,CAAW,QAAA;AAAA,MAC1B,eAAe,WAAA,CAAY;AAAA,KAC7B;AAAA,EACF;AACF,CAAA;AAMO,SAAS,qBACd,MAAA,EACgB;AAChB,EAAA,OAAO,IAAI,mBAAmB,MAAM,CAAA;AACtC","file":"index.js","sourcesContent":["/**\n * Serverless Host for Workflow Engine Command Kernel\n *\n * Platform-agnostic host for serverless environments (Cloudflare Workers,\n * AWS Lambda, Vercel Edge, Deno Deploy, etc.). Unlike the Node host, there\n * are no timers, loops, or signal handlers — every method is a single\n * stateless invocation.\n *\n * Consumers wire platform-specific glue (queue ack/retry, waitUntil,\n * cron triggers) around these methods.\n */\n\nimport type { JobTransport, Kernel } from \"@bratsos/workflow-engine/kernel\";\n\n// ============================================================================\n// Public interfaces\n// ============================================================================\n\nexport interface ServerlessHostConfig {\n /** Kernel instance to dispatch commands to. */\n kernel: Kernel;\n\n /** Job transport for complete/suspend/fail lifecycle. */\n jobTransport: JobTransport;\n\n /** Unique worker identifier (e.g. function name, worker name). */\n workerId: string;\n\n /** Stale lease threshold in milliseconds (default: 60_000). */\n staleLeaseThresholdMs?: number;\n\n /** Max pending runs to claim per maintenance tick (default: 10). */\n maxClaimsPerTick?: number;\n\n /** Max suspended stages to check per tick (default: 10). */\n maxSuspendedChecksPerTick?: number;\n\n /** Max outbox events to flush per tick (default: 100). */\n maxOutboxFlushPerTick?: number;\n}\n\n/** Message shape representing a job to execute. Matches DequeueResult fields. */\nexport interface JobMessage {\n jobId: string;\n workflowRunId: string;\n workflowId: string;\n stageId: string;\n attempt: number;\n maxAttempts?: number;\n payload: Record<string, unknown>;\n}\n\nexport interface JobResult {\n outcome: \"completed\" | \"suspended\" | \"failed\";\n error?: string;\n}\n\nexport interface ProcessJobsResult {\n processed: number;\n succeeded: number;\n failed: number;\n}\n\nexport interface MaintenanceTickResult {\n claimed: number;\n suspendedChecked: number;\n staleReleased: number;\n eventsFlushed: number;\n}\n\nexport interface ServerlessHost {\n /** Execute a single pre-dequeued job. Returns outcome so consumer can ack/retry. */\n handleJob(msg: JobMessage): Promise<JobResult>;\n\n /**\n * Dequeue and process jobs from the jobTransport.\n * Defaults to 1 job per call (safe for edge runtimes with CPU limits).\n * Pass maxJobs for longer-running environments like Lambda.\n */\n processAvailableJobs(opts?: { maxJobs?: number }): Promise<ProcessJobsResult>;\n\n /** Run one bounded maintenance tick (claim, poll, reap, flush). */\n runMaintenanceTick(): Promise<MaintenanceTickResult>;\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\nclass ServerlessHostImpl implements ServerlessHost {\n private readonly kernel: Kernel;\n private readonly jobTransport: JobTransport;\n private readonly workerId: string;\n private readonly staleLeaseThresholdMs: number;\n private readonly maxClaimsPerTick: number;\n private readonly maxSuspendedChecksPerTick: number;\n private readonly maxOutboxFlushPerTick: number;\n\n constructor(config: ServerlessHostConfig) {\n this.kernel = config.kernel;\n this.jobTransport = config.jobTransport;\n this.workerId = config.workerId;\n this.staleLeaseThresholdMs = config.staleLeaseThresholdMs ?? 60_000;\n this.maxClaimsPerTick = config.maxClaimsPerTick ?? 10;\n this.maxSuspendedChecksPerTick = config.maxSuspendedChecksPerTick ?? 10;\n this.maxOutboxFlushPerTick = config.maxOutboxFlushPerTick ?? 100;\n }\n\n async handleJob(msg: JobMessage): Promise<JobResult> {\n const config =\n (msg.payload as { config?: Record<string, unknown> }).config || {};\n\n const result = await this.kernel.dispatch({\n type: \"job.execute\",\n idempotencyKey: `job:${msg.jobId}:attempt:${msg.attempt}`,\n workflowRunId: msg.workflowRunId,\n workflowId: msg.workflowId,\n stageId: msg.stageId,\n config,\n });\n\n if (result.outcome === \"completed\") {\n await this.jobTransport.complete(msg.jobId);\n await this.kernel.dispatch({\n type: \"run.transition\",\n workflowRunId: msg.workflowRunId,\n });\n return { outcome: \"completed\" };\n }\n\n if (result.outcome === \"suspended\") {\n const nextPollAt = result.nextPollAt ?? new Date(Date.now() + 60_000);\n await this.jobTransport.suspend(msg.jobId, nextPollAt);\n return { outcome: \"suspended\" };\n }\n\n // failed\n const canRetry = msg.attempt < (msg.maxAttempts ?? 3);\n await this.jobTransport.fail(\n msg.jobId,\n result.error ?? \"Unknown error\",\n canRetry,\n );\n return { outcome: \"failed\", error: result.error };\n }\n\n async processAvailableJobs(opts?: {\n maxJobs?: number;\n }): Promise<ProcessJobsResult> {\n const maxJobs = opts?.maxJobs ?? 1;\n let processed = 0;\n let succeeded = 0;\n let failed = 0;\n\n while (processed < maxJobs) {\n const job = await this.jobTransport.dequeue();\n if (!job) break;\n\n const result = await this.handleJob({\n jobId: job.jobId,\n workflowRunId: job.workflowRunId,\n workflowId: job.workflowId,\n stageId: job.stageId,\n attempt: job.attempt,\n maxAttempts: job.maxAttempts,\n payload: job.payload,\n });\n\n processed++;\n if (result.outcome === \"failed\") {\n failed++;\n } else {\n succeeded++;\n }\n }\n\n return { processed, succeeded, failed };\n }\n\n async runMaintenanceTick(): Promise<MaintenanceTickResult> {\n // 1. Claim pending runs → enqueue first-stage jobs\n const claimResult = await this.kernel.dispatch({\n type: \"run.claimPending\",\n workerId: this.workerId,\n maxClaims: this.maxClaimsPerTick,\n });\n\n // 2. Poll suspended stages → resume if ready\n const pollResult = await this.kernel.dispatch({\n type: \"stage.pollSuspended\",\n maxChecks: this.maxSuspendedChecksPerTick,\n });\n for (const workflowRunId of pollResult.resumedWorkflowRunIds) {\n await this.kernel.dispatch({\n type: \"run.transition\",\n workflowRunId,\n });\n }\n\n // 3. Reap stale leases → release crashed worker locks\n const reapResult = await this.kernel.dispatch({\n type: \"lease.reapStale\",\n staleThresholdMs: this.staleLeaseThresholdMs,\n });\n\n // 4. Flush outbox → publish pending events through EventSink\n const flushResult = await this.kernel.dispatch({\n type: \"outbox.flush\",\n maxEvents: this.maxOutboxFlushPerTick,\n });\n\n return {\n claimed: claimResult.claimed.length,\n suspendedChecked: pollResult.checked,\n staleReleased: reapResult.released,\n eventsFlushed: flushResult.published,\n };\n }\n}\n\n// ============================================================================\n// Factory\n// ============================================================================\n\nexport function createServerlessHost(\n config: ServerlessHostConfig,\n): ServerlessHost {\n return new ServerlessHostImpl(config);\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bratsos/workflow-engine-host-serverless",
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"description": "Platform-agnostic serverless host for @bratsos/workflow-engine command kernel",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Alex Bratsos",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsup",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"typecheck": "tsc --noEmit"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@bratsos/workflow-engine": "workspace:*"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsup": "^8.5.1",
|
|
33
|
+
"typescript": "^5.8.3",
|
|
34
|
+
"vitest": "^3.2.4",
|
|
35
|
+
"zod": "^4.1.12"
|
|
36
|
+
}
|
|
37
|
+
}
|