@@ -23,9 +23,12 @@ export type RunsReplicationServiceOptions = {
2323 maxFlushConcurrency ?: number ;
2424 flushIntervalMs ?: number ;
2525 flushBatchSize ?: number ;
26+ leaderLockTimeoutMs ?: number ;
27+ leaderLockExtendIntervalMs ?: number ;
28+ ackIntervalSeconds ?: number ;
2629} ;
2730
28- type TaskRunInsert = { _version : bigint ; run : TaskRun ; event : "insert" | "update" } ;
31+ type TaskRunInsert = { _version : bigint ; run : TaskRun ; event : "insert" | "update" | "delete" } ;
2932
3033export class RunsReplicationService {
3134 private _lastLsn : string | null = null ;
@@ -61,9 +64,9 @@ export class RunsReplicationService {
6164 autoAcknowledge : false ,
6265 publicationActions : [ "insert" , "update" ] ,
6366 logger : new Logger ( "RunsReplicationService" , "debug" ) ,
64- leaderLockTimeoutMs : 30_000 ,
65- leaderLockExtendIntervalMs : 10_000 ,
66- ackIntervalSeconds : 10 ,
67+ leaderLockTimeoutMs : options . leaderLockTimeoutMs ?? 30_000 ,
68+ leaderLockExtendIntervalMs : options . leaderLockExtendIntervalMs ?? 10_000 ,
69+ ackIntervalSeconds : options . ackIntervalSeconds ?? 10 ,
6770 } ) ;
6871
6972 this . _concurrentFlushScheduler = new ConcurrentFlushScheduler < TaskRunInsert > ( {
@@ -217,20 +220,6 @@ export class RunsReplicationService {
217220 return ;
218221 }
219222
220- const relevantEvents = transaction . events . filter (
221- ( event ) => event . tag === "insert" || event . tag === "update"
222- ) ;
223-
224- if ( relevantEvents . length === 0 ) {
225- this . logger . debug ( "No relevant events" , {
226- transaction,
227- } ) ;
228-
229- await this . _replicationClient . acknowledge ( transaction . commitEndLsn ) ;
230-
231- return ;
232- }
233-
234223 this . logger . debug ( "Handling transaction" , {
235224 transaction,
236225 } ) ;
@@ -242,20 +231,20 @@ export class RunsReplicationService {
242231
243232 if ( this . _insertStrategy === "streaming" ) {
244233 await this . _concurrentFlushScheduler . addToBatch (
245- relevantEvents . map ( ( event ) => ( {
234+ transaction . events . map ( ( event ) => ( {
246235 _version,
247236 run : event . data ,
248- event : event . tag as "insert" | "update" ,
237+ event : event . tag ,
249238 } ) )
250239 ) ;
251240 } else {
252241 const [ flushError ] = await tryCatch (
253242 this . #flushBatch(
254243 nanoid ( ) ,
255- relevantEvents . map ( ( event ) => ( {
244+ transaction . events . map ( ( event ) => ( {
256245 _version,
257246 run : event . data ,
258- event : event . tag as "insert" | "update" ,
247+ event : event . tag ,
259248 } ) )
260249 )
261250 ) ;
@@ -376,11 +365,12 @@ export class RunsReplicationService {
376365 } ;
377366 }
378367
379- if ( event === "update" ) {
368+ if ( event === "update" || event === "delete" ) {
380369 const taskRunInsert = await this . #prepareTaskRunInsert(
381370 run ,
382371 run . organizationId ,
383372 run . environmentType ,
373+ event ,
384374 _version
385375 ) ;
386376
@@ -391,7 +381,7 @@ export class RunsReplicationService {
391381 }
392382
393383 const [ taskRunInsert , payloadInsert ] = await Promise . all ( [
394- this . #prepareTaskRunInsert( run , run . organizationId , run . environmentType , _version ) ,
384+ this . #prepareTaskRunInsert( run , run . organizationId , run . environmentType , event , _version ) ,
395385 this . #preparePayloadInsert( run , _version ) ,
396386 ] ) ;
397387
@@ -405,6 +395,7 @@ export class RunsReplicationService {
405395 run : TaskRun ,
406396 organizationId : string ,
407397 environmentType : string ,
398+ event : "insert" | "update" | "delete" ,
408399 _version : bigint
409400 ) : Promise < TaskRunV1 > {
410401 const output = await this . #prepareJson( run . output , run . outputType ) ;
@@ -424,10 +415,10 @@ export class RunsReplicationService {
424415 queue : run . queue ,
425416 span_id : run . spanId ,
426417 trace_id : run . traceId ,
427- error : run . error ? ( run . error as TaskRunError ) : undefined ,
418+ error : { data : run . error } ,
428419 attempt : run . attemptNumber ?? 1 ,
429- schedule_id : run . scheduleId ,
430- batch_id : run . batchId ,
420+ schedule_id : run . scheduleId ?? "" ,
421+ batch_id : run . batchId ?? "" ,
431422 completed_at : run . completedAt ?. getTime ( ) ,
432423 started_at : run . startedAt ?. getTime ( ) ,
433424 executed_at : run . executedAt ?. getTime ( ) ,
@@ -438,18 +429,19 @@ export class RunsReplicationService {
438429 cost_in_cents : run . costInCents ,
439430 base_cost_in_cents : run . baseCostInCents ,
440431 tags : run . runTags ?? [ ] ,
441- task_version : run . taskVersion ,
442- sdk_version : run . sdkVersion ,
443- cli_version : run . cliVersion ,
444- machine_preset : run . machinePreset ,
445- root_run_id : run . rootTaskRunId ,
446- parent_run_id : run . parentTaskRunId ,
432+ task_version : run . taskVersion ?? "" ,
433+ sdk_version : run . sdkVersion ?? "" ,
434+ cli_version : run . cliVersion ?? "" ,
435+ machine_preset : run . machinePreset ?? "" ,
436+ root_run_id : run . rootTaskRunId ?? "" ,
437+ parent_run_id : run . parentTaskRunId ?? "" ,
447438 depth : run . depth ,
448439 is_test : run . isTest ,
449- idempotency_key : run . idempotencyKey ,
450- expiration_ttl : run . ttl ,
440+ idempotency_key : run . idempotencyKey ?? "" ,
441+ expiration_ttl : run . ttl ?? "" ,
451442 output,
452443 _version : _version . toString ( ) ,
444+ _is_deleted : event === "delete" ? 1 : 0 ,
453445 } ;
454446 }
455447
0 commit comments