1+ import { Logger } from "@trigger.dev/core/logger" ;
12import { nanoid } from "nanoid" ;
23import pLimit from "p-limit" ;
3- import { logger } from "~/services/logger.server" ;
4- import { TaskEventKind } from "@trigger.dev/database" ;
54
65export type DynamicFlushSchedulerConfig < T > = {
76 batchSize : number ;
@@ -49,6 +48,8 @@ export class DynamicFlushScheduler<T> {
4948 private readonly isDroppableEvent ?: ( item : T ) => boolean ;
5049 private isLoadShedding : boolean = false ;
5150
51+ private readonly logger : Logger = new Logger ( "EventRepo.DynamicFlushScheduler" , "debug" ) ;
52+
5253 constructor ( config : DynamicFlushSchedulerConfig < T > ) {
5354 this . batchQueue = [ ] ;
5455 this . currentBatch = [ ] ;
@@ -98,16 +99,17 @@ export class DynamicFlushScheduler<T> {
9899
99100 if ( ! this . isLoadShedding ) {
100101 this . isLoadShedding = true ;
101- logger . warn ( "Load shedding activated" , {
102- totalQueuedItems : this . totalQueuedItems ,
103- threshold : this . loadSheddingThreshold ,
104- droppedCount : dropped . length ,
105- } ) ;
106102 }
103+
104+ this . logger . warn ( "Load shedding" , {
105+ totalQueuedItems : this . totalQueuedItems ,
106+ threshold : this . loadSheddingThreshold ,
107+ droppedCount : dropped . length ,
108+ } ) ;
107109 }
108110 } else if ( this . isLoadShedding && this . totalQueuedItems < this . loadSheddingThreshold * 0.8 ) {
109111 this . isLoadShedding = false ;
110- logger . info ( "Load shedding deactivated" , {
112+ this . logger . info ( "Load shedding deactivated" , {
111113 totalQueuedItems : this . totalQueuedItems ,
112114 threshold : this . loadSheddingThreshold ,
113115 totalDropped : this . metrics . droppedEvents ,
@@ -183,7 +185,7 @@ export class DynamicFlushScheduler<T> {
183185 this . metrics . flushedBatches ++ ;
184186 this . metrics . totalItemsFlushed += itemCount ;
185187
186- logger . debug ( "Batch flushed successfully" , {
188+ this . logger . debug ( "Batch flushed successfully" , {
187189 flushId,
188190 itemCount,
189191 duration,
@@ -195,7 +197,7 @@ export class DynamicFlushScheduler<T> {
195197 this . consecutiveFlushFailures ++ ;
196198 this . metrics . failedBatches ++ ;
197199
198- logger . error ( "Error flushing batch" , {
200+ this . logger . error ( "Error flushing batch" , {
199201 flushId,
200202 itemCount,
201203 error,
@@ -223,13 +225,21 @@ export class DynamicFlushScheduler<T> {
223225 } ) ;
224226 }
225227
228+ private lastConcurrencyAdjustment : number = Date . now ( ) ;
229+
226230 private adjustConcurrency ( backOff : boolean = false ) : void {
227231 const currentConcurrency = this . limiter . concurrency ;
228232 let newConcurrency = currentConcurrency ;
229-
233+
230234 // Calculate pressure metrics - moved outside the if/else block
231235 const queuePressure = this . totalQueuedItems / this . memoryPressureThreshold ;
232236 const timeSinceLastFlush = Date . now ( ) - this . lastFlushTime ;
237+ const timeSinceLastAdjustment = Date . now ( ) - this . lastConcurrencyAdjustment ;
238+
239+ // Don't adjust too frequently (except for backoff)
240+ if ( ! backOff && timeSinceLastAdjustment < 1000 ) {
241+ return ;
242+ }
233243
234244 if ( backOff ) {
235245 // Reduce concurrency on failures
@@ -258,12 +268,13 @@ export class DynamicFlushScheduler<T> {
258268 if ( newConcurrency !== currentConcurrency ) {
259269 this . limiter = pLimit ( newConcurrency ) ;
260270
261- logger . info ( "Adjusted flush concurrency" , {
271+ this . logger . info ( "Adjusted flush concurrency" , {
262272 previousConcurrency : currentConcurrency ,
263273 newConcurrency,
264274 queuePressure,
265275 totalQueuedItems : this . totalQueuedItems ,
266276 currentBatchSize : this . currentBatchSize ,
277+ memoryPressureThreshold : this . memoryPressureThreshold ,
267278 } ) ;
268279 }
269280 }
@@ -276,7 +287,7 @@ export class DynamicFlushScheduler<T> {
276287 droppedByKind [ kind ] = count ;
277288 } ) ;
278289
279- logger . info ( "DynamicFlushScheduler metrics" , {
290+ this . logger . info ( "DynamicFlushScheduler metrics" , {
280291 totalQueuedItems : this . totalQueuedItems ,
281292 batchQueueLength : this . batchQueue . length ,
282293 currentBatchLength : this . currentBatch . length ,
0 commit comments