1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
import * as restate from "@restatedev/restate-sdk";
import type { RunnerFuncs, RunnerOptions } from "@karakeep/shared/queueing";
import { QueueRetryAfterError } from "@karakeep/shared/queueing";
import { tryCatch } from "@karakeep/shared/tryCatch";
import type { RunnerJobData, RunnerResult, SerializedError } from "./types";
function serializeError(error: Error): SerializedError {
return {
name: error.name,
message: error.message,
stack: error.stack,
};
}
export function runnerServiceName(queueName: string): string {
return `${queueName}-runner`;
}
export function buildRunnerService<T, R>(
queueName: string,
funcs: RunnerFuncs<T, R>,
opts: RunnerOptions<T>,
) {
return restate.service({
name: runnerServiceName(queueName),
options: {
ingressPrivate: true,
inactivityTimeout: {
seconds: opts.timeoutSecs * 2,
},
// No retries at runner level - dispatcher handles retry logic
retryPolicy: {
maxAttempts: 1,
},
journalRetention: {
days: 3,
},
},
handlers: {
run: async (
ctx: restate.Context,
jobData: RunnerJobData<T>,
): Promise<RunnerResult<R>> => {
// Validate payload if validator provided
let payload = jobData.data;
if (opts.validator) {
const res = opts.validator.safeParse(jobData.data);
if (!res.success) {
return {
type: "error",
error: {
name: "ValidationError",
message: res.error.message,
},
};
}
payload = res.data;
}
const res = await tryCatch(
ctx
.run(
"main logic",
async () => {
const result = await tryCatch(
funcs.run({
id: jobData.id,
data: payload,
priority: jobData.priority,
runNumber: jobData.runNumber,
abortSignal: AbortSignal.timeout(
jobData.timeoutSecs * 1000,
),
}),
);
if (result.error) {
if (result.error instanceof QueueRetryAfterError) {
return {
type: "rate_limit" as const,
delayMs: result.error.delayMs,
};
}
throw result.error;
}
return { type: "success" as const, value: result.data };
},
{
maxRetryAttempts: 1,
},
)
.orTimeout({
seconds: jobData.timeoutSecs * 1.1,
}),
);
if (res.error) {
return {
type: "error",
error: serializeError(res.error),
};
}
return res.data as RunnerResult<R>;
},
onCompleted: async (
ctx: restate.Context,
data: { job: RunnerJobData<T>; result: R },
): Promise<void> => {
await ctx.run(
"onComplete",
async () => {
await funcs.onComplete?.(
{
id: data.job.id,
data: data.job.data,
priority: data.job.priority,
runNumber: data.job.runNumber,
abortSignal: AbortSignal.timeout(data.job.timeoutSecs * 1000),
},
data.result,
);
},
{
maxRetryAttempts: 1,
},
);
},
onError: async (
ctx: restate.Context,
data: { job: RunnerJobData<T>; error: SerializedError },
): Promise<void> => {
// Reconstruct the error
const reconstructedError = Object.assign(
new Error(data.error.message),
{
name: data.error.name,
stack: data.error.stack,
},
);
await ctx.run(
"onError",
async () => {
await funcs.onError?.({
id: data.job.id,
data: data.job.data,
priority: data.job.priority,
runNumber: data.job.runNumber,
numRetriesLeft: data.job.numRetriesLeft,
error: reconstructedError,
});
},
{
maxRetryAttempts: 1,
},
);
},
},
});
}
|