@porulle/jobs-trigger 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 +12 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +99 -0
- package/package.json +50 -0
- package/src/index.ts +140 -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,12 @@
|
|
|
1
|
+
# `@porulle/jobs-trigger`
|
|
2
|
+
|
|
3
|
+
Push execution engine that translates Porulle tasks into Trigger.dev tasks and enqueues them with the Trigger.dev SDK.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { TriggerExecutionEngine } from "@porulle/jobs-trigger";
|
|
7
|
+
|
|
8
|
+
export const jobs = new TriggerExecutionEngine();
|
|
9
|
+
export default defineConfig({ jobs: { adapter: jobs } });
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Import the module that creates the Porulle kernel from the Trigger.dev task directory. Registration calls Trigger.dev's `task()` API for every configured task; the Trigger.dev runtime then owns execution and retries.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AnyTask } from "@trigger.dev/sdk";
|
|
2
|
+
import type { EnqueueOptions, ExecutionEngine, ExecutionEngineSetup } from "@porulle/core";
|
|
3
|
+
type DebounceDelay = "1s" | "2s" | "5s" | "10s" | "30s" | "1m";
|
|
4
|
+
export interface TriggerExecutionEngineOptions {
|
|
5
|
+
queuePrefix?: string;
|
|
6
|
+
supersedesDebounce?: DebounceDelay;
|
|
7
|
+
}
|
|
8
|
+
export declare class TriggerExecutionEngine implements ExecutionEngine {
|
|
9
|
+
readonly execution: {
|
|
10
|
+
mode: "push";
|
|
11
|
+
};
|
|
12
|
+
readonly tasks: AnyTask[];
|
|
13
|
+
private readonly queuePrefix;
|
|
14
|
+
private readonly supersedesDebounce;
|
|
15
|
+
private setup;
|
|
16
|
+
constructor(options?: TriggerExecutionEngineOptions);
|
|
17
|
+
register(setup: ExecutionEngineSetup): void;
|
|
18
|
+
enqueue(taskSlug: string, input: Record<string, unknown>, options: EnqueueOptions): Promise<string>;
|
|
19
|
+
private createTask;
|
|
20
|
+
private translateRetry;
|
|
21
|
+
private requireSetup;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { task as createTriggerTask, tasks as triggerTasks, } from "@trigger.dev/sdk";
|
|
2
|
+
export class TriggerExecutionEngine {
|
|
3
|
+
execution = { mode: "push" };
|
|
4
|
+
tasks = [];
|
|
5
|
+
queuePrefix;
|
|
6
|
+
supersedesDebounce;
|
|
7
|
+
setup;
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.queuePrefix = options.queuePrefix ?? "porulle";
|
|
10
|
+
this.supersedesDebounce = options.supersedesDebounce ?? "1s";
|
|
11
|
+
}
|
|
12
|
+
register(setup) {
|
|
13
|
+
this.setup = setup;
|
|
14
|
+
this.tasks.splice(0, this.tasks.length);
|
|
15
|
+
for (const definition of setup.tasks.values()) {
|
|
16
|
+
this.tasks.push(this.createTask(definition));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async enqueue(taskSlug, input, options) {
|
|
20
|
+
const definition = this.requireSetup().tasks.get(taskSlug);
|
|
21
|
+
if (!definition)
|
|
22
|
+
throw new Error(`Unknown task slug: ${taskSlug}`);
|
|
23
|
+
const organizationId = options.organizationId.trim();
|
|
24
|
+
if (!organizationId)
|
|
25
|
+
throw new Error("Jobs enqueue requires a non-empty organizationId.");
|
|
26
|
+
const concurrencyKey = options.concurrencyKey ?? definition.concurrency?.key(input);
|
|
27
|
+
const exclusive = Boolean(definition.concurrency && definition.concurrency.exclusive !== false);
|
|
28
|
+
const supersedes = options.supersedes ?? definition.concurrency?.supersedes;
|
|
29
|
+
const maxAttempts = options.maxAttempts ?? definition.retries?.attempts ?? 1;
|
|
30
|
+
const payload = {
|
|
31
|
+
input,
|
|
32
|
+
organizationId,
|
|
33
|
+
maxAttempts,
|
|
34
|
+
...(concurrencyKey ? { concurrencyKey } : {}),
|
|
35
|
+
};
|
|
36
|
+
const handle = await triggerTasks.trigger(taskSlug, payload, {
|
|
37
|
+
maxAttempts,
|
|
38
|
+
...(exclusive && concurrencyKey ? { concurrencyKey } : {}),
|
|
39
|
+
...(options.delayMs !== undefined
|
|
40
|
+
? { delay: new Date(Date.now() + options.delayMs) }
|
|
41
|
+
: {}),
|
|
42
|
+
...(supersedes && concurrencyKey
|
|
43
|
+
? {
|
|
44
|
+
debounce: {
|
|
45
|
+
key: concurrencyKey,
|
|
46
|
+
delay: this.supersedesDebounce,
|
|
47
|
+
mode: "trailing",
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
: {}),
|
|
51
|
+
});
|
|
52
|
+
return handle.id;
|
|
53
|
+
}
|
|
54
|
+
createTask(definition) {
|
|
55
|
+
const exclusive = Boolean(definition.concurrency && definition.concurrency.exclusive !== false);
|
|
56
|
+
const retry = this.translateRetry(definition);
|
|
57
|
+
return createTriggerTask({
|
|
58
|
+
id: definition.slug,
|
|
59
|
+
...(exclusive
|
|
60
|
+
? {
|
|
61
|
+
queue: {
|
|
62
|
+
name: `${this.queuePrefix}-${definition.slug.replaceAll("/", "-")}`,
|
|
63
|
+
concurrencyLimit: 1,
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
: {}),
|
|
67
|
+
...(retry ? { retry } : {}),
|
|
68
|
+
run: async (payload, { ctx }) => {
|
|
69
|
+
const result = await definition.handler({
|
|
70
|
+
input: payload.input,
|
|
71
|
+
ctx: this.requireSetup().context,
|
|
72
|
+
job: {
|
|
73
|
+
attemptNumber: ctx.attempt.number,
|
|
74
|
+
maxAttempts: payload.maxAttempts,
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
return result.output;
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
translateRetry(definition) {
|
|
82
|
+
const retries = definition.retries;
|
|
83
|
+
if (!retries)
|
|
84
|
+
return undefined;
|
|
85
|
+
const delay = retries.backoff?.delay ?? 1_000;
|
|
86
|
+
return {
|
|
87
|
+
maxAttempts: retries.attempts,
|
|
88
|
+
factor: retries.backoff?.type === "exponential" ? 2 : 1,
|
|
89
|
+
minTimeoutInMs: delay,
|
|
90
|
+
...(retries.backoff?.type === "fixed" ? { maxTimeoutInMs: delay } : {}),
|
|
91
|
+
randomize: false,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
requireSetup() {
|
|
95
|
+
if (!this.setup)
|
|
96
|
+
throw new Error("TriggerExecutionEngine must be registered before use.");
|
|
97
|
+
return this.setup;
|
|
98
|
+
}
|
|
99
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@porulle/jobs-trigger",
|
|
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
|
+
"@trigger.dev/sdk": "^4.5.4",
|
|
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": "Trigger.dev 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-trigger"
|
|
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,140 @@
|
|
|
1
|
+
import {
|
|
2
|
+
task as createTriggerTask,
|
|
3
|
+
tasks as triggerTasks,
|
|
4
|
+
} from "@trigger.dev/sdk";
|
|
5
|
+
import type { AnyTask } from "@trigger.dev/sdk";
|
|
6
|
+
import type {
|
|
7
|
+
EnqueueOptions,
|
|
8
|
+
ExecutionEngine,
|
|
9
|
+
ExecutionEngineSetup,
|
|
10
|
+
TaskDefinition,
|
|
11
|
+
} from "@porulle/core";
|
|
12
|
+
|
|
13
|
+
interface TriggerJobPayload {
|
|
14
|
+
input: Record<string, unknown>;
|
|
15
|
+
organizationId: string;
|
|
16
|
+
maxAttempts: number;
|
|
17
|
+
concurrencyKey?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type DebounceDelay = "1s" | "2s" | "5s" | "10s" | "30s" | "1m";
|
|
21
|
+
|
|
22
|
+
export interface TriggerExecutionEngineOptions {
|
|
23
|
+
queuePrefix?: string;
|
|
24
|
+
supersedesDebounce?: DebounceDelay;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class TriggerExecutionEngine implements ExecutionEngine {
|
|
28
|
+
readonly execution = { mode: "push" as const };
|
|
29
|
+
readonly tasks: AnyTask[] = [];
|
|
30
|
+
|
|
31
|
+
private readonly queuePrefix: string;
|
|
32
|
+
private readonly supersedesDebounce: DebounceDelay;
|
|
33
|
+
private setup: ExecutionEngineSetup | undefined;
|
|
34
|
+
|
|
35
|
+
constructor(options: TriggerExecutionEngineOptions = {}) {
|
|
36
|
+
this.queuePrefix = options.queuePrefix ?? "porulle";
|
|
37
|
+
this.supersedesDebounce = options.supersedesDebounce ?? "1s";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
register(setup: ExecutionEngineSetup): void {
|
|
41
|
+
this.setup = setup;
|
|
42
|
+
this.tasks.splice(0, this.tasks.length);
|
|
43
|
+
for (const definition of setup.tasks.values()) {
|
|
44
|
+
this.tasks.push(this.createTask(definition));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async enqueue(
|
|
49
|
+
taskSlug: string,
|
|
50
|
+
input: Record<string, unknown>,
|
|
51
|
+
options: EnqueueOptions,
|
|
52
|
+
): Promise<string> {
|
|
53
|
+
const definition = this.requireSetup().tasks.get(taskSlug);
|
|
54
|
+
if (!definition) throw new Error(`Unknown task slug: ${taskSlug}`);
|
|
55
|
+
const organizationId = options.organizationId.trim();
|
|
56
|
+
if (!organizationId)
|
|
57
|
+
throw new Error("Jobs enqueue requires a non-empty organizationId.");
|
|
58
|
+
|
|
59
|
+
const concurrencyKey =
|
|
60
|
+
options.concurrencyKey ?? definition.concurrency?.key(input);
|
|
61
|
+
const exclusive = Boolean(
|
|
62
|
+
definition.concurrency && definition.concurrency.exclusive !== false,
|
|
63
|
+
);
|
|
64
|
+
const supersedes = options.supersedes ?? definition.concurrency?.supersedes;
|
|
65
|
+
const maxAttempts =
|
|
66
|
+
options.maxAttempts ?? definition.retries?.attempts ?? 1;
|
|
67
|
+
const payload: TriggerJobPayload = {
|
|
68
|
+
input,
|
|
69
|
+
organizationId,
|
|
70
|
+
maxAttempts,
|
|
71
|
+
...(concurrencyKey ? { concurrencyKey } : {}),
|
|
72
|
+
};
|
|
73
|
+
const handle = await triggerTasks.trigger(taskSlug, payload, {
|
|
74
|
+
maxAttempts,
|
|
75
|
+
...(exclusive && concurrencyKey ? { concurrencyKey } : {}),
|
|
76
|
+
...(options.delayMs !== undefined
|
|
77
|
+
? { delay: new Date(Date.now() + options.delayMs) }
|
|
78
|
+
: {}),
|
|
79
|
+
...(supersedes && concurrencyKey
|
|
80
|
+
? {
|
|
81
|
+
debounce: {
|
|
82
|
+
key: concurrencyKey,
|
|
83
|
+
delay: this.supersedesDebounce,
|
|
84
|
+
mode: "trailing" as const,
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
: {}),
|
|
88
|
+
});
|
|
89
|
+
return handle.id;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private createTask(definition: TaskDefinition): AnyTask {
|
|
93
|
+
const exclusive = Boolean(
|
|
94
|
+
definition.concurrency && definition.concurrency.exclusive !== false,
|
|
95
|
+
);
|
|
96
|
+
const retry = this.translateRetry(definition);
|
|
97
|
+
return createTriggerTask({
|
|
98
|
+
id: definition.slug,
|
|
99
|
+
...(exclusive
|
|
100
|
+
? {
|
|
101
|
+
queue: {
|
|
102
|
+
name: `${this.queuePrefix}-${definition.slug.replaceAll("/", "-")}`,
|
|
103
|
+
concurrencyLimit: 1,
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
: {}),
|
|
107
|
+
...(retry ? { retry } : {}),
|
|
108
|
+
run: async (payload: TriggerJobPayload, { ctx }) => {
|
|
109
|
+
const result = await definition.handler({
|
|
110
|
+
input: payload.input,
|
|
111
|
+
ctx: this.requireSetup().context,
|
|
112
|
+
job: {
|
|
113
|
+
attemptNumber: ctx.attempt.number,
|
|
114
|
+
maxAttempts: payload.maxAttempts,
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
return result.output;
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private translateRetry(definition: TaskDefinition) {
|
|
123
|
+
const retries = definition.retries;
|
|
124
|
+
if (!retries) return undefined;
|
|
125
|
+
const delay = retries.backoff?.delay ?? 1_000;
|
|
126
|
+
return {
|
|
127
|
+
maxAttempts: retries.attempts,
|
|
128
|
+
factor: retries.backoff?.type === "exponential" ? 2 : 1,
|
|
129
|
+
minTimeoutInMs: delay,
|
|
130
|
+
...(retries.backoff?.type === "fixed" ? { maxTimeoutInMs: delay } : {}),
|
|
131
|
+
randomize: false,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private requireSetup(): ExecutionEngineSetup {
|
|
136
|
+
if (!this.setup)
|
|
137
|
+
throw new Error("TriggerExecutionEngine must be registered before use.");
|
|
138
|
+
return this.setup;
|
|
139
|
+
}
|
|
140
|
+
}
|