@@ -16,6 +16,7 @@ import {
16
16
Prisma ,
17
17
PrismaClient ,
18
18
PrismaClientOrTransaction ,
19
+ PrismaReplicaClient ,
19
20
TaskRun ,
20
21
TaskRunExecutionSnapshot ,
21
22
Waitpoint ,
@@ -50,6 +51,7 @@ import { TtlSystem } from "./systems/ttlSystem.js";
50
51
import { WaitpointSystem } from "./systems/waitpointSystem.js" ;
51
52
import { EngineWorker , HeartbeatTimeouts , RunEngineOptions , TriggerParams } from "./types.js" ;
52
53
import { workerCatalog } from "./workerCatalog.js" ;
54
+ import { getFinalRunStatuses , isFinalRunStatus } from "./statuses.js" ;
53
55
54
56
export class RunEngine {
55
57
private runLockRedis : Redis ;
@@ -61,6 +63,7 @@ export class RunEngine {
61
63
private heartbeatTimeouts : HeartbeatTimeouts ;
62
64
63
65
prisma : PrismaClient ;
66
+ readOnlyPrisma : PrismaReplicaClient ;
64
67
runQueue : RunQueue ;
65
68
eventBus : EventBus = new EventEmitter < EventBusEvents > ( ) ;
66
69
executionSnapshotSystem : ExecutionSnapshotSystem ;
@@ -79,6 +82,7 @@ export class RunEngine {
79
82
constructor ( private readonly options : RunEngineOptions ) {
80
83
this . logger = options . logger ?? new Logger ( "RunEngine" , this . options . logLevel ?? "info" ) ;
81
84
this . prisma = options . prisma ;
85
+ this . readOnlyPrisma = options . readOnlyPrisma ?? this . prisma ;
82
86
this . runLockRedis = createRedisClient (
83
87
{
84
88
...options . runLock . redis ,
@@ -123,7 +127,7 @@ export class RunEngine {
123
127
defaultEnvConcurrencyLimit : options . queue ?. defaultEnvConcurrency ?? 10 ,
124
128
} ) ,
125
129
defaultEnvConcurrency : options . queue ?. defaultEnvConcurrency ?? 10 ,
126
- logger : new Logger ( "RunQueue" , this . options . logLevel ?? "info" ) ,
130
+ logger : new Logger ( "RunQueue" , options . queue ? .logLevel ?? "info" ) ,
127
131
redis : { ...options . queue . redis , keyPrefix : `${ options . queue . redis . keyPrefix } runqueue:` } ,
128
132
retryOptions : options . queue ?. retryOptions ,
129
133
workerOptions : {
@@ -133,6 +137,13 @@ export class RunEngine {
133
137
immediatePollIntervalMs : options . worker . immediatePollIntervalMs ,
134
138
shutdownTimeoutMs : options . worker . shutdownTimeoutMs ,
135
139
} ,
140
+ concurrencySweeper : {
141
+ scanSchedule : options . queue ?. concurrencySweeper ?. scanSchedule ,
142
+ processMarkedSchedule : options . queue ?. concurrencySweeper ?. processMarkedSchedule ,
143
+ scanJitterInMs : options . queue ?. concurrencySweeper ?. scanJitterInMs ,
144
+ processMarkedJitterInMs : options . queue ?. concurrencySweeper ?. processMarkedJitterInMs ,
145
+ callback : this . #concurrencySweeperCallback. bind ( this ) ,
146
+ } ,
136
147
shardCount : options . queue ?. shardCount ,
137
148
masterQueueConsumersDisabled : options . queue ?. masterQueueConsumersDisabled ,
138
149
masterQueueConsumersIntervalMs : options . queue ?. masterQueueConsumersIntervalMs ,
@@ -1329,4 +1340,44 @@ export class RunEngine {
1329
1340
}
1330
1341
} ) ;
1331
1342
}
1343
+
1344
+ async #concurrencySweeperCallback(
1345
+ runIds : string [ ]
1346
+ ) : Promise < Array < { id : string ; orgId : string } > > {
1347
+ const runs = await this . readOnlyPrisma . taskRun . findMany ( {
1348
+ where : {
1349
+ id : { in : runIds } ,
1350
+ completedAt : {
1351
+ lte : new Date ( Date . now ( ) - 1000 * 60 * 10 ) , // This only finds runs that were completed more than 10 minutes ago
1352
+ } ,
1353
+ organizationId : {
1354
+ not : null ,
1355
+ } ,
1356
+ status : {
1357
+ in : getFinalRunStatuses ( ) ,
1358
+ } ,
1359
+ } ,
1360
+ select : {
1361
+ id : true ,
1362
+ status : true ,
1363
+ organizationId : true ,
1364
+ } ,
1365
+ } ) ;
1366
+
1367
+ // Log the finished runs
1368
+ for ( const run of runs ) {
1369
+ this . logger . info ( "Concurrency sweeper callback found finished run" , {
1370
+ runId : run . id ,
1371
+ orgId : run . organizationId ,
1372
+ status : run . status ,
1373
+ } ) ;
1374
+ }
1375
+
1376
+ return runs
1377
+ . filter ( ( run ) => ! ! run . organizationId )
1378
+ . map ( ( run ) => ( {
1379
+ id : run . id ,
1380
+ orgId : run . organizationId ! ,
1381
+ } ) ) ;
1382
+ }
1332
1383
}
0 commit comments