File tree Expand file tree Collapse file tree 7 files changed +35
-7
lines changed
internal-packages/run-engine/src/engine
references/hello-world/src/trigger Expand file tree Collapse file tree 7 files changed +35
-7
lines changed Original file line number Diff line number Diff line change 11import { json } from "@remix-run/server-runtime" ;
22import { CreateWaitpointRequestBody , CreateWaitpointResponseBody } from "@trigger.dev/core/v3" ;
3+ import { ResumeTokenId , WaitpointId } from "@trigger.dev/core/v3/apps" ;
34import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server" ;
45import { parseDelay } from "~/utils/delays" ;
56import { resolveIdempotencyKeyTTL } from "~/utils/idempotencyKeys.server" ;
@@ -18,17 +19,21 @@ const { action } = createActionApiRoute(
1819
1920 const timeout = await parseDelay ( body . timeout ) ;
2021
21- const waitpoint = await engine . createManualWaitpoint ( {
22+ const result = await engine . createManualWaitpoint ( {
2223 environmentId : authentication . environment . id ,
2324 projectId : authentication . environment . projectId ,
2425 idempotencyKey : body . idempotencyKey ,
2526 idempotencyKeyExpiresAt,
2627 timeout,
2728 } ) ;
2829
30+ //result is a waitpoint but we want to make it look like a resume token
31+ const resumeTokenFriendlyId = ResumeTokenId . toFriendlyId ( result . waitpoint . id ) ;
32+
2933 return json < CreateWaitpointResponseBody > (
3034 {
31- id : waitpoint . friendlyId ,
35+ id : resumeTokenFriendlyId ,
36+ isCached : result . isCached ,
3237 } ,
3338 { status : 200 }
3439 ) ;
Original file line number Diff line number Diff line change @@ -1744,7 +1744,7 @@ export class RunEngine {
17441744 idempotencyKey ?: string ;
17451745 idempotencyKeyExpiresAt ?: Date ;
17461746 timeout ?: Date ;
1747- } ) : Promise < Waitpoint > {
1747+ } ) : Promise < { waitpoint : Waitpoint ; isCached : boolean } > {
17481748 const existingWaitpoint = idempotencyKey
17491749 ? await this . prisma . waitpoint . findUnique ( {
17501750 where : {
@@ -1775,7 +1775,7 @@ export class RunEngine {
17751775
17761776 //let it fall through to create a new waitpoint
17771777 } else {
1778- return existingWaitpoint ;
1778+ return { waitpoint : existingWaitpoint , isCached : true } ;
17791779 }
17801780 }
17811781
@@ -1815,7 +1815,7 @@ export class RunEngine {
18151815 } ) ;
18161816 }
18171817
1818- return waitpoint ;
1818+ return { waitpoint, isCached : false } ;
18191819 }
18201820
18211821 /** This block a run with a BATCH waitpoint.
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ export type ZodFetchOptions<T = unknown> = {
3232 name ?: string ;
3333 attributes ?: Attributes ;
3434 icon ?: string ;
35- onResponseBody ?: ( body : unknown , span : Span ) => void ;
35+ onResponseBody ?: ( body : T , span : Span ) => void ;
3636 prepareData ?: ( data : T ) => Promise < T > | T ;
3737} ;
3838
Original file line number Diff line number Diff line change @@ -90,3 +90,4 @@ export const RunId = new IdUtil("run");
9090export const SnapshotId = new IdUtil ( "snapshot" ) ;
9191export const WaitpointId = new IdUtil ( "waitpoint" ) ;
9292export const BatchId = new IdUtil ( "batch" ) ;
93+ export const ResumeTokenId = new IdUtil ( "resumetoken" ) ;
Original file line number Diff line number Diff line change @@ -914,6 +914,7 @@ export type CreateWaitpointRequestBody = z.infer<typeof CreateWaitpointRequestBo
914914
915915export const CreateWaitpointResponseBody = z . object ( {
916916 id : z . string ( ) ,
917+ isCached : z . boolean ( ) ,
917918} ) ;
918919export type CreateWaitpointResponseBody = z . infer < typeof CreateWaitpointResponseBody > ;
919920
Original file line number Diff line number Diff line change @@ -19,6 +19,19 @@ function createResumeToken(
1919 tracer,
2020 name : "resumeTokens.create()" ,
2121 icon : "wait" ,
22+ attributes : {
23+ idempotencyKey : options ?. idempotencyKey ,
24+ idempotencyKeyTTL : options ?. idempotencyKeyTTL ,
25+ timeout : options ?. timeout
26+ ? typeof options . timeout === "string"
27+ ? options . timeout
28+ : options . timeout . toISOString ( )
29+ : undefined ,
30+ } ,
31+ onResponseBody : ( body : CreateWaitpointResponseBody , span ) => {
32+ span . setAttribute ( "id" , body . id ) ;
33+ span . setAttribute ( "isCached" , body . isCached ) ;
34+ } ,
2235 } ,
2336 requestOptions
2437 ) ;
Original file line number Diff line number Diff line change @@ -5,7 +5,15 @@ export const resumeToken = task({
55 run : async ( payload : any , { ctx } ) => {
66 logger . log ( "Hello, world" , { payload } ) ;
77
8- const token = await resumeTokens . create ( ) ;
8+ const idempotencyKey = "a" ;
9+
10+ const token = await resumeTokens . create ( {
11+ idempotencyKey,
12+ timeout : new Date ( Date . now ( ) + 5_000 ) ,
13+ } ) ;
914 logger . log ( "Token" , token ) ;
15+
16+ const token2 = await resumeTokens . create ( { idempotencyKey, timeout : "10s" } ) ;
17+ logger . log ( "Token2" , token2 ) ;
1018 } ,
1119} ) ;
You can’t perform that action at this time.
0 commit comments