@porulle/jobs-cloudflare 0.9.0
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/LICENSE +21 -0
- package/README.md +21 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +81 -0
- package/package.json +49 -0
- package/src/index.ts +165 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 unified-commerce-engine contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# `@porulle/jobs-cloudflare`
|
|
2
|
+
|
|
3
|
+
Cloudflare Workflows binding adapter and task runner for Porulle. Keyed exclusivity and supersession require a durable coordinator (normally a Durable Object); the adapter fails fast when a keyed task is used without one.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { WorkflowEntrypoint } from "cloudflare:workers";
|
|
7
|
+
import { CloudflareExecutionEngine } from "@porulle/jobs-cloudflare";
|
|
8
|
+
|
|
9
|
+
export const jobs = new CloudflareExecutionEngine({
|
|
10
|
+
workflow: env.PORULLE_WORKFLOW,
|
|
11
|
+
coordinator,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export class PorulleWorkflow extends WorkflowEntrypoint {
|
|
15
|
+
async run(event, step) {
|
|
16
|
+
return jobs.run(event.payload, step);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The coordinator must durably supersede pending instances at enqueue and serialize `run()` by concurrency key. Unkeyed tasks can use the Workflow binding directly.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { EnqueueOptions, ExecutionEngine, ExecutionEngineSetup } from "@porulle/core";
|
|
2
|
+
export interface CloudflareJobPayload {
|
|
3
|
+
taskSlug: string;
|
|
4
|
+
input: Record<string, unknown>;
|
|
5
|
+
organizationId: string;
|
|
6
|
+
maxAttempts: number;
|
|
7
|
+
delayMs?: number;
|
|
8
|
+
concurrencyKey?: string;
|
|
9
|
+
exclusive: boolean;
|
|
10
|
+
supersedes: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface WorkflowBinding {
|
|
13
|
+
create(options: {
|
|
14
|
+
id?: string;
|
|
15
|
+
params: CloudflareJobPayload;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
id: string;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
export interface WorkflowStep {
|
|
21
|
+
sleep(name: string, duration: number | string): Promise<void>;
|
|
22
|
+
do<T>(name: string, config: {
|
|
23
|
+
retries: {
|
|
24
|
+
limit: number;
|
|
25
|
+
delay: number;
|
|
26
|
+
backoff: "constant" | "exponential";
|
|
27
|
+
};
|
|
28
|
+
}, callback: (context: {
|
|
29
|
+
attempt: number;
|
|
30
|
+
}) => Promise<T>): Promise<T>;
|
|
31
|
+
}
|
|
32
|
+
export interface CloudflareConcurrencyCoordinator {
|
|
33
|
+
enqueue(payload: CloudflareJobPayload, create: () => Promise<{
|
|
34
|
+
id: string;
|
|
35
|
+
}>): Promise<{
|
|
36
|
+
id: string;
|
|
37
|
+
}>;
|
|
38
|
+
run<T>(key: string, handler: () => Promise<T>): Promise<T>;
|
|
39
|
+
}
|
|
40
|
+
export interface CloudflareExecutionEngineOptions {
|
|
41
|
+
workflow: WorkflowBinding;
|
|
42
|
+
coordinator?: CloudflareConcurrencyCoordinator;
|
|
43
|
+
}
|
|
44
|
+
export declare class CloudflareExecutionEngine implements ExecutionEngine {
|
|
45
|
+
private readonly options;
|
|
46
|
+
readonly execution: {
|
|
47
|
+
mode: "push";
|
|
48
|
+
};
|
|
49
|
+
private setup;
|
|
50
|
+
constructor(options: CloudflareExecutionEngineOptions);
|
|
51
|
+
register(setup: ExecutionEngineSetup): void;
|
|
52
|
+
enqueue(taskSlug: string, input: Record<string, unknown>, options: EnqueueOptions): Promise<string>;
|
|
53
|
+
run(payload: CloudflareJobPayload, step: WorkflowStep): Promise<Record<string, unknown>>;
|
|
54
|
+
private requireSetup;
|
|
55
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export class CloudflareExecutionEngine {
|
|
2
|
+
options;
|
|
3
|
+
execution = { mode: "push" };
|
|
4
|
+
setup;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.options = options;
|
|
7
|
+
}
|
|
8
|
+
register(setup) {
|
|
9
|
+
this.setup = setup;
|
|
10
|
+
}
|
|
11
|
+
async enqueue(taskSlug, input, options) {
|
|
12
|
+
const task = this.requireSetup().tasks.get(taskSlug);
|
|
13
|
+
if (!task)
|
|
14
|
+
throw new Error(`Unknown task slug: ${taskSlug}`);
|
|
15
|
+
const organizationId = options.organizationId.trim();
|
|
16
|
+
if (!organizationId)
|
|
17
|
+
throw new Error("Jobs enqueue requires a non-empty organizationId.");
|
|
18
|
+
const concurrencyKey = options.concurrencyKey ?? task.concurrency?.key(input);
|
|
19
|
+
const exclusive = Boolean(task.concurrency && task.concurrency.exclusive !== false);
|
|
20
|
+
const supersedes = Boolean(options.supersedes ?? task.concurrency?.supersedes);
|
|
21
|
+
if ((exclusive || supersedes) &&
|
|
22
|
+
concurrencyKey &&
|
|
23
|
+
!this.options.coordinator) {
|
|
24
|
+
throw new Error("Cloudflare Workflows has no per-key queue concurrency primitive. Configure a durable CloudflareConcurrencyCoordinator for exclusive or superseding tasks.");
|
|
25
|
+
}
|
|
26
|
+
const payload = {
|
|
27
|
+
taskSlug,
|
|
28
|
+
input,
|
|
29
|
+
organizationId,
|
|
30
|
+
maxAttempts: options.maxAttempts ?? task.retries?.attempts ?? 1,
|
|
31
|
+
exclusive,
|
|
32
|
+
supersedes,
|
|
33
|
+
...(options.delayMs !== undefined ? { delayMs: options.delayMs } : {}),
|
|
34
|
+
...(concurrencyKey ? { concurrencyKey } : {}),
|
|
35
|
+
};
|
|
36
|
+
const create = () => this.options.workflow.create({
|
|
37
|
+
id: crypto.randomUUID(),
|
|
38
|
+
params: payload,
|
|
39
|
+
});
|
|
40
|
+
const instance = this.options.coordinator && concurrencyKey
|
|
41
|
+
? await this.options.coordinator.enqueue(payload, create)
|
|
42
|
+
: await create();
|
|
43
|
+
return instance.id;
|
|
44
|
+
}
|
|
45
|
+
async run(payload, step) {
|
|
46
|
+
const setup = this.requireSetup();
|
|
47
|
+
const task = setup.tasks.get(payload.taskSlug);
|
|
48
|
+
if (!task)
|
|
49
|
+
throw new Error(`Unknown task slug: ${payload.taskSlug}`);
|
|
50
|
+
if (payload.delayMs && payload.delayMs > 0) {
|
|
51
|
+
await step.sleep("porulle-delay", payload.delayMs);
|
|
52
|
+
}
|
|
53
|
+
const backoff = task.retries?.backoff;
|
|
54
|
+
const execute = () => step.do(`porulle:${payload.taskSlug}`, {
|
|
55
|
+
retries: {
|
|
56
|
+
limit: payload.maxAttempts,
|
|
57
|
+
delay: backoff?.delay ?? 1_000,
|
|
58
|
+
backoff: backoff?.type === "exponential" ? "exponential" : "constant",
|
|
59
|
+
},
|
|
60
|
+
}, async ({ attempt }) => {
|
|
61
|
+
const result = await task.handler({
|
|
62
|
+
input: payload.input,
|
|
63
|
+
ctx: setup.context,
|
|
64
|
+
job: { attemptNumber: attempt, maxAttempts: payload.maxAttempts },
|
|
65
|
+
});
|
|
66
|
+
return result.output;
|
|
67
|
+
});
|
|
68
|
+
if (payload.exclusive && payload.concurrencyKey) {
|
|
69
|
+
if (!this.options.coordinator) {
|
|
70
|
+
throw new Error("Exclusive Cloudflare task requires a durable concurrency coordinator.");
|
|
71
|
+
}
|
|
72
|
+
return this.options.coordinator.run(payload.concurrencyKey, execute);
|
|
73
|
+
}
|
|
74
|
+
return execute();
|
|
75
|
+
}
|
|
76
|
+
requireSetup() {
|
|
77
|
+
if (!this.setup)
|
|
78
|
+
throw new Error("CloudflareExecutionEngine must be registered before use.");
|
|
79
|
+
return this.setup;
|
|
80
|
+
}
|
|
81
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@porulle/jobs-cloudflare",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"bun": "./src/index.ts",
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./src/index.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@porulle/core": "0.9.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^24.5.2",
|
|
18
|
+
"eslint": "^9.39.1",
|
|
19
|
+
"typescript": "5.9.2",
|
|
20
|
+
"vitest": "^3.2.4",
|
|
21
|
+
"@porulle/eslint-config": "0.1.0",
|
|
22
|
+
"@porulle/typescript-config": "0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"src",
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"description": "Cloudflare Workflows execution engine for Porulle background jobs.",
|
|
33
|
+
"homepage": "https://porulle-docs.vercel.app",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/asyncdotengineering/porulle/issues"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/asyncdotengineering/porulle.git",
|
|
40
|
+
"directory": "packages/jobs-cloudflare"
|
|
41
|
+
},
|
|
42
|
+
"author": "Porulle contributors",
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "rm -rf dist tsconfig.build.tsbuildinfo && tsc -p tsconfig.build.json",
|
|
45
|
+
"check-types": "tsc --noEmit",
|
|
46
|
+
"lint": "eslint . --max-warnings 1000",
|
|
47
|
+
"test": "vitest run"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EnqueueOptions,
|
|
3
|
+
ExecutionEngine,
|
|
4
|
+
ExecutionEngineSetup,
|
|
5
|
+
} from "@porulle/core";
|
|
6
|
+
|
|
7
|
+
export interface CloudflareJobPayload {
|
|
8
|
+
taskSlug: string;
|
|
9
|
+
input: Record<string, unknown>;
|
|
10
|
+
organizationId: string;
|
|
11
|
+
maxAttempts: number;
|
|
12
|
+
delayMs?: number;
|
|
13
|
+
concurrencyKey?: string;
|
|
14
|
+
exclusive: boolean;
|
|
15
|
+
supersedes: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface WorkflowBinding {
|
|
19
|
+
create(options: {
|
|
20
|
+
id?: string;
|
|
21
|
+
params: CloudflareJobPayload;
|
|
22
|
+
}): Promise<{ id: string }>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface WorkflowStep {
|
|
26
|
+
sleep(name: string, duration: number | string): Promise<void>;
|
|
27
|
+
do<T>(
|
|
28
|
+
name: string,
|
|
29
|
+
config: {
|
|
30
|
+
retries: {
|
|
31
|
+
limit: number;
|
|
32
|
+
delay: number;
|
|
33
|
+
backoff: "constant" | "exponential";
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
callback: (context: { attempt: number }) => Promise<T>,
|
|
37
|
+
): Promise<T>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface CloudflareConcurrencyCoordinator {
|
|
41
|
+
enqueue(
|
|
42
|
+
payload: CloudflareJobPayload,
|
|
43
|
+
create: () => Promise<{ id: string }>,
|
|
44
|
+
): Promise<{ id: string }>;
|
|
45
|
+
run<T>(key: string, handler: () => Promise<T>): Promise<T>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface CloudflareExecutionEngineOptions {
|
|
49
|
+
workflow: WorkflowBinding;
|
|
50
|
+
coordinator?: CloudflareConcurrencyCoordinator;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class CloudflareExecutionEngine implements ExecutionEngine {
|
|
54
|
+
readonly execution = { mode: "push" as const };
|
|
55
|
+
private setup: ExecutionEngineSetup | undefined;
|
|
56
|
+
|
|
57
|
+
constructor(private readonly options: CloudflareExecutionEngineOptions) {}
|
|
58
|
+
|
|
59
|
+
register(setup: ExecutionEngineSetup): void {
|
|
60
|
+
this.setup = setup;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async enqueue(
|
|
64
|
+
taskSlug: string,
|
|
65
|
+
input: Record<string, unknown>,
|
|
66
|
+
options: EnqueueOptions,
|
|
67
|
+
): Promise<string> {
|
|
68
|
+
const task = this.requireSetup().tasks.get(taskSlug);
|
|
69
|
+
if (!task) throw new Error(`Unknown task slug: ${taskSlug}`);
|
|
70
|
+
const organizationId = options.organizationId.trim();
|
|
71
|
+
if (!organizationId)
|
|
72
|
+
throw new Error("Jobs enqueue requires a non-empty organizationId.");
|
|
73
|
+
|
|
74
|
+
const concurrencyKey =
|
|
75
|
+
options.concurrencyKey ?? task.concurrency?.key(input);
|
|
76
|
+
const exclusive = Boolean(
|
|
77
|
+
task.concurrency && task.concurrency.exclusive !== false,
|
|
78
|
+
);
|
|
79
|
+
const supersedes = Boolean(
|
|
80
|
+
options.supersedes ?? task.concurrency?.supersedes,
|
|
81
|
+
);
|
|
82
|
+
if (
|
|
83
|
+
(exclusive || supersedes) &&
|
|
84
|
+
concurrencyKey &&
|
|
85
|
+
!this.options.coordinator
|
|
86
|
+
) {
|
|
87
|
+
throw new Error(
|
|
88
|
+
"Cloudflare Workflows has no per-key queue concurrency primitive. Configure a durable CloudflareConcurrencyCoordinator for exclusive or superseding tasks.",
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const payload: CloudflareJobPayload = {
|
|
93
|
+
taskSlug,
|
|
94
|
+
input,
|
|
95
|
+
organizationId,
|
|
96
|
+
maxAttempts: options.maxAttempts ?? task.retries?.attempts ?? 1,
|
|
97
|
+
exclusive,
|
|
98
|
+
supersedes,
|
|
99
|
+
...(options.delayMs !== undefined ? { delayMs: options.delayMs } : {}),
|
|
100
|
+
...(concurrencyKey ? { concurrencyKey } : {}),
|
|
101
|
+
};
|
|
102
|
+
const create = () =>
|
|
103
|
+
this.options.workflow.create({
|
|
104
|
+
id: crypto.randomUUID(),
|
|
105
|
+
params: payload,
|
|
106
|
+
});
|
|
107
|
+
const instance =
|
|
108
|
+
this.options.coordinator && concurrencyKey
|
|
109
|
+
? await this.options.coordinator.enqueue(payload, create)
|
|
110
|
+
: await create();
|
|
111
|
+
return instance.id;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async run(
|
|
115
|
+
payload: CloudflareJobPayload,
|
|
116
|
+
step: WorkflowStep,
|
|
117
|
+
): Promise<Record<string, unknown>> {
|
|
118
|
+
const setup = this.requireSetup();
|
|
119
|
+
const task = setup.tasks.get(payload.taskSlug);
|
|
120
|
+
if (!task) throw new Error(`Unknown task slug: ${payload.taskSlug}`);
|
|
121
|
+
|
|
122
|
+
if (payload.delayMs && payload.delayMs > 0) {
|
|
123
|
+
await step.sleep("porulle-delay", payload.delayMs);
|
|
124
|
+
}
|
|
125
|
+
const backoff = task.retries?.backoff;
|
|
126
|
+
const execute = () =>
|
|
127
|
+
step.do(
|
|
128
|
+
`porulle:${payload.taskSlug}`,
|
|
129
|
+
{
|
|
130
|
+
retries: {
|
|
131
|
+
limit: payload.maxAttempts,
|
|
132
|
+
delay: backoff?.delay ?? 1_000,
|
|
133
|
+
backoff:
|
|
134
|
+
backoff?.type === "exponential" ? "exponential" : "constant",
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
async ({ attempt }) => {
|
|
138
|
+
const result = await task.handler({
|
|
139
|
+
input: payload.input,
|
|
140
|
+
ctx: setup.context,
|
|
141
|
+
job: { attemptNumber: attempt, maxAttempts: payload.maxAttempts },
|
|
142
|
+
});
|
|
143
|
+
return result.output;
|
|
144
|
+
},
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
if (payload.exclusive && payload.concurrencyKey) {
|
|
148
|
+
if (!this.options.coordinator) {
|
|
149
|
+
throw new Error(
|
|
150
|
+
"Exclusive Cloudflare task requires a durable concurrency coordinator.",
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
return this.options.coordinator.run(payload.concurrencyKey, execute);
|
|
154
|
+
}
|
|
155
|
+
return execute();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private requireSetup(): ExecutionEngineSetup {
|
|
159
|
+
if (!this.setup)
|
|
160
|
+
throw new Error(
|
|
161
|
+
"CloudflareExecutionEngine must be registered before use.",
|
|
162
|
+
);
|
|
163
|
+
return this.setup;
|
|
164
|
+
}
|
|
165
|
+
}
|