@porulle/jobs-inngest 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 +20 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +100 -0
- package/package.json +50 -0
- package/src/index.ts +151 -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,20 @@
|
|
|
1
|
+
# `@porulle/jobs-inngest`
|
|
2
|
+
|
|
3
|
+
Push execution engine that translates Porulle tasks into Inngest functions. Pass `engine.functions` to your Inngest `serve()` adapter; Inngest owns delivery and retries.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { Inngest } from "inngest";
|
|
7
|
+
import { serve } from "inngest/hono";
|
|
8
|
+
import { InngestExecutionEngine } from "@porulle/jobs-inngest";
|
|
9
|
+
|
|
10
|
+
const inngest = new Inngest({ id: "store" });
|
|
11
|
+
export const jobs = new InngestExecutionEngine({ client: inngest });
|
|
12
|
+
|
|
13
|
+
export default defineConfig({ jobs: { adapter: jobs } });
|
|
14
|
+
export const inngestHandler = serve({
|
|
15
|
+
client: inngest,
|
|
16
|
+
functions: jobs.functions,
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Create the Porulle kernel before serving so task registration has populated `jobs.functions`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Inngest } from "inngest";
|
|
2
|
+
import type { EnqueueOptions, ExecutionEngine, ExecutionEngineSetup } from "@porulle/core";
|
|
3
|
+
type InngestFunction = ReturnType<Inngest["createFunction"]>;
|
|
4
|
+
type DebouncePeriod = "1s" | "2s" | "5s" | "10s" | "30s" | "1m";
|
|
5
|
+
export interface InngestExecutionEngineOptions {
|
|
6
|
+
client?: Inngest;
|
|
7
|
+
appId?: string;
|
|
8
|
+
eventPrefix?: string;
|
|
9
|
+
supersedesDebounce?: DebouncePeriod;
|
|
10
|
+
}
|
|
11
|
+
export declare class InngestExecutionEngine implements ExecutionEngine {
|
|
12
|
+
readonly execution: {
|
|
13
|
+
mode: "push";
|
|
14
|
+
};
|
|
15
|
+
readonly functions: InngestFunction[];
|
|
16
|
+
private readonly client;
|
|
17
|
+
private readonly eventPrefix;
|
|
18
|
+
private readonly supersedesDebounce;
|
|
19
|
+
private setup;
|
|
20
|
+
constructor(options?: InngestExecutionEngineOptions);
|
|
21
|
+
register(setup: ExecutionEngineSetup): void;
|
|
22
|
+
enqueue(taskSlug: string, input: Record<string, unknown>, options: EnqueueOptions): Promise<string>;
|
|
23
|
+
private createFunction;
|
|
24
|
+
private eventName;
|
|
25
|
+
private requireSetup;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Inngest } from "inngest";
|
|
2
|
+
const INNGEST_RETRY_VALUES = [
|
|
3
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
|
4
|
+
];
|
|
5
|
+
export class InngestExecutionEngine {
|
|
6
|
+
execution = { mode: "push" };
|
|
7
|
+
functions = [];
|
|
8
|
+
client;
|
|
9
|
+
eventPrefix;
|
|
10
|
+
supersedesDebounce;
|
|
11
|
+
setup;
|
|
12
|
+
constructor(options = {}) {
|
|
13
|
+
this.client =
|
|
14
|
+
options.client ?? new Inngest({ id: options.appId ?? "porulle" });
|
|
15
|
+
this.eventPrefix = options.eventPrefix ?? "porulle/job";
|
|
16
|
+
this.supersedesDebounce = options.supersedesDebounce ?? "1s";
|
|
17
|
+
}
|
|
18
|
+
register(setup) {
|
|
19
|
+
this.setup = setup;
|
|
20
|
+
this.functions.splice(0, this.functions.length);
|
|
21
|
+
for (const task of setup.tasks.values()) {
|
|
22
|
+
this.functions.push(this.createFunction(task));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async enqueue(taskSlug, input, options) {
|
|
26
|
+
const task = this.requireSetup().tasks.get(taskSlug);
|
|
27
|
+
if (!task)
|
|
28
|
+
throw new Error(`Unknown task slug: ${taskSlug}`);
|
|
29
|
+
const organizationId = options.organizationId.trim();
|
|
30
|
+
if (!organizationId)
|
|
31
|
+
throw new Error("Jobs enqueue requires a non-empty organizationId.");
|
|
32
|
+
const taskMaxAttempts = task.retries?.attempts ?? 1;
|
|
33
|
+
if (options.maxAttempts !== undefined &&
|
|
34
|
+
options.maxAttempts !== taskMaxAttempts) {
|
|
35
|
+
throw new Error("Inngest retry counts are fixed when functions are registered; configure retries on TaskDefinition instead of enqueue().");
|
|
36
|
+
}
|
|
37
|
+
const taskSupersedes = Boolean(task.concurrency?.supersedes);
|
|
38
|
+
if (options.supersedes !== undefined &&
|
|
39
|
+
options.supersedes !== taskSupersedes) {
|
|
40
|
+
throw new Error("Inngest debounce is fixed when functions are registered; configure supersedes on TaskDefinition instead of enqueue().");
|
|
41
|
+
}
|
|
42
|
+
const concurrencyKey = options.concurrencyKey ?? task.concurrency?.key(input);
|
|
43
|
+
const data = {
|
|
44
|
+
input,
|
|
45
|
+
organizationId,
|
|
46
|
+
maxAttempts: taskMaxAttempts,
|
|
47
|
+
...(concurrencyKey ? { concurrencyKey } : {}),
|
|
48
|
+
};
|
|
49
|
+
const result = await this.client.send({
|
|
50
|
+
name: this.eventName(taskSlug),
|
|
51
|
+
data,
|
|
52
|
+
...(options.delayMs !== undefined
|
|
53
|
+
? { ts: Date.now() + options.delayMs }
|
|
54
|
+
: {}),
|
|
55
|
+
});
|
|
56
|
+
const id = result.ids[0];
|
|
57
|
+
if (!id)
|
|
58
|
+
throw new Error("Inngest did not return an event id.");
|
|
59
|
+
return id;
|
|
60
|
+
}
|
|
61
|
+
createFunction(task) {
|
|
62
|
+
const exclusive = Boolean(task.concurrency && task.concurrency.exclusive !== false);
|
|
63
|
+
const retries = INNGEST_RETRY_VALUES[Math.min(Math.max((task.retries?.attempts ?? 1) - 1, 0), 20)];
|
|
64
|
+
return this.client.createFunction({
|
|
65
|
+
id: `porulle:${task.slug}`,
|
|
66
|
+
triggers: [{ event: this.eventName(task.slug) }],
|
|
67
|
+
retries,
|
|
68
|
+
...(exclusive
|
|
69
|
+
? { concurrency: { limit: 1, key: "event.data.concurrencyKey" } }
|
|
70
|
+
: {}),
|
|
71
|
+
...(task.concurrency?.supersedes
|
|
72
|
+
? {
|
|
73
|
+
debounce: {
|
|
74
|
+
key: "event.data.concurrencyKey",
|
|
75
|
+
period: this.supersedesDebounce,
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
: {}),
|
|
79
|
+
}, async ({ event, attempt }) => {
|
|
80
|
+
const data = event.data;
|
|
81
|
+
const result = await task.handler({
|
|
82
|
+
input: data.input,
|
|
83
|
+
ctx: this.requireSetup().context,
|
|
84
|
+
job: {
|
|
85
|
+
attemptNumber: attempt + 1,
|
|
86
|
+
maxAttempts: data.maxAttempts,
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
return result.output;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
eventName(taskSlug) {
|
|
93
|
+
return `${this.eventPrefix}/${taskSlug}`;
|
|
94
|
+
}
|
|
95
|
+
requireSetup() {
|
|
96
|
+
if (!this.setup)
|
|
97
|
+
throw new Error("InngestExecutionEngine must be registered before use.");
|
|
98
|
+
return this.setup;
|
|
99
|
+
}
|
|
100
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@porulle/jobs-inngest",
|
|
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
|
+
"inngest": "^4.13.0",
|
|
15
|
+
"@porulle/core": "0.9.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^24.5.2",
|
|
19
|
+
"eslint": "^9.39.1",
|
|
20
|
+
"typescript": "5.9.2",
|
|
21
|
+
"vitest": "^3.2.4",
|
|
22
|
+
"@porulle/eslint-config": "0.1.0",
|
|
23
|
+
"@porulle/typescript-config": "0.1.0"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"src",
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"description": "Inngest execution engine for Porulle background jobs.",
|
|
34
|
+
"homepage": "https://porulle-docs.vercel.app",
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/asyncdotengineering/porulle/issues"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/asyncdotengineering/porulle.git",
|
|
41
|
+
"directory": "packages/jobs-inngest"
|
|
42
|
+
},
|
|
43
|
+
"author": "Porulle contributors",
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "rm -rf dist tsconfig.build.tsbuildinfo && tsc -p tsconfig.build.json",
|
|
46
|
+
"check-types": "tsc --noEmit",
|
|
47
|
+
"lint": "eslint . --max-warnings 1000",
|
|
48
|
+
"test": "vitest run"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Inngest } from "inngest";
|
|
2
|
+
import type {
|
|
3
|
+
EnqueueOptions,
|
|
4
|
+
ExecutionEngine,
|
|
5
|
+
ExecutionEngineSetup,
|
|
6
|
+
TaskDefinition,
|
|
7
|
+
} from "@porulle/core";
|
|
8
|
+
|
|
9
|
+
interface InngestJobData extends Record<string, unknown> {
|
|
10
|
+
input: Record<string, unknown>;
|
|
11
|
+
organizationId: string;
|
|
12
|
+
maxAttempts: number;
|
|
13
|
+
concurrencyKey?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type InngestFunction = ReturnType<Inngest["createFunction"]>;
|
|
17
|
+
type DebouncePeriod = "1s" | "2s" | "5s" | "10s" | "30s" | "1m";
|
|
18
|
+
const INNGEST_RETRY_VALUES = [
|
|
19
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
|
20
|
+
] as const;
|
|
21
|
+
|
|
22
|
+
export interface InngestExecutionEngineOptions {
|
|
23
|
+
client?: Inngest;
|
|
24
|
+
appId?: string;
|
|
25
|
+
eventPrefix?: string;
|
|
26
|
+
supersedesDebounce?: DebouncePeriod;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class InngestExecutionEngine implements ExecutionEngine {
|
|
30
|
+
readonly execution = { mode: "push" as const };
|
|
31
|
+
readonly functions: InngestFunction[] = [];
|
|
32
|
+
|
|
33
|
+
private readonly client: Inngest;
|
|
34
|
+
private readonly eventPrefix: string;
|
|
35
|
+
private readonly supersedesDebounce: DebouncePeriod;
|
|
36
|
+
private setup: ExecutionEngineSetup | undefined;
|
|
37
|
+
|
|
38
|
+
constructor(options: InngestExecutionEngineOptions = {}) {
|
|
39
|
+
this.client =
|
|
40
|
+
options.client ?? new Inngest({ id: options.appId ?? "porulle" });
|
|
41
|
+
this.eventPrefix = options.eventPrefix ?? "porulle/job";
|
|
42
|
+
this.supersedesDebounce = options.supersedesDebounce ?? "1s";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
register(setup: ExecutionEngineSetup): void {
|
|
46
|
+
this.setup = setup;
|
|
47
|
+
this.functions.splice(0, this.functions.length);
|
|
48
|
+
for (const task of setup.tasks.values()) {
|
|
49
|
+
this.functions.push(this.createFunction(task));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async enqueue(
|
|
54
|
+
taskSlug: string,
|
|
55
|
+
input: Record<string, unknown>,
|
|
56
|
+
options: EnqueueOptions,
|
|
57
|
+
): Promise<string> {
|
|
58
|
+
const task = this.requireSetup().tasks.get(taskSlug);
|
|
59
|
+
if (!task) throw new Error(`Unknown task slug: ${taskSlug}`);
|
|
60
|
+
const organizationId = options.organizationId.trim();
|
|
61
|
+
if (!organizationId)
|
|
62
|
+
throw new Error("Jobs enqueue requires a non-empty organizationId.");
|
|
63
|
+
const taskMaxAttempts = task.retries?.attempts ?? 1;
|
|
64
|
+
if (
|
|
65
|
+
options.maxAttempts !== undefined &&
|
|
66
|
+
options.maxAttempts !== taskMaxAttempts
|
|
67
|
+
) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
"Inngest retry counts are fixed when functions are registered; configure retries on TaskDefinition instead of enqueue().",
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
const taskSupersedes = Boolean(task.concurrency?.supersedes);
|
|
73
|
+
if (
|
|
74
|
+
options.supersedes !== undefined &&
|
|
75
|
+
options.supersedes !== taskSupersedes
|
|
76
|
+
) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
"Inngest debounce is fixed when functions are registered; configure supersedes on TaskDefinition instead of enqueue().",
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const concurrencyKey =
|
|
83
|
+
options.concurrencyKey ?? task.concurrency?.key(input);
|
|
84
|
+
const data: InngestJobData = {
|
|
85
|
+
input,
|
|
86
|
+
organizationId,
|
|
87
|
+
maxAttempts: taskMaxAttempts,
|
|
88
|
+
...(concurrencyKey ? { concurrencyKey } : {}),
|
|
89
|
+
};
|
|
90
|
+
const result = await this.client.send({
|
|
91
|
+
name: this.eventName(taskSlug),
|
|
92
|
+
data,
|
|
93
|
+
...(options.delayMs !== undefined
|
|
94
|
+
? { ts: Date.now() + options.delayMs }
|
|
95
|
+
: {}),
|
|
96
|
+
});
|
|
97
|
+
const id = result.ids[0];
|
|
98
|
+
if (!id) throw new Error("Inngest did not return an event id.");
|
|
99
|
+
return id;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private createFunction(task: TaskDefinition): InngestFunction {
|
|
103
|
+
const exclusive = Boolean(
|
|
104
|
+
task.concurrency && task.concurrency.exclusive !== false,
|
|
105
|
+
);
|
|
106
|
+
const retries =
|
|
107
|
+
INNGEST_RETRY_VALUES[
|
|
108
|
+
Math.min(Math.max((task.retries?.attempts ?? 1) - 1, 0), 20)
|
|
109
|
+
]!;
|
|
110
|
+
return this.client.createFunction(
|
|
111
|
+
{
|
|
112
|
+
id: `porulle:${task.slug}`,
|
|
113
|
+
triggers: [{ event: this.eventName(task.slug) }],
|
|
114
|
+
retries,
|
|
115
|
+
...(exclusive
|
|
116
|
+
? { concurrency: { limit: 1, key: "event.data.concurrencyKey" } }
|
|
117
|
+
: {}),
|
|
118
|
+
...(task.concurrency?.supersedes
|
|
119
|
+
? {
|
|
120
|
+
debounce: {
|
|
121
|
+
key: "event.data.concurrencyKey",
|
|
122
|
+
period: this.supersedesDebounce,
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
: {}),
|
|
126
|
+
},
|
|
127
|
+
async ({ event, attempt }) => {
|
|
128
|
+
const data = event.data as InngestJobData;
|
|
129
|
+
const result = await task.handler({
|
|
130
|
+
input: data.input,
|
|
131
|
+
ctx: this.requireSetup().context,
|
|
132
|
+
job: {
|
|
133
|
+
attemptNumber: attempt + 1,
|
|
134
|
+
maxAttempts: data.maxAttempts,
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
return result.output;
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private eventName(taskSlug: string): string {
|
|
143
|
+
return `${this.eventPrefix}/${taskSlug}`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private requireSetup(): ExecutionEngineSetup {
|
|
147
|
+
if (!this.setup)
|
|
148
|
+
throw new Error("InngestExecutionEngine must be registered before use.");
|
|
149
|
+
return this.setup;
|
|
150
|
+
}
|
|
151
|
+
}
|