@trigger.dev/sdk 3.0.0-beta.50 → 3.0.0-beta.51
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 +202 -22
- package/dist/v3/index.d.ts +202 -22
- package/dist/v3/index.js +218 -296
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/index.mjs +220 -299
- package/dist/v3/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/v3/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { TriggerTracer, SemanticInternalAttributes, apiClientManager, isRequestOptions, mergeRequestOptions, accessoryAttributes, flattenAttributes, taskContext, runtime, usage as usage$1, taskCatalog, TimezonesResult, defaultRetryOptions, calculateNextRetryDelay, defaultFetchRetryOptions, calculateResetAt, eventFilterMatches, stringifyIO,
|
|
1
|
+
import { TriggerTracer, SemanticInternalAttributes, apiClientManager, isRequestOptions, mergeRequestOptions, accessoryAttributes, flattenAttributes, taskContext, runtime, usage as usage$1, UnprocessableEntityError, logger, taskCatalog, TimezonesResult, defaultRetryOptions, calculateNextRetryDelay, defaultFetchRetryOptions, calculateResetAt, eventFilterMatches, stringifyIO, conditionallyImportPacket, parsePacket, createErrorTaskError } from '@trigger.dev/core/v3';
|
|
2
2
|
export { AbortTaskRunError, ApiError, AuthenticationError, BadRequestError, ConflictError, InternalServerError, NotFoundError, PermissionDeniedError, RateLimitError, UnprocessableEntityError, logger } from '@trigger.dev/core/v3';
|
|
3
3
|
import { trace, context, SpanStatusCode, SpanKind } from '@opentelemetry/api';
|
|
4
|
-
import { SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_URL, SEMATTRS_HTTP_HOST, SEMATTRS_HTTP_SCHEME, SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, SEMATTRS_MESSAGING_OPERATION,
|
|
4
|
+
import { SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_URL, SEMATTRS_HTTP_HOST, SEMATTRS_HTTP_SCHEME, SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, SEMATTRS_MESSAGING_OPERATION, SEMATTRS_MESSAGING_SYSTEM, SEMATTRS_MESSAGING_DESTINATION } from '@opentelemetry/semantic-conventions';
|
|
5
5
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
6
6
|
import { zodfetch } from '@trigger.dev/core/v3/zodfetch';
|
|
7
7
|
|
|
@@ -18,7 +18,7 @@ var __publicField = (obj, key, value) => {
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
// package.json
|
|
21
|
-
var version = "3.0.0-beta.
|
|
21
|
+
var version = "3.0.0-beta.51";
|
|
22
22
|
|
|
23
23
|
// src/v3/tracer.ts
|
|
24
24
|
var tracer = new TriggerTracer({
|
|
@@ -685,16 +685,16 @@ function rescheduleRun(runId, body, requestOptions) {
|
|
|
685
685
|
}
|
|
686
686
|
__name(rescheduleRun, "rescheduleRun");
|
|
687
687
|
var MAX_POLL_ATTEMPTS = 500;
|
|
688
|
-
async function poll(
|
|
688
|
+
async function poll(runId, options, requestOptions) {
|
|
689
689
|
let attempts = 0;
|
|
690
690
|
while (attempts++ < MAX_POLL_ATTEMPTS) {
|
|
691
|
-
const run = await runs.retrieve(
|
|
691
|
+
const run = await runs.retrieve(runId, requestOptions);
|
|
692
692
|
if (run.isCompleted) {
|
|
693
693
|
return run;
|
|
694
694
|
}
|
|
695
695
|
await new Promise((resolve) => setTimeout(resolve, options?.pollIntervalMs ?? 1e3));
|
|
696
696
|
}
|
|
697
|
-
throw new Error(`Run ${
|
|
697
|
+
throw new Error(`Run ${typeof runId === "string" ? runId : runId.id} did not complete after ${MAX_POLL_ATTEMPTS} attempts`);
|
|
698
698
|
}
|
|
699
699
|
__name(poll, "poll");
|
|
700
700
|
var idempotencyKeys = {
|
|
@@ -748,265 +748,20 @@ function createTask(params) {
|
|
|
748
748
|
const task3 = {
|
|
749
749
|
id: params.id,
|
|
750
750
|
trigger: async (payload, options) => {
|
|
751
|
-
const apiClient = apiClientManager.client;
|
|
752
|
-
if (!apiClient) {
|
|
753
|
-
throw apiClientMissingError();
|
|
754
|
-
}
|
|
755
751
|
const taskMetadata = taskCatalog.getTaskMetadata(params.id);
|
|
756
|
-
|
|
757
|
-
const handle = await apiClient.triggerTask(params.id, {
|
|
758
|
-
payload: payloadPacket.data,
|
|
759
|
-
options: {
|
|
760
|
-
queue: options?.queue ?? params.queue,
|
|
761
|
-
concurrencyKey: options?.concurrencyKey,
|
|
762
|
-
test: taskContext.ctx?.run.isTest,
|
|
763
|
-
payloadType: payloadPacket.dataType,
|
|
764
|
-
idempotencyKey: await makeKey(options?.idempotencyKey),
|
|
765
|
-
delay: options?.delay,
|
|
766
|
-
ttl: options?.ttl,
|
|
767
|
-
maxAttempts: options?.maxAttempts
|
|
768
|
-
}
|
|
769
|
-
}, {
|
|
770
|
-
spanParentAsLink: true
|
|
771
|
-
}, {
|
|
772
|
-
name: taskMetadata ? `${taskMetadata.exportName}.trigger()` : `trigger()`,
|
|
773
|
-
tracer,
|
|
774
|
-
icon: "trigger",
|
|
775
|
-
attributes: {
|
|
776
|
-
[SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
777
|
-
["messaging.client_id"]: taskContext.worker?.id,
|
|
778
|
-
[SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
779
|
-
[SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
780
|
-
...accessoryAttributes({
|
|
781
|
-
items: [
|
|
782
|
-
{
|
|
783
|
-
text: params.id,
|
|
784
|
-
variant: "normal"
|
|
785
|
-
}
|
|
786
|
-
],
|
|
787
|
-
style: "codepath"
|
|
788
|
-
})
|
|
789
|
-
},
|
|
790
|
-
onResponseBody: (body, span) => {
|
|
791
|
-
body && typeof body === "object" && !Array.isArray(body) && "id" in body && typeof body.id === "string" && span.setAttribute("messaging.message.id", body.id);
|
|
792
|
-
}
|
|
793
|
-
});
|
|
794
|
-
return handle;
|
|
752
|
+
return await trigger_internal(taskMetadata && taskMetadata.exportName ? `${taskMetadata.exportName}.trigger()` : `trigger()`, params.id, payload, options);
|
|
795
753
|
},
|
|
796
754
|
batchTrigger: async (items) => {
|
|
797
|
-
const apiClient = apiClientManager.client;
|
|
798
|
-
if (!apiClient) {
|
|
799
|
-
throw apiClientMissingError();
|
|
800
|
-
}
|
|
801
755
|
const taskMetadata = taskCatalog.getTaskMetadata(params.id);
|
|
802
|
-
|
|
803
|
-
items: await Promise.all(items.map(async (item) => {
|
|
804
|
-
const payloadPacket = await stringifyIO(item.payload);
|
|
805
|
-
return {
|
|
806
|
-
payload: payloadPacket.data,
|
|
807
|
-
options: {
|
|
808
|
-
queue: item.options?.queue ?? params.queue,
|
|
809
|
-
concurrencyKey: item.options?.concurrencyKey,
|
|
810
|
-
test: taskContext.ctx?.run.isTest,
|
|
811
|
-
payloadType: payloadPacket.dataType,
|
|
812
|
-
idempotencyKey: await makeKey(item.options?.idempotencyKey),
|
|
813
|
-
delay: item.options?.delay,
|
|
814
|
-
ttl: item.options?.ttl,
|
|
815
|
-
maxAttempts: item.options?.maxAttempts
|
|
816
|
-
}
|
|
817
|
-
};
|
|
818
|
-
}))
|
|
819
|
-
}, {
|
|
820
|
-
spanParentAsLink: true
|
|
821
|
-
}, {
|
|
822
|
-
name: taskMetadata ? `${taskMetadata.exportName}.batchTrigger()` : `batchTrigger()`,
|
|
823
|
-
icon: "trigger",
|
|
824
|
-
tracer,
|
|
825
|
-
attributes: {
|
|
826
|
-
[SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
827
|
-
["messaging.batch.message_count"]: items.length,
|
|
828
|
-
["messaging.client_id"]: taskContext.worker?.id,
|
|
829
|
-
[SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
830
|
-
[SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
831
|
-
...accessoryAttributes({
|
|
832
|
-
items: [
|
|
833
|
-
{
|
|
834
|
-
text: params.id,
|
|
835
|
-
variant: "normal"
|
|
836
|
-
}
|
|
837
|
-
],
|
|
838
|
-
style: "codepath"
|
|
839
|
-
})
|
|
840
|
-
}
|
|
841
|
-
});
|
|
842
|
-
const handle = {
|
|
843
|
-
batchId: response.batchId,
|
|
844
|
-
runs: response.runs.map((id) => ({
|
|
845
|
-
id
|
|
846
|
-
}))
|
|
847
|
-
};
|
|
848
|
-
return handle;
|
|
756
|
+
return await batchTrigger_internal(taskMetadata && taskMetadata.exportName ? `${taskMetadata.exportName}.batchTrigger()` : `batchTrigger()`, params.id, items);
|
|
849
757
|
},
|
|
850
758
|
triggerAndWait: async (payload, options) => {
|
|
851
|
-
const ctx = taskContext.ctx;
|
|
852
|
-
if (!ctx) {
|
|
853
|
-
throw new Error("triggerAndWait can only be used from inside a task.run()");
|
|
854
|
-
}
|
|
855
|
-
const apiClient = apiClientManager.client;
|
|
856
|
-
if (!apiClient) {
|
|
857
|
-
throw apiClientMissingError();
|
|
858
|
-
}
|
|
859
759
|
const taskMetadata = taskCatalog.getTaskMetadata(params.id);
|
|
860
|
-
|
|
861
|
-
return await tracer.startActiveSpan(taskMetadata ? `${taskMetadata.exportName}.triggerAndWait()` : `triggerAndWait()`, async (span) => {
|
|
862
|
-
const response = await apiClient.triggerTask(params.id, {
|
|
863
|
-
payload: payloadPacket.data,
|
|
864
|
-
options: {
|
|
865
|
-
dependentAttempt: ctx.attempt.id,
|
|
866
|
-
lockToVersion: taskContext.worker?.version,
|
|
867
|
-
queue: options?.queue ?? params.queue,
|
|
868
|
-
concurrencyKey: options?.concurrencyKey,
|
|
869
|
-
test: taskContext.ctx?.run.isTest,
|
|
870
|
-
payloadType: payloadPacket.dataType,
|
|
871
|
-
idempotencyKey: await makeKey(options?.idempotencyKey),
|
|
872
|
-
delay: options?.delay,
|
|
873
|
-
ttl: options?.ttl,
|
|
874
|
-
maxAttempts: options?.maxAttempts
|
|
875
|
-
}
|
|
876
|
-
});
|
|
877
|
-
span.setAttribute("messaging.message.id", response.id);
|
|
878
|
-
if (options?.idempotencyKey) {
|
|
879
|
-
const result2 = await apiClient.getRunResult(response.id);
|
|
880
|
-
if (result2) {
|
|
881
|
-
logger.log(`Result reused from previous task run with idempotency key '${options.idempotencyKey}'.`, {
|
|
882
|
-
runId: response.id,
|
|
883
|
-
idempotencyKey: options.idempotencyKey
|
|
884
|
-
});
|
|
885
|
-
return await handleTaskRunExecutionResult(result2);
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
const result = await runtime.waitForTask({
|
|
889
|
-
id: response.id,
|
|
890
|
-
ctx
|
|
891
|
-
});
|
|
892
|
-
return await handleTaskRunExecutionResult(result);
|
|
893
|
-
}, {
|
|
894
|
-
kind: SpanKind.PRODUCER,
|
|
895
|
-
attributes: {
|
|
896
|
-
[SemanticInternalAttributes.STYLE_ICON]: "trigger",
|
|
897
|
-
[SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
898
|
-
["messaging.client_id"]: taskContext.worker?.id,
|
|
899
|
-
[SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
900
|
-
[SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
901
|
-
...accessoryAttributes({
|
|
902
|
-
items: [
|
|
903
|
-
{
|
|
904
|
-
text: params.id,
|
|
905
|
-
variant: "normal"
|
|
906
|
-
}
|
|
907
|
-
],
|
|
908
|
-
style: "codepath"
|
|
909
|
-
})
|
|
910
|
-
}
|
|
911
|
-
});
|
|
760
|
+
return await triggerAndWait_internal(taskMetadata && taskMetadata.exportName ? `${taskMetadata.exportName}.triggerAndWait()` : `triggerAndWait()`, params.id, payload, options);
|
|
912
761
|
},
|
|
913
762
|
batchTriggerAndWait: async (items) => {
|
|
914
|
-
const ctx = taskContext.ctx;
|
|
915
|
-
if (!ctx) {
|
|
916
|
-
throw new Error("batchTriggerAndWait can only be used from inside a task.run()");
|
|
917
|
-
}
|
|
918
|
-
const apiClient = apiClientManager.client;
|
|
919
|
-
if (!apiClient) {
|
|
920
|
-
throw apiClientMissingError();
|
|
921
|
-
}
|
|
922
763
|
const taskMetadata = taskCatalog.getTaskMetadata(params.id);
|
|
923
|
-
return await
|
|
924
|
-
const response = await apiClient.batchTriggerTask(params.id, {
|
|
925
|
-
items: await Promise.all(items.map(async (item) => {
|
|
926
|
-
const payloadPacket = await stringifyIO(item.payload);
|
|
927
|
-
return {
|
|
928
|
-
payload: payloadPacket.data,
|
|
929
|
-
options: {
|
|
930
|
-
lockToVersion: taskContext.worker?.version,
|
|
931
|
-
queue: item.options?.queue ?? params.queue,
|
|
932
|
-
concurrencyKey: item.options?.concurrencyKey,
|
|
933
|
-
test: taskContext.ctx?.run.isTest,
|
|
934
|
-
payloadType: payloadPacket.dataType,
|
|
935
|
-
idempotencyKey: await makeKey(item.options?.idempotencyKey),
|
|
936
|
-
delay: item.options?.delay,
|
|
937
|
-
ttl: item.options?.ttl,
|
|
938
|
-
maxAttempts: item.options?.maxAttempts
|
|
939
|
-
}
|
|
940
|
-
};
|
|
941
|
-
})),
|
|
942
|
-
dependentAttempt: ctx.attempt.id
|
|
943
|
-
});
|
|
944
|
-
span.setAttribute("messaging.message.id", response.batchId);
|
|
945
|
-
const getBatchResults = /* @__PURE__ */ __name(async () => {
|
|
946
|
-
const hasIdempotencyKey = items.some((item) => item.options?.idempotencyKey);
|
|
947
|
-
if (hasIdempotencyKey) {
|
|
948
|
-
const results = await apiClient.getBatchResults(response.batchId);
|
|
949
|
-
if (results) {
|
|
950
|
-
return results;
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
return {
|
|
954
|
-
id: response.batchId,
|
|
955
|
-
items: []
|
|
956
|
-
};
|
|
957
|
-
}, "getBatchResults");
|
|
958
|
-
const existingResults = await getBatchResults();
|
|
959
|
-
const incompleteRuns = response.runs.filter((runId) => !existingResults.items.some((item) => item.id === runId));
|
|
960
|
-
if (incompleteRuns.length === 0) {
|
|
961
|
-
logger.log(`Results reused from previous task runs because of the provided idempotency keys.`);
|
|
962
|
-
const runs3 = await handleBatchTaskRunExecutionResult(existingResults.items);
|
|
963
|
-
return {
|
|
964
|
-
id: existingResults.id,
|
|
965
|
-
runs: runs3
|
|
966
|
-
};
|
|
967
|
-
}
|
|
968
|
-
const result = await runtime.waitForBatch({
|
|
969
|
-
id: response.batchId,
|
|
970
|
-
runs: incompleteRuns,
|
|
971
|
-
ctx
|
|
972
|
-
});
|
|
973
|
-
const combinedItems = [];
|
|
974
|
-
for (const runId of response.runs) {
|
|
975
|
-
const existingItem = existingResults.items.find((item) => item.id === runId);
|
|
976
|
-
if (existingItem) {
|
|
977
|
-
combinedItems.push(existingItem);
|
|
978
|
-
} else {
|
|
979
|
-
const newItem = result.items.find((item) => item.id === runId);
|
|
980
|
-
if (newItem) {
|
|
981
|
-
combinedItems.push(newItem);
|
|
982
|
-
}
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
const runs2 = await handleBatchTaskRunExecutionResult(combinedItems);
|
|
986
|
-
return {
|
|
987
|
-
id: result.id,
|
|
988
|
-
runs: runs2
|
|
989
|
-
};
|
|
990
|
-
}, {
|
|
991
|
-
kind: SpanKind.PRODUCER,
|
|
992
|
-
attributes: {
|
|
993
|
-
[SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
994
|
-
["messaging.batch.message_count"]: items.length,
|
|
995
|
-
["messaging.client_id"]: taskContext.worker?.id,
|
|
996
|
-
[SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
|
|
997
|
-
[SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
998
|
-
[SemanticInternalAttributes.STYLE_ICON]: "trigger",
|
|
999
|
-
...accessoryAttributes({
|
|
1000
|
-
items: [
|
|
1001
|
-
{
|
|
1002
|
-
text: params.id,
|
|
1003
|
-
variant: "normal"
|
|
1004
|
-
}
|
|
1005
|
-
],
|
|
1006
|
-
style: "codepath"
|
|
1007
|
-
})
|
|
1008
|
-
}
|
|
1009
|
-
});
|
|
764
|
+
return await batchTriggerAndWait_internal(taskMetadata && taskMetadata.exportName ? `${taskMetadata.exportName}.batchTriggerAndWait()` : `batchTriggerAndWait()`, params.id, items);
|
|
1010
765
|
}
|
|
1011
766
|
};
|
|
1012
767
|
taskCatalog.registerTaskMetadata({
|
|
@@ -1033,6 +788,27 @@ function createTask(params) {
|
|
|
1033
788
|
}
|
|
1034
789
|
__name(createTask, "createTask");
|
|
1035
790
|
async function trigger(id, payload, options, requestOptions) {
|
|
791
|
+
return await trigger_internal("tasks.trigger()", id, payload, options, requestOptions);
|
|
792
|
+
}
|
|
793
|
+
__name(trigger, "trigger");
|
|
794
|
+
async function triggerAndWait(id, payload, options, requestOptions) {
|
|
795
|
+
return await triggerAndWait_internal("tasks.triggerAndWait()", id, payload, options, requestOptions);
|
|
796
|
+
}
|
|
797
|
+
__name(triggerAndWait, "triggerAndWait");
|
|
798
|
+
async function batchTriggerAndWait(id, items, requestOptions) {
|
|
799
|
+
return await batchTriggerAndWait_internal("tasks.batchTriggerAndWait()", id, items, requestOptions);
|
|
800
|
+
}
|
|
801
|
+
__name(batchTriggerAndWait, "batchTriggerAndWait");
|
|
802
|
+
async function triggerAndPoll(id, payload, options, requestOptions) {
|
|
803
|
+
const handle = await trigger(id, payload, options, requestOptions);
|
|
804
|
+
return runs.poll(handle, options, requestOptions);
|
|
805
|
+
}
|
|
806
|
+
__name(triggerAndPoll, "triggerAndPoll");
|
|
807
|
+
async function batchTrigger(id, items, requestOptions) {
|
|
808
|
+
return await batchTrigger_internal("tasks.batchTrigger()", id, items, requestOptions);
|
|
809
|
+
}
|
|
810
|
+
__name(batchTrigger, "batchTrigger");
|
|
811
|
+
async function trigger_internal(name2, id, payload, options, requestOptions) {
|
|
1036
812
|
const apiClient = apiClientManager.client;
|
|
1037
813
|
if (!apiClient) {
|
|
1038
814
|
throw apiClientMissingError();
|
|
@@ -1048,12 +824,13 @@ async function trigger(id, payload, options, requestOptions) {
|
|
|
1048
824
|
idempotencyKey: await makeKey(options?.idempotencyKey),
|
|
1049
825
|
delay: options?.delay,
|
|
1050
826
|
ttl: options?.ttl,
|
|
827
|
+
tags: options?.tags,
|
|
1051
828
|
maxAttempts: options?.maxAttempts
|
|
1052
829
|
}
|
|
1053
830
|
}, {
|
|
1054
831
|
spanParentAsLink: true
|
|
1055
832
|
}, {
|
|
1056
|
-
name:
|
|
833
|
+
name: name2,
|
|
1057
834
|
tracer,
|
|
1058
835
|
icon: "trigger",
|
|
1059
836
|
attributes: {
|
|
@@ -1077,18 +854,72 @@ async function trigger(id, payload, options, requestOptions) {
|
|
|
1077
854
|
});
|
|
1078
855
|
return handle;
|
|
1079
856
|
}
|
|
1080
|
-
__name(
|
|
1081
|
-
async function
|
|
857
|
+
__name(trigger_internal, "trigger_internal");
|
|
858
|
+
async function batchTrigger_internal(name2, id, items, requestOptions) {
|
|
859
|
+
const apiClient = apiClientManager.client;
|
|
860
|
+
if (!apiClient) {
|
|
861
|
+
throw apiClientMissingError();
|
|
862
|
+
}
|
|
863
|
+
const response = await apiClient.batchTriggerTask(id, {
|
|
864
|
+
items: await Promise.all(items.map(async (item) => {
|
|
865
|
+
const payloadPacket = await stringifyIO(item.payload);
|
|
866
|
+
return {
|
|
867
|
+
payload: payloadPacket.data,
|
|
868
|
+
options: {
|
|
869
|
+
queue: item.options?.queue,
|
|
870
|
+
concurrencyKey: item.options?.concurrencyKey,
|
|
871
|
+
test: taskContext.ctx?.run.isTest,
|
|
872
|
+
payloadType: payloadPacket.dataType,
|
|
873
|
+
idempotencyKey: await makeKey(item.options?.idempotencyKey),
|
|
874
|
+
delay: item.options?.delay,
|
|
875
|
+
ttl: item.options?.ttl,
|
|
876
|
+
tags: item.options?.tags,
|
|
877
|
+
maxAttempts: item.options?.maxAttempts
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
}))
|
|
881
|
+
}, {
|
|
882
|
+
spanParentAsLink: true
|
|
883
|
+
}, {
|
|
884
|
+
name: name2,
|
|
885
|
+
tracer,
|
|
886
|
+
icon: "trigger",
|
|
887
|
+
attributes: {
|
|
888
|
+
[SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
889
|
+
["messaging.client_id"]: taskContext.worker?.id,
|
|
890
|
+
[SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
891
|
+
...accessoryAttributes({
|
|
892
|
+
items: [
|
|
893
|
+
{
|
|
894
|
+
text: id,
|
|
895
|
+
variant: "normal"
|
|
896
|
+
}
|
|
897
|
+
],
|
|
898
|
+
style: "codepath"
|
|
899
|
+
})
|
|
900
|
+
},
|
|
901
|
+
...requestOptions
|
|
902
|
+
});
|
|
903
|
+
const handle = {
|
|
904
|
+
batchId: response.batchId,
|
|
905
|
+
runs: response.runs.map((id2) => ({
|
|
906
|
+
id: id2
|
|
907
|
+
}))
|
|
908
|
+
};
|
|
909
|
+
return handle;
|
|
910
|
+
}
|
|
911
|
+
__name(batchTrigger_internal, "batchTrigger_internal");
|
|
912
|
+
async function triggerAndWait_internal(name2, id, payload, options, requestOptions) {
|
|
1082
913
|
const ctx = taskContext.ctx;
|
|
1083
914
|
if (!ctx) {
|
|
1084
|
-
throw new Error("
|
|
915
|
+
throw new Error("triggerAndWait can only be used from inside a task.run()");
|
|
1085
916
|
}
|
|
1086
917
|
const apiClient = apiClientManager.client;
|
|
1087
918
|
if (!apiClient) {
|
|
1088
919
|
throw apiClientMissingError();
|
|
1089
920
|
}
|
|
1090
921
|
const payloadPacket = await stringifyIO(payload);
|
|
1091
|
-
return await tracer.startActiveSpan(
|
|
922
|
+
return await tracer.startActiveSpan(name2, async (span) => {
|
|
1092
923
|
const response = await apiClient.triggerTask(id, {
|
|
1093
924
|
payload: payloadPacket.data,
|
|
1094
925
|
options: {
|
|
@@ -1101,6 +932,7 @@ async function triggerAndWait(id, payload, options, requestOptions) {
|
|
|
1101
932
|
idempotencyKey: await makeKey(options?.idempotencyKey),
|
|
1102
933
|
delay: options?.delay,
|
|
1103
934
|
ttl: options?.ttl,
|
|
935
|
+
tags: options?.tags,
|
|
1104
936
|
maxAttempts: options?.maxAttempts
|
|
1105
937
|
}
|
|
1106
938
|
}, {}, requestOptions);
|
|
@@ -1140,43 +972,92 @@ async function triggerAndWait(id, payload, options, requestOptions) {
|
|
|
1140
972
|
}
|
|
1141
973
|
});
|
|
1142
974
|
}
|
|
1143
|
-
__name(
|
|
1144
|
-
async function
|
|
1145
|
-
const
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
async function batchTrigger(id, items, requestOptions) {
|
|
975
|
+
__name(triggerAndWait_internal, "triggerAndWait_internal");
|
|
976
|
+
async function batchTriggerAndWait_internal(name2, id, items, requestOptions) {
|
|
977
|
+
const ctx = taskContext.ctx;
|
|
978
|
+
if (!ctx) {
|
|
979
|
+
throw new Error("batchTriggerAndWait can only be used from inside a task.run()");
|
|
980
|
+
}
|
|
1150
981
|
const apiClient = apiClientManager.client;
|
|
1151
982
|
if (!apiClient) {
|
|
1152
983
|
throw apiClientMissingError();
|
|
1153
984
|
}
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
985
|
+
return await tracer.startActiveSpan(name2, async (span) => {
|
|
986
|
+
const response = await apiClient.batchTriggerTask(id, {
|
|
987
|
+
items: await Promise.all(items.map(async (item) => {
|
|
988
|
+
const payloadPacket = await stringifyIO(item.payload);
|
|
989
|
+
return {
|
|
990
|
+
payload: payloadPacket.data,
|
|
991
|
+
options: {
|
|
992
|
+
lockToVersion: taskContext.worker?.version,
|
|
993
|
+
queue: item.options?.queue,
|
|
994
|
+
concurrencyKey: item.options?.concurrencyKey,
|
|
995
|
+
test: taskContext.ctx?.run.isTest,
|
|
996
|
+
payloadType: payloadPacket.dataType,
|
|
997
|
+
idempotencyKey: await makeKey(item.options?.idempotencyKey),
|
|
998
|
+
delay: item.options?.delay,
|
|
999
|
+
ttl: item.options?.ttl,
|
|
1000
|
+
tags: item.options?.tags,
|
|
1001
|
+
maxAttempts: item.options?.maxAttempts
|
|
1002
|
+
}
|
|
1003
|
+
};
|
|
1004
|
+
})),
|
|
1005
|
+
dependentAttempt: ctx.attempt.id
|
|
1006
|
+
}, {}, requestOptions);
|
|
1007
|
+
span.setAttribute("messaging.message.id", response.batchId);
|
|
1008
|
+
const getBatchResults = /* @__PURE__ */ __name(async () => {
|
|
1009
|
+
const hasIdempotencyKey = items.some((item) => item.options?.idempotencyKey);
|
|
1010
|
+
if (hasIdempotencyKey) {
|
|
1011
|
+
const results = await apiClient.getBatchResults(response.batchId);
|
|
1012
|
+
if (results) {
|
|
1013
|
+
return results;
|
|
1168
1014
|
}
|
|
1015
|
+
}
|
|
1016
|
+
return {
|
|
1017
|
+
id: response.batchId,
|
|
1018
|
+
items: []
|
|
1169
1019
|
};
|
|
1170
|
-
})
|
|
1171
|
-
|
|
1172
|
-
|
|
1020
|
+
}, "getBatchResults");
|
|
1021
|
+
const existingResults = await getBatchResults();
|
|
1022
|
+
const incompleteRuns = response.runs.filter((runId) => !existingResults.items.some((item) => item.id === runId));
|
|
1023
|
+
if (incompleteRuns.length === 0) {
|
|
1024
|
+
logger.log(`Results reused from previous task runs because of the provided idempotency keys.`);
|
|
1025
|
+
const runs3 = await handleBatchTaskRunExecutionResult(existingResults.items);
|
|
1026
|
+
return {
|
|
1027
|
+
id: existingResults.id,
|
|
1028
|
+
runs: runs3
|
|
1029
|
+
};
|
|
1030
|
+
}
|
|
1031
|
+
const result = await runtime.waitForBatch({
|
|
1032
|
+
id: response.batchId,
|
|
1033
|
+
runs: incompleteRuns,
|
|
1034
|
+
ctx
|
|
1035
|
+
});
|
|
1036
|
+
const combinedItems = [];
|
|
1037
|
+
for (const runId of response.runs) {
|
|
1038
|
+
const existingItem = existingResults.items.find((item) => item.id === runId);
|
|
1039
|
+
if (existingItem) {
|
|
1040
|
+
combinedItems.push(existingItem);
|
|
1041
|
+
} else {
|
|
1042
|
+
const newItem = result.items.find((item) => item.id === runId);
|
|
1043
|
+
if (newItem) {
|
|
1044
|
+
combinedItems.push(newItem);
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
const runs2 = await handleBatchTaskRunExecutionResult(combinedItems);
|
|
1049
|
+
return {
|
|
1050
|
+
id: result.id,
|
|
1051
|
+
runs: runs2
|
|
1052
|
+
};
|
|
1173
1053
|
}, {
|
|
1174
|
-
|
|
1175
|
-
tracer,
|
|
1176
|
-
icon: "trigger",
|
|
1054
|
+
kind: SpanKind.PRODUCER,
|
|
1177
1055
|
attributes: {
|
|
1056
|
+
[SemanticInternalAttributes.STYLE_ICON]: "trigger",
|
|
1057
|
+
["messaging.batch.message_count"]: items.length,
|
|
1178
1058
|
[SEMATTRS_MESSAGING_OPERATION]: "publish",
|
|
1179
1059
|
["messaging.client_id"]: taskContext.worker?.id,
|
|
1060
|
+
[SEMATTRS_MESSAGING_DESTINATION]: id,
|
|
1180
1061
|
[SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
|
|
1181
1062
|
...accessoryAttributes({
|
|
1182
1063
|
items: [
|
|
@@ -1187,18 +1068,10 @@ async function batchTrigger(id, items, requestOptions) {
|
|
|
1187
1068
|
],
|
|
1188
1069
|
style: "codepath"
|
|
1189
1070
|
})
|
|
1190
|
-
}
|
|
1191
|
-
...requestOptions
|
|
1071
|
+
}
|
|
1192
1072
|
});
|
|
1193
|
-
const handle = {
|
|
1194
|
-
batchId: response.batchId,
|
|
1195
|
-
runs: response.runs.map((id2) => ({
|
|
1196
|
-
id: id2
|
|
1197
|
-
}))
|
|
1198
|
-
};
|
|
1199
|
-
return handle;
|
|
1200
1073
|
}
|
|
1201
|
-
__name(
|
|
1074
|
+
__name(batchTriggerAndWait_internal, "batchTriggerAndWait_internal");
|
|
1202
1075
|
async function handleBatchTaskRunExecutionResult(items) {
|
|
1203
1076
|
const someObjectStoreOutputs = items.some((item) => item.ok && item.outputType === "application/store");
|
|
1204
1077
|
if (!someObjectStoreOutputs) {
|
|
@@ -1274,7 +1147,8 @@ var tasks = {
|
|
|
1274
1147
|
trigger,
|
|
1275
1148
|
triggerAndPoll,
|
|
1276
1149
|
batchTrigger,
|
|
1277
|
-
triggerAndWait
|
|
1150
|
+
triggerAndWait,
|
|
1151
|
+
batchTriggerAndWait
|
|
1278
1152
|
};
|
|
1279
1153
|
var wait = {
|
|
1280
1154
|
for: async (options) => {
|
|
@@ -1480,6 +1354,53 @@ var usage = {
|
|
|
1480
1354
|
};
|
|
1481
1355
|
}
|
|
1482
1356
|
};
|
|
1357
|
+
var tags = {
|
|
1358
|
+
add: addTags
|
|
1359
|
+
};
|
|
1360
|
+
async function addTags(tags2, requestOptions) {
|
|
1361
|
+
const apiClient = apiClientManager.client;
|
|
1362
|
+
if (!apiClient) {
|
|
1363
|
+
throw apiClientMissingError();
|
|
1364
|
+
}
|
|
1365
|
+
const run = taskContext.ctx?.run;
|
|
1366
|
+
if (!run) {
|
|
1367
|
+
throw new Error("Can't set tags outside of a run. You can trigger a task and set tags in the options.");
|
|
1368
|
+
}
|
|
1369
|
+
const $requestOptions = mergeRequestOptions({
|
|
1370
|
+
tracer,
|
|
1371
|
+
name: "tags.set()",
|
|
1372
|
+
icon: "tag",
|
|
1373
|
+
attributes: {
|
|
1374
|
+
...accessoryAttributes({
|
|
1375
|
+
items: [
|
|
1376
|
+
{
|
|
1377
|
+
text: typeof tags2 === "string" ? tags2 : tags2.join(", "),
|
|
1378
|
+
variant: "normal"
|
|
1379
|
+
}
|
|
1380
|
+
],
|
|
1381
|
+
style: "codepath"
|
|
1382
|
+
})
|
|
1383
|
+
}
|
|
1384
|
+
}, requestOptions);
|
|
1385
|
+
try {
|
|
1386
|
+
await apiClient.addTags(run.id, {
|
|
1387
|
+
tags: tags2
|
|
1388
|
+
}, $requestOptions);
|
|
1389
|
+
} catch (error) {
|
|
1390
|
+
if (error instanceof UnprocessableEntityError) {
|
|
1391
|
+
logger.error(error.message, {
|
|
1392
|
+
existingTags: run.tags,
|
|
1393
|
+
newTags: tags2
|
|
1394
|
+
});
|
|
1395
|
+
return;
|
|
1396
|
+
}
|
|
1397
|
+
logger.error("Failed to set tags", {
|
|
1398
|
+
error
|
|
1399
|
+
});
|
|
1400
|
+
throw error;
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
__name(addTags, "addTags");
|
|
1483
1404
|
|
|
1484
1405
|
// src/v3/schedules/index.ts
|
|
1485
1406
|
var schedules_exports = {};
|
|
@@ -1914,6 +1835,6 @@ function configure(options) {
|
|
|
1914
1835
|
}
|
|
1915
1836
|
__name(configure, "configure");
|
|
1916
1837
|
|
|
1917
|
-
export { InMemoryCache, configure, createCache, envvars_exports as envvars, idempotencyKeys, isIdempotencyKey, queue, retry, runs, schedules_exports as schedules, task, tasks, usage, wait };
|
|
1838
|
+
export { InMemoryCache, configure, createCache, envvars_exports as envvars, idempotencyKeys, isIdempotencyKey, queue, retry, runs, schedules_exports as schedules, tags, task, tasks, usage, wait };
|
|
1918
1839
|
//# sourceMappingURL=out.js.map
|
|
1919
1840
|
//# sourceMappingURL=index.mjs.map
|