1- import { singleton } from "~/utils/singleton" ;
21import { ClickHouse } from "@internal/clickhouse" ;
2+ import { EventEmitter } from "node:events" ;
3+ import { prisma } from "~/db.server" ;
4+ import { singleton } from "~/utils/singleton" ;
5+ import { engine } from "~/v3/runEngine.server" ;
6+ import { logger } from "./logger.server" ;
37import {
48 RunDashboardEventBus ,
59 RunDashboardEvents ,
610 RunsDashboardService ,
711} from "./runsDashboardService.server" ;
8- import { EventEmitter } from "node:events" ;
9- import { RuntimeEnvironmentType , TaskRun } from "@trigger.dev/database" ;
10- import { engine } from "~/v3/runEngine.server" ;
11- import { logger } from "./logger.server" ;
1212
1313const runDashboardEventBus : RunDashboardEventBus = new EventEmitter < RunDashboardEvents > ( ) ;
1414
15- export type TaskRunStatusUpdateEnvironment = {
16- type : RuntimeEnvironmentType ;
17- organizationId : string ;
18- } ;
19-
20- export function emitRunStatusUpdate ( run : TaskRun , environment : TaskRunStatusUpdateEnvironment ) {
15+ export function emitRunStatusUpdate ( runId : string ) {
2116 runDashboardEventBus . emit ( "runStatusUpdate" , {
22- run,
23- environment,
24- organization : { id : environment . organizationId } ,
17+ time : new Date ( ) ,
18+ runId,
2519 } ) ;
2620}
2721
@@ -31,16 +25,46 @@ export const runsDashboard = singleton("runsDashboard", () => {
3125 const service = new RunsDashboardService ( clickhouse ) ;
3226
3327 runDashboardEventBus . on ( "runStatusUpdate" , async ( event ) => {
34- await service . upsertRun ( event . run , event . environment . type , event . organization . id ) ;
28+ await upsertRun ( event . time , event . runId , service ) ;
3529 } ) ;
3630
3731 engine . eventBus . on ( "runStatusChanged" , async ( event ) => {
38- logger . debug ( "RunDashboard: runStatusChanged" , {
39- event,
40- } ) ;
41-
42- await service . upsertRun ( event . run , event . environment . type , event . environment . organization . id ) ;
32+ await upsertRun ( event . time , event . runId , service ) ;
4333 } ) ;
4434
4535 return service ;
4636} ) ;
37+
38+ async function upsertRun ( time : Date , runId : string , service : RunsDashboardService ) {
39+ const run = await prisma . taskRun . findFirst ( {
40+ where : {
41+ id : runId ,
42+ } ,
43+ } ) ;
44+
45+ if ( ! run ) {
46+ logger . error ( "RunDashboard: upsertRun: run not found" , {
47+ runId,
48+ } ) ;
49+
50+ return ;
51+ }
52+
53+ if ( ! run . environmentType ) {
54+ logger . error ( "RunDashboard: upsertRun: run environment type not found" , {
55+ runId,
56+ } ) ;
57+
58+ return ;
59+ }
60+
61+ if ( ! run . organizationId ) {
62+ logger . error ( "RunDashboard: upsertRun: run organization id not found" , {
63+ runId,
64+ } ) ;
65+
66+ return ;
67+ }
68+
69+ await service . upsertRun ( time , run , run . environmentType , run . organizationId ) ;
70+ }
0 commit comments