1- import { CompletedWaitpoint , ExecutionResult } from "@trigger.dev/core/v3" ;
1+ import { CompletedWaitpoint , ExecutionResult , RunExecutionData } from "@trigger.dev/core/v3" ;
22import { BatchId , RunId , SnapshotId } from "@trigger.dev/core/v3/isomorphic" ;
33import {
44 Prisma ,
@@ -17,31 +17,23 @@ export type ExecutionSnapshotSystemOptions = {
1717 heartbeatTimeouts : HeartbeatTimeouts ;
1818} ;
1919
20- export interface LatestExecutionSnapshot extends TaskRunExecutionSnapshot {
20+ export interface EnhancedExecutionSnapshot extends TaskRunExecutionSnapshot {
2121 friendlyId : string ;
2222 runFriendlyId : string ;
2323 checkpoint : TaskRunCheckpoint | null ;
2424 completedWaitpoints : CompletedWaitpoint [ ] ;
2525}
2626
27- /* Gets the most recent valid snapshot for a run */
28- export async function getLatestExecutionSnapshot (
29- prisma : PrismaClientOrTransaction ,
30- runId : string
31- ) : Promise < LatestExecutionSnapshot > {
32- const snapshot = await prisma . taskRunExecutionSnapshot . findFirst ( {
33- where : { runId, isValid : true } ,
34- include : {
35- completedWaitpoints : true ,
36- checkpoint : true ,
37- } ,
38- orderBy : { createdAt : "desc" } ,
39- } ) ;
40-
41- if ( ! snapshot ) {
42- throw new Error ( `No execution snapshot found for TaskRun ${ runId } ` ) ;
43- }
27+ type ExecutionSnapshotWithCheckAndWaitpoints = Prisma . TaskRunExecutionSnapshotGetPayload < {
28+ include : {
29+ checkpoint : true ;
30+ completedWaitpoints : true ;
31+ } ;
32+ } > ;
4433
34+ function enhanceExecutionSnapshot (
35+ snapshot : ExecutionSnapshotWithCheckAndWaitpoints
36+ ) : EnhancedExecutionSnapshot {
4537 return {
4638 ...snapshot ,
4739 friendlyId : SnapshotId . toFriendlyId ( snapshot . id ) ,
@@ -99,6 +91,27 @@ export async function getLatestExecutionSnapshot(
9991 } ;
10092}
10193
94+ /* Gets the most recent valid snapshot for a run */
95+ export async function getLatestExecutionSnapshot (
96+ prisma : PrismaClientOrTransaction ,
97+ runId : string
98+ ) : Promise < EnhancedExecutionSnapshot > {
99+ const snapshot = await prisma . taskRunExecutionSnapshot . findFirst ( {
100+ where : { runId, isValid : true } ,
101+ include : {
102+ completedWaitpoints : true ,
103+ checkpoint : true ,
104+ } ,
105+ orderBy : { createdAt : "desc" } ,
106+ } ) ;
107+
108+ if ( ! snapshot ) {
109+ throw new Error ( `No execution snapshot found for TaskRun ${ runId } ` ) ;
110+ }
111+
112+ return enhanceExecutionSnapshot ( snapshot ) ;
113+ }
114+
102115export async function getExecutionSnapshotCompletedWaitpoints (
103116 prisma : PrismaClientOrTransaction ,
104117 snapshotId : string
@@ -141,6 +154,72 @@ export function executionResultFromSnapshot(snapshot: TaskRunExecutionSnapshot):
141154 } ;
142155}
143156
157+ export function executionDataFromSnapshot ( snapshot : EnhancedExecutionSnapshot ) : RunExecutionData {
158+ return {
159+ version : "1" as const ,
160+ snapshot : {
161+ id : snapshot . id ,
162+ friendlyId : snapshot . friendlyId ,
163+ executionStatus : snapshot . executionStatus ,
164+ description : snapshot . description ,
165+ } ,
166+ run : {
167+ id : snapshot . runId ,
168+ friendlyId : snapshot . runFriendlyId ,
169+ status : snapshot . runStatus ,
170+ attemptNumber : snapshot . attemptNumber ?? undefined ,
171+ } ,
172+ batch : snapshot . batchId
173+ ? {
174+ id : snapshot . batchId ,
175+ friendlyId : BatchId . toFriendlyId ( snapshot . batchId ) ,
176+ }
177+ : undefined ,
178+ checkpoint : snapshot . checkpoint
179+ ? {
180+ id : snapshot . checkpoint . id ,
181+ friendlyId : snapshot . checkpoint . friendlyId ,
182+ type : snapshot . checkpoint . type ,
183+ location : snapshot . checkpoint . location ,
184+ imageRef : snapshot . checkpoint . imageRef ,
185+ reason : snapshot . checkpoint . reason ?? undefined ,
186+ }
187+ : undefined ,
188+ completedWaitpoints : snapshot . completedWaitpoints ,
189+ } ;
190+ }
191+
192+ export async function getExecutionSnapshotsSince (
193+ prisma : PrismaClientOrTransaction ,
194+ runId : string ,
195+ sinceSnapshotId : string
196+ ) : Promise < EnhancedExecutionSnapshot [ ] > {
197+ // Find the createdAt of the sinceSnapshotId
198+ const sinceSnapshot = await prisma . taskRunExecutionSnapshot . findUnique ( {
199+ where : { id : sinceSnapshotId } ,
200+ select : { createdAt : true } ,
201+ } ) ;
202+
203+ if ( ! sinceSnapshot ) {
204+ throw new Error ( `No execution snapshot found for id ${ sinceSnapshotId } ` ) ;
205+ }
206+
207+ const snapshots = await prisma . taskRunExecutionSnapshot . findMany ( {
208+ where : {
209+ runId,
210+ isValid : true ,
211+ createdAt : { gt : sinceSnapshot . createdAt } ,
212+ } ,
213+ include : {
214+ completedWaitpoints : true ,
215+ checkpoint : true ,
216+ } ,
217+ orderBy : { createdAt : "asc" } ,
218+ } ) ;
219+
220+ return snapshots . map ( enhanceExecutionSnapshot ) ;
221+ }
222+
144223export class ExecutionSnapshotSystem {
145224 private readonly $ : SystemResources ;
146225 private readonly heartbeatTimeouts : HeartbeatTimeouts ;
0 commit comments