@trigger.dev/sdk 3.0.0-beta.3 → 3.0.0-beta.30
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/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/v3/index.d.mts +245 -74
- package/dist/v3/index.d.ts +245 -74
- package/dist/v3/index.js +682 -477
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/index.mjs +644 -479
- package/dist/v3/index.mjs.map +1 -1
- package/package.json +5 -12
package/dist/v3/index.js
CHANGED
|
@@ -1,524 +1,147 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var v3 = require('@trigger.dev/core/v3');
|
|
3
4
|
var api = require('@opentelemetry/api');
|
|
4
5
|
var semanticConventions = require('@opentelemetry/semantic-conventions');
|
|
5
|
-
var v3 = require('@trigger.dev/core/v3');
|
|
6
6
|
var node_async_hooks = require('node:async_hooks');
|
|
7
7
|
|
|
8
8
|
var __defProp = Object.defineProperty;
|
|
9
9
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
10
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
11
15
|
var __publicField = (obj, key, value) => {
|
|
12
16
|
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
13
17
|
return value;
|
|
14
18
|
};
|
|
15
19
|
|
|
16
20
|
// package.json
|
|
17
|
-
var version = "3.0.0-beta.
|
|
21
|
+
var version = "3.0.0-beta.30";
|
|
22
|
+
|
|
23
|
+
// src/v3/tracer.ts
|
|
18
24
|
var tracer = new v3.TriggerTracer({
|
|
19
25
|
name: "@trigger.dev/sdk",
|
|
20
26
|
version
|
|
21
27
|
});
|
|
22
28
|
|
|
23
|
-
// src/v3/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
// src/v3/cache.ts
|
|
30
|
+
var _InMemoryCache = class _InMemoryCache {
|
|
31
|
+
constructor() {
|
|
32
|
+
__publicField(this, "_cache", /* @__PURE__ */ new Map());
|
|
33
|
+
}
|
|
34
|
+
get(key) {
|
|
35
|
+
return this._cache.get(key);
|
|
36
|
+
}
|
|
37
|
+
set(key, value) {
|
|
38
|
+
this._cache.set(key, value);
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
delete(key) {
|
|
42
|
+
this._cache.delete(key);
|
|
43
|
+
return void 0;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
__name(_InMemoryCache, "InMemoryCache");
|
|
47
|
+
var InMemoryCache = _InMemoryCache;
|
|
48
|
+
function createCache(store) {
|
|
49
|
+
return /* @__PURE__ */ __name(function cache(cacheKey, fn) {
|
|
50
|
+
return tracer.startActiveSpan("cache", async (span) => {
|
|
51
|
+
span.setAttribute("cache.key", cacheKey);
|
|
52
|
+
span.setAttribute(v3.SemanticInternalAttributes.STYLE_ICON, "device-sd-card");
|
|
53
|
+
const cacheEntry = await store.get(cacheKey);
|
|
54
|
+
if (cacheEntry) {
|
|
55
|
+
span.updateName(`cache.hit ${cacheKey}`);
|
|
56
|
+
return cacheEntry.value;
|
|
30
57
|
}
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
payload,
|
|
35
|
-
options: {
|
|
36
|
-
queue: params.queue,
|
|
37
|
-
concurrencyKey: options?.concurrencyKey,
|
|
38
|
-
test: v3.taskContextManager.ctx?.run.isTest
|
|
39
|
-
}
|
|
40
|
-
}, {
|
|
41
|
-
spanParentAsLink: true
|
|
42
|
-
});
|
|
43
|
-
if (!response.ok) {
|
|
44
|
-
throw new Error(response.error);
|
|
45
|
-
}
|
|
46
|
-
span.setAttribute("messaging.message.id", response.data.id);
|
|
47
|
-
return response.data;
|
|
58
|
+
span.updateName(`cache.miss ${cacheKey}`);
|
|
59
|
+
const value = await tracer.startActiveSpan("cache.getFreshValue", async (span2) => {
|
|
60
|
+
return await fn();
|
|
48
61
|
}, {
|
|
49
|
-
kind: api.SpanKind.PRODUCER,
|
|
50
62
|
attributes: {
|
|
51
|
-
|
|
52
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "
|
|
53
|
-
["messaging.client_id"]: v3.taskContextManager.worker?.id,
|
|
54
|
-
[semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
55
|
-
["messaging.message.body.size"]: JSON.stringify(payload).length,
|
|
56
|
-
[semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
57
|
-
...taskMetadata ? v3.accessoryAttributes({
|
|
58
|
-
items: [
|
|
59
|
-
{
|
|
60
|
-
text: `${taskMetadata.exportName}.trigger()`,
|
|
61
|
-
variant: "normal"
|
|
62
|
-
}
|
|
63
|
-
],
|
|
64
|
-
style: "codepath"
|
|
65
|
-
}) : {}
|
|
63
|
+
"cache.key": cacheKey,
|
|
64
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "device-sd-card"
|
|
66
65
|
}
|
|
67
66
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
const taskMetadata = v3.runtime.getTaskMetadata(params.id);
|
|
76
|
-
const response = await tracer.startActiveSpan(taskMetadata ? "Batch trigger" : `${params.id} batchTrigger()`, async (span) => {
|
|
77
|
-
const response2 = await apiClient.batchTriggerTask(params.id, {
|
|
78
|
-
items: items.map((item) => ({
|
|
79
|
-
payload: item.payload,
|
|
80
|
-
options: {
|
|
81
|
-
queue: item.options?.queue ?? params.queue,
|
|
82
|
-
concurrencyKey: item.options?.concurrencyKey,
|
|
83
|
-
test: v3.taskContextManager.ctx?.run.isTest
|
|
84
|
-
}
|
|
85
|
-
}))
|
|
86
|
-
}, {
|
|
87
|
-
spanParentAsLink: true
|
|
67
|
+
await tracer.startActiveSpan("cache.set", async (span2) => {
|
|
68
|
+
await store.set(cacheKey, {
|
|
69
|
+
value,
|
|
70
|
+
metadata: {
|
|
71
|
+
createdTime: Date.now()
|
|
72
|
+
}
|
|
88
73
|
});
|
|
89
|
-
if (!response2.ok) {
|
|
90
|
-
throw new Error(response2.error);
|
|
91
|
-
}
|
|
92
|
-
span.setAttribute("messaging.message.id", response2.data.batchId);
|
|
93
|
-
return response2.data;
|
|
94
74
|
}, {
|
|
95
|
-
kind: api.SpanKind.PRODUCER,
|
|
96
75
|
attributes: {
|
|
97
|
-
|
|
98
|
-
[
|
|
99
|
-
["messaging.client_id"]: v3.taskContextManager.worker?.id,
|
|
100
|
-
[semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
101
|
-
["messaging.message.body.size"]: items.map((item) => JSON.stringify(item.payload)).join("").length,
|
|
102
|
-
[semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
103
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
|
|
104
|
-
...taskMetadata ? v3.accessoryAttributes({
|
|
105
|
-
items: [
|
|
106
|
-
{
|
|
107
|
-
text: `${taskMetadata.exportName}.batchTrigger()`,
|
|
108
|
-
variant: "normal"
|
|
109
|
-
}
|
|
110
|
-
],
|
|
111
|
-
style: "codepath"
|
|
112
|
-
}) : {}
|
|
76
|
+
"cache.key": cacheKey,
|
|
77
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "device-sd-card"
|
|
113
78
|
}
|
|
114
79
|
});
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
payload,
|
|
130
|
-
options: {
|
|
131
|
-
dependentAttempt: ctx.attempt.id,
|
|
132
|
-
lockToVersion: v3.taskContextManager.worker?.version,
|
|
133
|
-
queue: params.queue,
|
|
134
|
-
concurrencyKey: options?.concurrencyKey,
|
|
135
|
-
test: v3.taskContextManager.ctx?.run.isTest
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
if (!response.ok) {
|
|
139
|
-
throw new Error(response.error);
|
|
140
|
-
}
|
|
141
|
-
span.setAttribute("messaging.message.id", response.data.id);
|
|
142
|
-
const result = await v3.runtime.waitForTask({
|
|
143
|
-
id: response.data.id,
|
|
144
|
-
ctx
|
|
145
|
-
});
|
|
146
|
-
const runResult = await handleTaskRunExecutionResult(result);
|
|
147
|
-
if (!runResult.ok) {
|
|
148
|
-
throw runResult.error;
|
|
149
|
-
}
|
|
150
|
-
return runResult.output;
|
|
151
|
-
}, {
|
|
152
|
-
kind: api.SpanKind.PRODUCER,
|
|
80
|
+
return value;
|
|
81
|
+
});
|
|
82
|
+
}, "cache");
|
|
83
|
+
}
|
|
84
|
+
__name(createCache, "createCache");
|
|
85
|
+
function onThrow(fn, options) {
|
|
86
|
+
const opts = {
|
|
87
|
+
...v3.defaultRetryOptions,
|
|
88
|
+
...options
|
|
89
|
+
};
|
|
90
|
+
return tracer.startActiveSpan(`retry.onThrow()`, async (span) => {
|
|
91
|
+
let attempt = 1;
|
|
92
|
+
while (attempt <= opts.maxAttempts) {
|
|
93
|
+
const innerSpan = tracer.startSpan("retry.fn()", {
|
|
153
94
|
attributes: {
|
|
154
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "
|
|
155
|
-
|
|
156
|
-
["messaging.client_id"]: v3.taskContextManager.worker?.id,
|
|
157
|
-
[semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
158
|
-
[semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
159
|
-
...taskMetadata ? v3.accessoryAttributes({
|
|
95
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "function",
|
|
96
|
+
...v3.accessoryAttributes({
|
|
160
97
|
items: [
|
|
161
98
|
{
|
|
162
|
-
text: `${
|
|
99
|
+
text: `${attempt}/${opts.maxAttempts}`,
|
|
163
100
|
variant: "normal"
|
|
164
101
|
}
|
|
165
102
|
],
|
|
166
103
|
style: "codepath"
|
|
167
|
-
})
|
|
104
|
+
})
|
|
168
105
|
}
|
|
169
106
|
});
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
if (!apiClient) {
|
|
178
|
-
throw apiClientMissingError();
|
|
179
|
-
}
|
|
180
|
-
const taskMetadata = v3.runtime.getTaskMetadata(params.id);
|
|
181
|
-
return await tracer.startActiveSpan(taskMetadata ? "Batch trigger" : `${params.id} batchTriggerAndWait()`, async (span) => {
|
|
182
|
-
const response = await apiClient.batchTriggerTask(params.id, {
|
|
183
|
-
items: items.map((item) => ({
|
|
184
|
-
payload: item.payload,
|
|
185
|
-
options: {
|
|
186
|
-
lockToVersion: v3.taskContextManager.worker?.version,
|
|
187
|
-
queue: item.options?.queue ?? params.queue,
|
|
188
|
-
concurrencyKey: item.options?.concurrencyKey,
|
|
189
|
-
test: v3.taskContextManager.ctx?.run.isTest
|
|
190
|
-
}
|
|
191
|
-
})),
|
|
192
|
-
dependentAttempt: ctx.attempt.id
|
|
107
|
+
const contextWithSpanSet = api.trace.setSpan(api.context.active(), innerSpan);
|
|
108
|
+
try {
|
|
109
|
+
const result = await api.context.with(contextWithSpanSet, async () => {
|
|
110
|
+
return fn({
|
|
111
|
+
attempt,
|
|
112
|
+
maxAttempts: opts.maxAttempts
|
|
113
|
+
});
|
|
193
114
|
});
|
|
194
|
-
|
|
195
|
-
|
|
115
|
+
innerSpan.end();
|
|
116
|
+
return result;
|
|
117
|
+
} catch (e) {
|
|
118
|
+
if (e instanceof Error || typeof e === "string") {
|
|
119
|
+
innerSpan.recordException(e);
|
|
120
|
+
} else {
|
|
121
|
+
innerSpan.recordException(String(e));
|
|
196
122
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
id: response.data.batchId,
|
|
200
|
-
runs: response.data.runs,
|
|
201
|
-
ctx
|
|
123
|
+
innerSpan.setStatus({
|
|
124
|
+
code: api.SpanStatusCode.ERROR
|
|
202
125
|
});
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
};
|
|
208
|
-
}, {
|
|
209
|
-
kind: api.SpanKind.PRODUCER,
|
|
210
|
-
attributes: {
|
|
211
|
-
[semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
212
|
-
["messaging.batch.message_count"]: items.length,
|
|
213
|
-
["messaging.client_id"]: v3.taskContextManager.worker?.id,
|
|
214
|
-
[semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
215
|
-
["messaging.message.body.size"]: items.map((item) => JSON.stringify(item.payload)).join("").length,
|
|
216
|
-
[semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
217
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
|
|
218
|
-
...taskMetadata ? v3.accessoryAttributes({
|
|
219
|
-
items: [
|
|
220
|
-
{
|
|
221
|
-
text: `${taskMetadata.exportName}.batchTriggerAndWait()`,
|
|
222
|
-
variant: "normal"
|
|
223
|
-
}
|
|
224
|
-
],
|
|
225
|
-
style: "codepath"
|
|
226
|
-
}) : {}
|
|
126
|
+
const nextRetryDelay = v3.calculateNextRetryDelay(opts, attempt);
|
|
127
|
+
if (!nextRetryDelay) {
|
|
128
|
+
innerSpan.end();
|
|
129
|
+
throw e;
|
|
227
130
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
queue: params.queue,
|
|
236
|
-
retry: params.retry ? {
|
|
237
|
-
...v3.defaultRetryOptions,
|
|
238
|
-
...params.retry
|
|
239
|
-
} : void 0,
|
|
240
|
-
machine: params.machine,
|
|
241
|
-
fns: {
|
|
242
|
-
run: params.run,
|
|
243
|
-
init: params.init,
|
|
244
|
-
cleanup: params.cleanup,
|
|
245
|
-
middleware: params.middleware,
|
|
246
|
-
handleError: params.handleError
|
|
131
|
+
innerSpan.setAttribute(v3.SemanticInternalAttributes.RETRY_AT, new Date(Date.now() + nextRetryDelay).toISOString());
|
|
132
|
+
innerSpan.setAttribute(v3.SemanticInternalAttributes.RETRY_COUNT, attempt);
|
|
133
|
+
innerSpan.setAttribute(v3.SemanticInternalAttributes.RETRY_DELAY, `${nextRetryDelay}ms`);
|
|
134
|
+
innerSpan.end();
|
|
135
|
+
await v3.runtime.waitForDuration(nextRetryDelay);
|
|
136
|
+
} finally {
|
|
137
|
+
attempt++;
|
|
247
138
|
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
});
|
|
251
|
-
return task2;
|
|
252
|
-
}
|
|
253
|
-
__name(createTask, "createTask");
|
|
254
|
-
async function handleBatchTaskRunExecutionResult(items) {
|
|
255
|
-
const someObjectStoreOutputs = items.some((item) => item.ok && item.outputType === "application/store");
|
|
256
|
-
if (!someObjectStoreOutputs) {
|
|
257
|
-
const results = await Promise.all(items.map(async (item) => {
|
|
258
|
-
return await handleTaskRunExecutionResult(item);
|
|
259
|
-
}));
|
|
260
|
-
return results;
|
|
261
|
-
}
|
|
262
|
-
return await tracer.startActiveSpan("store.downloadPayloads", async (span) => {
|
|
263
|
-
const results = await Promise.all(items.map(async (item) => {
|
|
264
|
-
return await handleTaskRunExecutionResult(item);
|
|
265
|
-
}));
|
|
266
|
-
return results;
|
|
139
|
+
}
|
|
140
|
+
throw new Error("Max attempts reached");
|
|
267
141
|
}, {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
__name(handleBatchTaskRunExecutionResult, "handleBatchTaskRunExecutionResult");
|
|
273
|
-
async function handleTaskRunExecutionResult(execution) {
|
|
274
|
-
if (execution.ok) {
|
|
275
|
-
const outputPacket = {
|
|
276
|
-
data: execution.output,
|
|
277
|
-
dataType: execution.outputType
|
|
278
|
-
};
|
|
279
|
-
const importedPacket = await v3.conditionallyImportPacket(outputPacket, tracer);
|
|
280
|
-
return {
|
|
281
|
-
ok: true,
|
|
282
|
-
id: execution.id,
|
|
283
|
-
output: await v3.parsePacket(importedPacket)
|
|
284
|
-
};
|
|
285
|
-
} else {
|
|
286
|
-
return {
|
|
287
|
-
ok: false,
|
|
288
|
-
id: execution.id,
|
|
289
|
-
error: v3.createErrorTaskError(execution.error)
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
__name(handleTaskRunExecutionResult, "handleTaskRunExecutionResult");
|
|
294
|
-
function apiClientMissingError() {
|
|
295
|
-
const hasBaseUrl = !!v3.apiClientManager.baseURL;
|
|
296
|
-
const hasAccessToken = !!v3.apiClientManager.accessToken;
|
|
297
|
-
if (!hasBaseUrl && !hasAccessToken) {
|
|
298
|
-
return `You need to set the TRIGGER_API_URL and TRIGGER_SECRET_KEY environment variables.`;
|
|
299
|
-
} else if (!hasBaseUrl) {
|
|
300
|
-
return `You need to set the TRIGGER_API_URL environment variable.`;
|
|
301
|
-
} else if (!hasAccessToken) {
|
|
302
|
-
return `You need to set the TRIGGER_SECRET_KEY environment variable.`;
|
|
303
|
-
}
|
|
304
|
-
return `Unknown error`;
|
|
305
|
-
}
|
|
306
|
-
__name(apiClientMissingError, "apiClientMissingError");
|
|
307
|
-
|
|
308
|
-
// src/v3/tasks.ts
|
|
309
|
-
function task(options) {
|
|
310
|
-
return createTask(options);
|
|
311
|
-
}
|
|
312
|
-
__name(task, "task");
|
|
313
|
-
var wait = {
|
|
314
|
-
for: async (options) => {
|
|
315
|
-
return tracer.startActiveSpan(`wait.for()`, async (span) => {
|
|
316
|
-
const durationInMs = calculateDurationInMs(options);
|
|
317
|
-
await v3.runtime.waitForDuration(durationInMs);
|
|
318
|
-
}, {
|
|
319
|
-
attributes: {
|
|
320
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "wait",
|
|
321
|
-
...v3.accessoryAttributes({
|
|
322
|
-
items: [
|
|
323
|
-
{
|
|
324
|
-
text: nameForWaitOptions(options),
|
|
325
|
-
variant: "normal"
|
|
326
|
-
}
|
|
327
|
-
],
|
|
328
|
-
style: "codepath"
|
|
329
|
-
})
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
},
|
|
333
|
-
until: async (options) => {
|
|
334
|
-
return tracer.startActiveSpan(`wait.until()`, async (span) => {
|
|
335
|
-
const start = Date.now();
|
|
336
|
-
if (options.throwIfInThePast && options.date < /* @__PURE__ */ new Date()) {
|
|
337
|
-
throw new Error("Date is in the past");
|
|
338
|
-
}
|
|
339
|
-
const durationInMs = options.date.getTime() - start;
|
|
340
|
-
await v3.runtime.waitForDuration(durationInMs);
|
|
341
|
-
}, {
|
|
342
|
-
attributes: {
|
|
343
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "wait",
|
|
344
|
-
...v3.accessoryAttributes({
|
|
345
|
-
items: [
|
|
346
|
-
{
|
|
347
|
-
text: options.date.toISOString(),
|
|
348
|
-
variant: "normal"
|
|
349
|
-
}
|
|
350
|
-
],
|
|
351
|
-
style: "codepath"
|
|
352
|
-
})
|
|
353
|
-
}
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
function nameForWaitOptions(options) {
|
|
358
|
-
if ("seconds" in options) {
|
|
359
|
-
return options.seconds === 1 ? `1 second` : `${options.seconds} seconds`;
|
|
360
|
-
}
|
|
361
|
-
if ("minutes" in options) {
|
|
362
|
-
return options.minutes === 1 ? `1 minute` : `${options.minutes} minutes`;
|
|
363
|
-
}
|
|
364
|
-
if ("hours" in options) {
|
|
365
|
-
return options.hours === 1 ? `1 hour` : `${options.hours} hours`;
|
|
366
|
-
}
|
|
367
|
-
if ("days" in options) {
|
|
368
|
-
return options.days === 1 ? `1 day` : `${options.days} days`;
|
|
369
|
-
}
|
|
370
|
-
if ("weeks" in options) {
|
|
371
|
-
return options.weeks === 1 ? `1 week` : `${options.weeks} weeks`;
|
|
372
|
-
}
|
|
373
|
-
if ("months" in options) {
|
|
374
|
-
return options.months === 1 ? `1 month` : `${options.months} months`;
|
|
375
|
-
}
|
|
376
|
-
if ("years" in options) {
|
|
377
|
-
return options.years === 1 ? `1 year` : `${options.years} years`;
|
|
378
|
-
}
|
|
379
|
-
return "NaN";
|
|
380
|
-
}
|
|
381
|
-
__name(nameForWaitOptions, "nameForWaitOptions");
|
|
382
|
-
function calculateDurationInMs(options) {
|
|
383
|
-
if ("seconds" in options) {
|
|
384
|
-
return options.seconds * 1e3;
|
|
385
|
-
}
|
|
386
|
-
if ("minutes" in options) {
|
|
387
|
-
return options.minutes * 1e3 * 60;
|
|
388
|
-
}
|
|
389
|
-
if ("hours" in options) {
|
|
390
|
-
return options.hours * 1e3 * 60 * 60;
|
|
391
|
-
}
|
|
392
|
-
if ("days" in options) {
|
|
393
|
-
return options.days * 1e3 * 60 * 60 * 24;
|
|
394
|
-
}
|
|
395
|
-
if ("weeks" in options) {
|
|
396
|
-
return options.weeks * 1e3 * 60 * 60 * 24 * 7;
|
|
397
|
-
}
|
|
398
|
-
if ("months" in options) {
|
|
399
|
-
return options.months * 1e3 * 60 * 60 * 24 * 30;
|
|
400
|
-
}
|
|
401
|
-
if ("years" in options) {
|
|
402
|
-
return options.years * 1e3 * 60 * 60 * 24 * 365;
|
|
403
|
-
}
|
|
404
|
-
throw new Error("Invalid options");
|
|
405
|
-
}
|
|
406
|
-
__name(calculateDurationInMs, "calculateDurationInMs");
|
|
407
|
-
var _InMemoryCache = class _InMemoryCache {
|
|
408
|
-
constructor() {
|
|
409
|
-
__publicField(this, "_cache", /* @__PURE__ */ new Map());
|
|
410
|
-
}
|
|
411
|
-
get(key) {
|
|
412
|
-
return this._cache.get(key);
|
|
413
|
-
}
|
|
414
|
-
set(key, value) {
|
|
415
|
-
this._cache.set(key, value);
|
|
416
|
-
return void 0;
|
|
417
|
-
}
|
|
418
|
-
delete(key) {
|
|
419
|
-
this._cache.delete(key);
|
|
420
|
-
return void 0;
|
|
421
|
-
}
|
|
422
|
-
};
|
|
423
|
-
__name(_InMemoryCache, "InMemoryCache");
|
|
424
|
-
var InMemoryCache = _InMemoryCache;
|
|
425
|
-
function createCache(store) {
|
|
426
|
-
return /* @__PURE__ */ __name(function cache(cacheKey, fn) {
|
|
427
|
-
return tracer.startActiveSpan("cache", async (span) => {
|
|
428
|
-
span.setAttribute("cache.key", cacheKey);
|
|
429
|
-
span.setAttribute(v3.SemanticInternalAttributes.STYLE_ICON, "device-sd-card");
|
|
430
|
-
const cacheEntry = await store.get(cacheKey);
|
|
431
|
-
if (cacheEntry) {
|
|
432
|
-
span.updateName(`cache.hit ${cacheKey}`);
|
|
433
|
-
return cacheEntry.value;
|
|
434
|
-
}
|
|
435
|
-
span.updateName(`cache.miss ${cacheKey}`);
|
|
436
|
-
const value = await tracer.startActiveSpan("cache.getFreshValue", async (span2) => {
|
|
437
|
-
return await fn();
|
|
438
|
-
}, {
|
|
439
|
-
attributes: {
|
|
440
|
-
"cache.key": cacheKey,
|
|
441
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "device-sd-card"
|
|
442
|
-
}
|
|
443
|
-
});
|
|
444
|
-
await tracer.startActiveSpan("cache.set", async (span2) => {
|
|
445
|
-
await store.set(cacheKey, {
|
|
446
|
-
value,
|
|
447
|
-
metadata: {
|
|
448
|
-
createdTime: Date.now()
|
|
449
|
-
}
|
|
450
|
-
});
|
|
451
|
-
}, {
|
|
452
|
-
attributes: {
|
|
453
|
-
"cache.key": cacheKey,
|
|
454
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "device-sd-card"
|
|
455
|
-
}
|
|
456
|
-
});
|
|
457
|
-
return value;
|
|
458
|
-
});
|
|
459
|
-
}, "cache");
|
|
460
|
-
}
|
|
461
|
-
__name(createCache, "createCache");
|
|
462
|
-
function onThrow(fn, options) {
|
|
463
|
-
const opts = {
|
|
464
|
-
...v3.defaultRetryOptions,
|
|
465
|
-
...options
|
|
466
|
-
};
|
|
467
|
-
return tracer.startActiveSpan(`retry.onThrow()`, async (span) => {
|
|
468
|
-
let attempt = 1;
|
|
469
|
-
while (attempt <= opts.maxAttempts) {
|
|
470
|
-
const innerSpan = tracer.startSpan("retry.fn()", {
|
|
471
|
-
attributes: {
|
|
472
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "function",
|
|
473
|
-
...v3.accessoryAttributes({
|
|
474
|
-
items: [
|
|
475
|
-
{
|
|
476
|
-
text: `${attempt}/${opts.maxAttempts}`,
|
|
477
|
-
variant: "normal"
|
|
478
|
-
}
|
|
479
|
-
],
|
|
480
|
-
style: "codepath"
|
|
481
|
-
})
|
|
482
|
-
}
|
|
483
|
-
});
|
|
484
|
-
const contextWithSpanSet = api.trace.setSpan(api.context.active(), innerSpan);
|
|
485
|
-
try {
|
|
486
|
-
const result = await api.context.with(contextWithSpanSet, async () => {
|
|
487
|
-
return fn({
|
|
488
|
-
attempt,
|
|
489
|
-
maxAttempts: opts.maxAttempts
|
|
490
|
-
});
|
|
491
|
-
});
|
|
492
|
-
innerSpan.end();
|
|
493
|
-
return result;
|
|
494
|
-
} catch (e) {
|
|
495
|
-
if (e instanceof Error || typeof e === "string") {
|
|
496
|
-
innerSpan.recordException(e);
|
|
497
|
-
} else {
|
|
498
|
-
innerSpan.recordException(String(e));
|
|
499
|
-
}
|
|
500
|
-
innerSpan.setStatus({
|
|
501
|
-
code: api.SpanStatusCode.ERROR
|
|
502
|
-
});
|
|
503
|
-
const nextRetryDelay = v3.calculateNextRetryDelay(opts, attempt);
|
|
504
|
-
if (!nextRetryDelay) {
|
|
505
|
-
innerSpan.end();
|
|
506
|
-
throw e;
|
|
507
|
-
}
|
|
508
|
-
innerSpan.setAttribute(v3.SemanticInternalAttributes.RETRY_AT, new Date(Date.now() + nextRetryDelay).toISOString());
|
|
509
|
-
innerSpan.setAttribute(v3.SemanticInternalAttributes.RETRY_COUNT, attempt);
|
|
510
|
-
innerSpan.setAttribute(v3.SemanticInternalAttributes.RETRY_DELAY, `${nextRetryDelay}ms`);
|
|
511
|
-
innerSpan.end();
|
|
512
|
-
await v3.runtime.waitForDuration(nextRetryDelay);
|
|
513
|
-
} finally {
|
|
514
|
-
attempt++;
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
throw new Error("Max attempts reached");
|
|
518
|
-
}, {
|
|
519
|
-
attributes: {
|
|
520
|
-
[v3.SemanticInternalAttributes.STYLE_ICON]: "arrow-capsule"
|
|
521
|
-
}
|
|
142
|
+
attributes: {
|
|
143
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "arrow-capsule"
|
|
144
|
+
}
|
|
522
145
|
});
|
|
523
146
|
}
|
|
524
147
|
__name(onThrow, "onThrow");
|
|
@@ -878,14 +501,596 @@ var retry = {
|
|
|
878
501
|
fetch: retryFetch,
|
|
879
502
|
interceptFetch
|
|
880
503
|
};
|
|
881
|
-
|
|
504
|
+
function queue(options) {
|
|
505
|
+
return options;
|
|
506
|
+
}
|
|
507
|
+
__name(queue, "queue");
|
|
508
|
+
function createTask(params) {
|
|
509
|
+
const task3 = {
|
|
510
|
+
id: params.id,
|
|
511
|
+
trigger: async (payload, options) => {
|
|
512
|
+
const apiClient = v3.apiClientManager.client;
|
|
513
|
+
if (!apiClient) {
|
|
514
|
+
throw apiClientMissingError();
|
|
515
|
+
}
|
|
516
|
+
const taskMetadata = v3.taskCatalog.getTaskMetadata(params.id);
|
|
517
|
+
const payloadPacket = await v3.stringifyIO(payload);
|
|
518
|
+
const handle = await tracer.startActiveSpan(taskMetadata ? "Trigger" : `${params.id} trigger()`, async (span) => {
|
|
519
|
+
const response = await apiClient.triggerTask(params.id, {
|
|
520
|
+
payload: payloadPacket.data,
|
|
521
|
+
options: {
|
|
522
|
+
queue: params.queue,
|
|
523
|
+
concurrencyKey: options?.concurrencyKey,
|
|
524
|
+
test: v3.taskContext.ctx?.run.isTest,
|
|
525
|
+
payloadType: payloadPacket.dataType,
|
|
526
|
+
idempotencyKey: options?.idempotencyKey
|
|
527
|
+
}
|
|
528
|
+
}, {
|
|
529
|
+
spanParentAsLink: true
|
|
530
|
+
});
|
|
531
|
+
span.setAttribute("messaging.message.id", response.id);
|
|
532
|
+
return response;
|
|
533
|
+
}, {
|
|
534
|
+
kind: api.SpanKind.PRODUCER,
|
|
535
|
+
attributes: {
|
|
536
|
+
[semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
537
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
|
|
538
|
+
["messaging.client_id"]: v3.taskContext.worker?.id,
|
|
539
|
+
[semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
540
|
+
[semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
541
|
+
...taskMetadata ? v3.accessoryAttributes({
|
|
542
|
+
items: [
|
|
543
|
+
{
|
|
544
|
+
text: `${taskMetadata.exportName}.trigger()`,
|
|
545
|
+
variant: "normal"
|
|
546
|
+
}
|
|
547
|
+
],
|
|
548
|
+
style: "codepath"
|
|
549
|
+
}) : {}
|
|
550
|
+
}
|
|
551
|
+
});
|
|
552
|
+
return handle;
|
|
553
|
+
},
|
|
554
|
+
batchTrigger: async (items) => {
|
|
555
|
+
const apiClient = v3.apiClientManager.client;
|
|
556
|
+
if (!apiClient) {
|
|
557
|
+
throw apiClientMissingError();
|
|
558
|
+
}
|
|
559
|
+
const taskMetadata = v3.taskCatalog.getTaskMetadata(params.id);
|
|
560
|
+
const response = await tracer.startActiveSpan(taskMetadata ? "Batch trigger" : `${params.id} batchTrigger()`, async (span) => {
|
|
561
|
+
const response2 = await apiClient.batchTriggerTask(params.id, {
|
|
562
|
+
items: await Promise.all(items.map(async (item) => {
|
|
563
|
+
const payloadPacket = await v3.stringifyIO(item.payload);
|
|
564
|
+
return {
|
|
565
|
+
payload: payloadPacket.data,
|
|
566
|
+
options: {
|
|
567
|
+
queue: item.options?.queue ?? params.queue,
|
|
568
|
+
concurrencyKey: item.options?.concurrencyKey,
|
|
569
|
+
test: v3.taskContext.ctx?.run.isTest,
|
|
570
|
+
payloadType: payloadPacket.dataType,
|
|
571
|
+
idempotencyKey: item.options?.idempotencyKey
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
}))
|
|
575
|
+
}, {
|
|
576
|
+
spanParentAsLink: true
|
|
577
|
+
});
|
|
578
|
+
span.setAttribute("messaging.message.id", response2.batchId);
|
|
579
|
+
return response2;
|
|
580
|
+
}, {
|
|
581
|
+
kind: api.SpanKind.PRODUCER,
|
|
582
|
+
attributes: {
|
|
583
|
+
[semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
584
|
+
["messaging.batch.message_count"]: items.length,
|
|
585
|
+
["messaging.client_id"]: v3.taskContext.worker?.id,
|
|
586
|
+
[semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
587
|
+
[semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
588
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
|
|
589
|
+
...taskMetadata ? v3.accessoryAttributes({
|
|
590
|
+
items: [
|
|
591
|
+
{
|
|
592
|
+
text: `${taskMetadata.exportName}.batchTrigger()`,
|
|
593
|
+
variant: "normal"
|
|
594
|
+
}
|
|
595
|
+
],
|
|
596
|
+
style: "codepath"
|
|
597
|
+
}) : {}
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
return response;
|
|
601
|
+
},
|
|
602
|
+
triggerAndWait: async (payload, options) => {
|
|
603
|
+
const ctx = v3.taskContext.ctx;
|
|
604
|
+
if (!ctx) {
|
|
605
|
+
throw new Error("triggerAndWait can only be used from inside a task.run()");
|
|
606
|
+
}
|
|
607
|
+
const apiClient = v3.apiClientManager.client;
|
|
608
|
+
if (!apiClient) {
|
|
609
|
+
throw apiClientMissingError();
|
|
610
|
+
}
|
|
611
|
+
const taskMetadata = v3.taskCatalog.getTaskMetadata(params.id);
|
|
612
|
+
const payloadPacket = await v3.stringifyIO(payload);
|
|
613
|
+
return await tracer.startActiveSpan(taskMetadata ? "Trigger" : `${params.id} triggerAndWait()`, async (span) => {
|
|
614
|
+
const response = await apiClient.triggerTask(params.id, {
|
|
615
|
+
payload: payloadPacket.data,
|
|
616
|
+
options: {
|
|
617
|
+
dependentAttempt: ctx.attempt.id,
|
|
618
|
+
lockToVersion: v3.taskContext.worker?.version,
|
|
619
|
+
queue: params.queue,
|
|
620
|
+
concurrencyKey: options?.concurrencyKey,
|
|
621
|
+
test: v3.taskContext.ctx?.run.isTest,
|
|
622
|
+
payloadType: payloadPacket.dataType,
|
|
623
|
+
idempotencyKey: options?.idempotencyKey
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
span.setAttribute("messaging.message.id", response.id);
|
|
627
|
+
if (options?.idempotencyKey) {
|
|
628
|
+
const result2 = await apiClient.getRunResult(response.id);
|
|
629
|
+
if (result2) {
|
|
630
|
+
v3.logger.log(`Result reused from previous task run with idempotency key '${options.idempotencyKey}'.`, {
|
|
631
|
+
runId: response.id,
|
|
632
|
+
idempotencyKey: options.idempotencyKey
|
|
633
|
+
});
|
|
634
|
+
return await handleTaskRunExecutionResult(result2);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
const result = await v3.runtime.waitForTask({
|
|
638
|
+
id: response.id,
|
|
639
|
+
ctx
|
|
640
|
+
});
|
|
641
|
+
return await handleTaskRunExecutionResult(result);
|
|
642
|
+
}, {
|
|
643
|
+
kind: api.SpanKind.PRODUCER,
|
|
644
|
+
attributes: {
|
|
645
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
|
|
646
|
+
[semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
647
|
+
["messaging.client_id"]: v3.taskContext.worker?.id,
|
|
648
|
+
[semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
649
|
+
[semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
650
|
+
...taskMetadata ? v3.accessoryAttributes({
|
|
651
|
+
items: [
|
|
652
|
+
{
|
|
653
|
+
text: `${taskMetadata.exportName}.triggerAndWait()`,
|
|
654
|
+
variant: "normal"
|
|
655
|
+
}
|
|
656
|
+
],
|
|
657
|
+
style: "codepath"
|
|
658
|
+
}) : {}
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
},
|
|
662
|
+
batchTriggerAndWait: async (items) => {
|
|
663
|
+
const ctx = v3.taskContext.ctx;
|
|
664
|
+
if (!ctx) {
|
|
665
|
+
throw new Error("batchTriggerAndWait can only be used from inside a task.run()");
|
|
666
|
+
}
|
|
667
|
+
const apiClient = v3.apiClientManager.client;
|
|
668
|
+
if (!apiClient) {
|
|
669
|
+
throw apiClientMissingError();
|
|
670
|
+
}
|
|
671
|
+
const taskMetadata = v3.taskCatalog.getTaskMetadata(params.id);
|
|
672
|
+
return await tracer.startActiveSpan(taskMetadata ? "Batch trigger" : `${params.id} batchTriggerAndWait()`, async (span) => {
|
|
673
|
+
const response = await apiClient.batchTriggerTask(params.id, {
|
|
674
|
+
items: await Promise.all(items.map(async (item) => {
|
|
675
|
+
const payloadPacket = await v3.stringifyIO(item.payload);
|
|
676
|
+
return {
|
|
677
|
+
payload: payloadPacket.data,
|
|
678
|
+
options: {
|
|
679
|
+
lockToVersion: v3.taskContext.worker?.version,
|
|
680
|
+
queue: item.options?.queue ?? params.queue,
|
|
681
|
+
concurrencyKey: item.options?.concurrencyKey,
|
|
682
|
+
test: v3.taskContext.ctx?.run.isTest,
|
|
683
|
+
payloadType: payloadPacket.dataType,
|
|
684
|
+
idempotencyKey: item.options?.idempotencyKey
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
})),
|
|
688
|
+
dependentAttempt: ctx.attempt.id
|
|
689
|
+
});
|
|
690
|
+
span.setAttribute("messaging.message.id", response.batchId);
|
|
691
|
+
const getBatchResults = /* @__PURE__ */ __name(async () => {
|
|
692
|
+
const hasIdempotencyKey = items.some((item) => item.options?.idempotencyKey);
|
|
693
|
+
if (hasIdempotencyKey) {
|
|
694
|
+
const results = await apiClient.getBatchResults(response.batchId);
|
|
695
|
+
if (results) {
|
|
696
|
+
return results;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
return {
|
|
700
|
+
id: response.batchId,
|
|
701
|
+
items: []
|
|
702
|
+
};
|
|
703
|
+
}, "getBatchResults");
|
|
704
|
+
const existingResults = await getBatchResults();
|
|
705
|
+
const incompleteRuns = response.runs.filter((runId) => !existingResults.items.some((item) => item.id === runId));
|
|
706
|
+
if (incompleteRuns.length === 0) {
|
|
707
|
+
v3.logger.log(`Results reused from previous task runs because of the provided idempotency keys.`);
|
|
708
|
+
const runs3 = await handleBatchTaskRunExecutionResult(existingResults.items);
|
|
709
|
+
return {
|
|
710
|
+
id: existingResults.id,
|
|
711
|
+
runs: runs3
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
const result = await v3.runtime.waitForBatch({
|
|
715
|
+
id: response.batchId,
|
|
716
|
+
runs: incompleteRuns,
|
|
717
|
+
ctx
|
|
718
|
+
});
|
|
719
|
+
const combinedItems = [];
|
|
720
|
+
for (const runId of response.runs) {
|
|
721
|
+
const existingItem = existingResults.items.find((item) => item.id === runId);
|
|
722
|
+
if (existingItem) {
|
|
723
|
+
combinedItems.push(existingItem);
|
|
724
|
+
} else {
|
|
725
|
+
const newItem = result.items.find((item) => item.id === runId);
|
|
726
|
+
if (newItem) {
|
|
727
|
+
combinedItems.push(newItem);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
const runs2 = await handleBatchTaskRunExecutionResult(combinedItems);
|
|
732
|
+
return {
|
|
733
|
+
id: result.id,
|
|
734
|
+
runs: runs2
|
|
735
|
+
};
|
|
736
|
+
}, {
|
|
737
|
+
kind: api.SpanKind.PRODUCER,
|
|
738
|
+
attributes: {
|
|
739
|
+
[semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
740
|
+
["messaging.batch.message_count"]: items.length,
|
|
741
|
+
["messaging.client_id"]: v3.taskContext.worker?.id,
|
|
742
|
+
[semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
743
|
+
[semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
744
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
|
|
745
|
+
...taskMetadata ? v3.accessoryAttributes({
|
|
746
|
+
items: [
|
|
747
|
+
{
|
|
748
|
+
text: `${taskMetadata.exportName}.batchTriggerAndWait()`,
|
|
749
|
+
variant: "normal"
|
|
750
|
+
}
|
|
751
|
+
],
|
|
752
|
+
style: "codepath"
|
|
753
|
+
}) : {}
|
|
754
|
+
}
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
v3.taskCatalog.registerTaskMetadata({
|
|
759
|
+
id: params.id,
|
|
760
|
+
packageVersion: version,
|
|
761
|
+
queue: params.queue,
|
|
762
|
+
retry: params.retry ? {
|
|
763
|
+
...v3.defaultRetryOptions,
|
|
764
|
+
...params.retry
|
|
765
|
+
} : void 0,
|
|
766
|
+
machine: params.machine,
|
|
767
|
+
fns: {
|
|
768
|
+
run: params.run,
|
|
769
|
+
init: params.init,
|
|
770
|
+
cleanup: params.cleanup,
|
|
771
|
+
middleware: params.middleware,
|
|
772
|
+
handleError: params.handleError,
|
|
773
|
+
onSuccess: params.onSuccess,
|
|
774
|
+
onFailure: params.onFailure,
|
|
775
|
+
onStart: params.onStart
|
|
776
|
+
}
|
|
777
|
+
});
|
|
778
|
+
return task3;
|
|
779
|
+
}
|
|
780
|
+
__name(createTask, "createTask");
|
|
781
|
+
async function handleBatchTaskRunExecutionResult(items) {
|
|
782
|
+
const someObjectStoreOutputs = items.some((item) => item.ok && item.outputType === "application/store");
|
|
783
|
+
if (!someObjectStoreOutputs) {
|
|
784
|
+
const results = await Promise.all(items.map(async (item) => {
|
|
785
|
+
return await handleTaskRunExecutionResult(item);
|
|
786
|
+
}));
|
|
787
|
+
return results;
|
|
788
|
+
}
|
|
789
|
+
return await tracer.startActiveSpan("store.downloadPayloads", async (span) => {
|
|
790
|
+
const results = await Promise.all(items.map(async (item) => {
|
|
791
|
+
return await handleTaskRunExecutionResult(item);
|
|
792
|
+
}));
|
|
793
|
+
return results;
|
|
794
|
+
}, {
|
|
795
|
+
kind: api.SpanKind.INTERNAL,
|
|
796
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "cloud-download"
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
__name(handleBatchTaskRunExecutionResult, "handleBatchTaskRunExecutionResult");
|
|
800
|
+
async function handleTaskRunExecutionResult(execution) {
|
|
801
|
+
if (execution.ok) {
|
|
802
|
+
const outputPacket = {
|
|
803
|
+
data: execution.output,
|
|
804
|
+
dataType: execution.outputType
|
|
805
|
+
};
|
|
806
|
+
const importedPacket = await v3.conditionallyImportPacket(outputPacket, tracer);
|
|
807
|
+
return {
|
|
808
|
+
ok: true,
|
|
809
|
+
id: execution.id,
|
|
810
|
+
output: await v3.parsePacket(importedPacket)
|
|
811
|
+
};
|
|
812
|
+
} else {
|
|
813
|
+
return {
|
|
814
|
+
ok: false,
|
|
815
|
+
id: execution.id,
|
|
816
|
+
error: v3.createErrorTaskError(execution.error)
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
__name(handleTaskRunExecutionResult, "handleTaskRunExecutionResult");
|
|
821
|
+
function apiClientMissingError() {
|
|
822
|
+
const hasBaseUrl = !!v3.apiClientManager.baseURL;
|
|
823
|
+
const hasAccessToken = !!v3.apiClientManager.accessToken;
|
|
824
|
+
if (!hasBaseUrl && !hasAccessToken) {
|
|
825
|
+
return `You need to set the TRIGGER_API_URL and TRIGGER_SECRET_KEY environment variables.`;
|
|
826
|
+
} else if (!hasBaseUrl) {
|
|
827
|
+
return `You need to set the TRIGGER_API_URL environment variable.`;
|
|
828
|
+
} else if (!hasAccessToken) {
|
|
829
|
+
return `You need to set the TRIGGER_SECRET_KEY environment variable.`;
|
|
830
|
+
}
|
|
831
|
+
return `Unknown error`;
|
|
832
|
+
}
|
|
833
|
+
__name(apiClientMissingError, "apiClientMissingError");
|
|
834
|
+
|
|
835
|
+
// src/v3/tasks.ts
|
|
836
|
+
function task(options) {
|
|
837
|
+
return createTask(options);
|
|
838
|
+
}
|
|
839
|
+
__name(task, "task");
|
|
840
|
+
var wait = {
|
|
841
|
+
for: async (options) => {
|
|
842
|
+
return tracer.startActiveSpan(`wait.for()`, async (span) => {
|
|
843
|
+
const durationInMs = calculateDurationInMs(options);
|
|
844
|
+
await v3.runtime.waitForDuration(durationInMs);
|
|
845
|
+
}, {
|
|
846
|
+
attributes: {
|
|
847
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "wait",
|
|
848
|
+
...v3.accessoryAttributes({
|
|
849
|
+
items: [
|
|
850
|
+
{
|
|
851
|
+
text: nameForWaitOptions(options),
|
|
852
|
+
variant: "normal"
|
|
853
|
+
}
|
|
854
|
+
],
|
|
855
|
+
style: "codepath"
|
|
856
|
+
})
|
|
857
|
+
}
|
|
858
|
+
});
|
|
859
|
+
},
|
|
860
|
+
until: async (options) => {
|
|
861
|
+
return tracer.startActiveSpan(`wait.until()`, async (span) => {
|
|
862
|
+
const start = Date.now();
|
|
863
|
+
if (options.throwIfInThePast && options.date < /* @__PURE__ */ new Date()) {
|
|
864
|
+
throw new Error("Date is in the past");
|
|
865
|
+
}
|
|
866
|
+
const durationInMs = options.date.getTime() - start;
|
|
867
|
+
await v3.runtime.waitForDuration(durationInMs);
|
|
868
|
+
}, {
|
|
869
|
+
attributes: {
|
|
870
|
+
[v3.SemanticInternalAttributes.STYLE_ICON]: "wait",
|
|
871
|
+
...v3.accessoryAttributes({
|
|
872
|
+
items: [
|
|
873
|
+
{
|
|
874
|
+
text: options.date.toISOString(),
|
|
875
|
+
variant: "normal"
|
|
876
|
+
}
|
|
877
|
+
],
|
|
878
|
+
style: "codepath"
|
|
879
|
+
})
|
|
880
|
+
}
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
function nameForWaitOptions(options) {
|
|
885
|
+
if ("seconds" in options) {
|
|
886
|
+
return options.seconds === 1 ? `1 second` : `${options.seconds} seconds`;
|
|
887
|
+
}
|
|
888
|
+
if ("minutes" in options) {
|
|
889
|
+
return options.minutes === 1 ? `1 minute` : `${options.minutes} minutes`;
|
|
890
|
+
}
|
|
891
|
+
if ("hours" in options) {
|
|
892
|
+
return options.hours === 1 ? `1 hour` : `${options.hours} hours`;
|
|
893
|
+
}
|
|
894
|
+
if ("days" in options) {
|
|
895
|
+
return options.days === 1 ? `1 day` : `${options.days} days`;
|
|
896
|
+
}
|
|
897
|
+
if ("weeks" in options) {
|
|
898
|
+
return options.weeks === 1 ? `1 week` : `${options.weeks} weeks`;
|
|
899
|
+
}
|
|
900
|
+
if ("months" in options) {
|
|
901
|
+
return options.months === 1 ? `1 month` : `${options.months} months`;
|
|
902
|
+
}
|
|
903
|
+
if ("years" in options) {
|
|
904
|
+
return options.years === 1 ? `1 year` : `${options.years} years`;
|
|
905
|
+
}
|
|
906
|
+
return "NaN";
|
|
907
|
+
}
|
|
908
|
+
__name(nameForWaitOptions, "nameForWaitOptions");
|
|
909
|
+
function calculateDurationInMs(options) {
|
|
910
|
+
if ("seconds" in options) {
|
|
911
|
+
return options.seconds * 1e3;
|
|
912
|
+
}
|
|
913
|
+
if ("minutes" in options) {
|
|
914
|
+
return options.minutes * 1e3 * 60;
|
|
915
|
+
}
|
|
916
|
+
if ("hours" in options) {
|
|
917
|
+
return options.hours * 1e3 * 60 * 60;
|
|
918
|
+
}
|
|
919
|
+
if ("days" in options) {
|
|
920
|
+
return options.days * 1e3 * 60 * 60 * 24;
|
|
921
|
+
}
|
|
922
|
+
if ("weeks" in options) {
|
|
923
|
+
return options.weeks * 1e3 * 60 * 60 * 24 * 7;
|
|
924
|
+
}
|
|
925
|
+
if ("months" in options) {
|
|
926
|
+
return options.months * 1e3 * 60 * 60 * 24 * 30;
|
|
927
|
+
}
|
|
928
|
+
if ("years" in options) {
|
|
929
|
+
return options.years * 1e3 * 60 * 60 * 24 * 365;
|
|
930
|
+
}
|
|
931
|
+
throw new Error("Invalid options");
|
|
932
|
+
}
|
|
933
|
+
__name(calculateDurationInMs, "calculateDurationInMs");
|
|
934
|
+
var runs = {
|
|
935
|
+
replay: replayRun,
|
|
936
|
+
cancel: cancelRun,
|
|
937
|
+
retrieve: retrieveRun
|
|
938
|
+
};
|
|
939
|
+
async function retrieveRun(runId) {
|
|
940
|
+
const apiClient = v3.apiClientManager.client;
|
|
941
|
+
if (!apiClient) {
|
|
942
|
+
throw apiClientMissingError();
|
|
943
|
+
}
|
|
944
|
+
return await apiClient.retrieveRun(runId);
|
|
945
|
+
}
|
|
946
|
+
__name(retrieveRun, "retrieveRun");
|
|
947
|
+
async function replayRun(runId) {
|
|
948
|
+
const apiClient = v3.apiClientManager.client;
|
|
949
|
+
if (!apiClient) {
|
|
950
|
+
throw apiClientMissingError();
|
|
951
|
+
}
|
|
952
|
+
return await apiClient.replayRun(runId);
|
|
953
|
+
}
|
|
954
|
+
__name(replayRun, "replayRun");
|
|
955
|
+
async function cancelRun(runId) {
|
|
956
|
+
const apiClient = v3.apiClientManager.client;
|
|
957
|
+
if (!apiClient) {
|
|
958
|
+
throw apiClientMissingError();
|
|
959
|
+
}
|
|
960
|
+
return await apiClient.cancelRun(runId);
|
|
961
|
+
}
|
|
962
|
+
__name(cancelRun, "cancelRun");
|
|
963
|
+
|
|
964
|
+
// src/v3/schedules/index.ts
|
|
965
|
+
var schedules_exports = {};
|
|
966
|
+
__export(schedules_exports, {
|
|
967
|
+
activate: () => activate,
|
|
968
|
+
create: () => create,
|
|
969
|
+
deactivate: () => deactivate,
|
|
970
|
+
del: () => del,
|
|
971
|
+
list: () => list,
|
|
972
|
+
retrieve: () => retrieve,
|
|
973
|
+
task: () => task2,
|
|
974
|
+
update: () => update
|
|
975
|
+
});
|
|
976
|
+
function task2(params) {
|
|
977
|
+
const task3 = createTask(params);
|
|
978
|
+
v3.taskCatalog.updateTaskMetadata(task3.id, {
|
|
979
|
+
triggerSource: "schedule"
|
|
980
|
+
});
|
|
981
|
+
return task3;
|
|
982
|
+
}
|
|
983
|
+
__name(task2, "task");
|
|
984
|
+
async function create(options) {
|
|
985
|
+
const apiClient = v3.apiClientManager.client;
|
|
986
|
+
if (!apiClient) {
|
|
987
|
+
throw apiClientMissingError();
|
|
988
|
+
}
|
|
989
|
+
return apiClient.createSchedule(options);
|
|
990
|
+
}
|
|
991
|
+
__name(create, "create");
|
|
992
|
+
async function retrieve(scheduleId) {
|
|
993
|
+
const apiClient = v3.apiClientManager.client;
|
|
994
|
+
if (!apiClient) {
|
|
995
|
+
throw apiClientMissingError();
|
|
996
|
+
}
|
|
997
|
+
return apiClient.retrieveSchedule(scheduleId);
|
|
998
|
+
}
|
|
999
|
+
__name(retrieve, "retrieve");
|
|
1000
|
+
async function update(scheduleId, options) {
|
|
1001
|
+
const apiClient = v3.apiClientManager.client;
|
|
1002
|
+
if (!apiClient) {
|
|
1003
|
+
throw apiClientMissingError();
|
|
1004
|
+
}
|
|
1005
|
+
return apiClient.updateSchedule(scheduleId, options);
|
|
1006
|
+
}
|
|
1007
|
+
__name(update, "update");
|
|
1008
|
+
async function del(scheduleId) {
|
|
1009
|
+
const apiClient = v3.apiClientManager.client;
|
|
1010
|
+
if (!apiClient) {
|
|
1011
|
+
throw apiClientMissingError();
|
|
1012
|
+
}
|
|
1013
|
+
return apiClient.deleteSchedule(scheduleId);
|
|
1014
|
+
}
|
|
1015
|
+
__name(del, "del");
|
|
1016
|
+
async function deactivate(scheduleId) {
|
|
1017
|
+
const apiClient = v3.apiClientManager.client;
|
|
1018
|
+
if (!apiClient) {
|
|
1019
|
+
throw apiClientMissingError();
|
|
1020
|
+
}
|
|
1021
|
+
return apiClient.deactivateSchedule(scheduleId);
|
|
1022
|
+
}
|
|
1023
|
+
__name(deactivate, "deactivate");
|
|
1024
|
+
async function activate(scheduleId) {
|
|
1025
|
+
const apiClient = v3.apiClientManager.client;
|
|
1026
|
+
if (!apiClient) {
|
|
1027
|
+
throw apiClientMissingError();
|
|
1028
|
+
}
|
|
1029
|
+
return apiClient.activateSchedule(scheduleId);
|
|
1030
|
+
}
|
|
1031
|
+
__name(activate, "activate");
|
|
1032
|
+
async function list(options) {
|
|
1033
|
+
const apiClient = v3.apiClientManager.client;
|
|
1034
|
+
if (!apiClient) {
|
|
1035
|
+
throw apiClientMissingError();
|
|
1036
|
+
}
|
|
1037
|
+
return apiClient.listSchedules(options);
|
|
1038
|
+
}
|
|
1039
|
+
__name(list, "list");
|
|
1040
|
+
|
|
1041
|
+
// src/v3/index.ts
|
|
1042
|
+
function configure(options) {
|
|
1043
|
+
v3.apiClientManager.setGlobalAPIClientConfiguration(options);
|
|
1044
|
+
}
|
|
1045
|
+
__name(configure, "configure");
|
|
1046
|
+
|
|
1047
|
+
Object.defineProperty(exports, 'APIError', {
|
|
1048
|
+
enumerable: true,
|
|
1049
|
+
get: function () { return v3.APIError; }
|
|
1050
|
+
});
|
|
1051
|
+
Object.defineProperty(exports, 'AuthenticationError', {
|
|
1052
|
+
enumerable: true,
|
|
1053
|
+
get: function () { return v3.AuthenticationError; }
|
|
1054
|
+
});
|
|
1055
|
+
Object.defineProperty(exports, 'BadRequestError', {
|
|
1056
|
+
enumerable: true,
|
|
1057
|
+
get: function () { return v3.BadRequestError; }
|
|
1058
|
+
});
|
|
1059
|
+
Object.defineProperty(exports, 'ConflictError', {
|
|
1060
|
+
enumerable: true,
|
|
1061
|
+
get: function () { return v3.ConflictError; }
|
|
1062
|
+
});
|
|
1063
|
+
Object.defineProperty(exports, 'InternalServerError', {
|
|
1064
|
+
enumerable: true,
|
|
1065
|
+
get: function () { return v3.InternalServerError; }
|
|
1066
|
+
});
|
|
1067
|
+
Object.defineProperty(exports, 'NotFoundError', {
|
|
1068
|
+
enumerable: true,
|
|
1069
|
+
get: function () { return v3.NotFoundError; }
|
|
1070
|
+
});
|
|
1071
|
+
Object.defineProperty(exports, 'PermissionDeniedError', {
|
|
1072
|
+
enumerable: true,
|
|
1073
|
+
get: function () { return v3.PermissionDeniedError; }
|
|
1074
|
+
});
|
|
1075
|
+
Object.defineProperty(exports, 'RateLimitError', {
|
|
1076
|
+
enumerable: true,
|
|
1077
|
+
get: function () { return v3.RateLimitError; }
|
|
1078
|
+
});
|
|
1079
|
+
Object.defineProperty(exports, 'UnprocessableEntityError', {
|
|
1080
|
+
enumerable: true,
|
|
1081
|
+
get: function () { return v3.UnprocessableEntityError; }
|
|
1082
|
+
});
|
|
882
1083
|
Object.defineProperty(exports, 'logger', {
|
|
883
1084
|
enumerable: true,
|
|
884
1085
|
get: function () { return v3.logger; }
|
|
885
1086
|
});
|
|
886
1087
|
exports.InMemoryCache = InMemoryCache;
|
|
1088
|
+
exports.configure = configure;
|
|
887
1089
|
exports.createCache = createCache;
|
|
1090
|
+
exports.queue = queue;
|
|
888
1091
|
exports.retry = retry;
|
|
1092
|
+
exports.runs = runs;
|
|
1093
|
+
exports.schedules = schedules_exports;
|
|
889
1094
|
exports.task = task;
|
|
890
1095
|
exports.wait = wait;
|
|
891
1096
|
//# sourceMappingURL=out.js.map
|