@@ -13,7 +13,6 @@ import {
13
13
MachinePreset ,
14
14
ProdTaskRunExecution ,
15
15
ProdTaskRunExecutionPayload ,
16
- QueueOptions ,
17
16
TaskRunError ,
18
17
TaskRunErrorCodes ,
19
18
TaskRunExecution ,
@@ -29,13 +28,13 @@ import {
29
28
BackgroundWorker ,
30
29
BackgroundWorkerTask ,
31
30
Prisma ,
32
- TaskQueue ,
33
31
TaskRunStatus ,
34
32
} from "@trigger.dev/database" ;
35
33
import { z } from "zod" ;
36
34
import { $replica , prisma } from "~/db.server" ;
37
35
import { env } from "~/env.server" ;
38
36
import { findEnvironmentById } from "~/models/runtimeEnvironment.server" ;
37
+ import { findQueueInEnvironment , sanitizeQueueName } from "~/models/taskQueue.server" ;
39
38
import { generateJWTTokenForEnvironment } from "~/services/apiAuth.server" ;
40
39
import { logger } from "~/services/logger.server" ;
41
40
import { singleton } from "~/utils/singleton" ;
@@ -67,7 +66,6 @@ import {
67
66
import { tracer } from "../tracer.server" ;
68
67
import { getMaxDuration } from "../utils/maxDuration" ;
69
68
import { MessagePayload } from "./types" ;
70
- import { findQueueInEnvironment , sanitizeQueueName } from "~/models/taskQueue.server" ;
71
69
72
70
const WithTraceContext = z . object ( {
73
71
traceparent : z . string ( ) . optional ( ) ,
@@ -323,6 +321,14 @@ export class SharedQueueConsumer {
323
321
ROOT_CONTEXT
324
322
) ;
325
323
324
+ logger . debug ( "SharedQueueConsumer starting new trace" , {
325
+ reasonStats : this . _reasonStats ,
326
+ actionStats : this . _actionStats ,
327
+ outcomeStats : this . _outcomeStats ,
328
+ iterationCount : this . _iterationsCount ,
329
+ consumerId : this . _id ,
330
+ } ) ;
331
+
326
332
// Get the span trace context
327
333
this . _currentSpanContext = trace . setSpan ( ROOT_CONTEXT , this . _currentSpan ) ;
328
334
@@ -351,6 +357,10 @@ export class SharedQueueConsumer {
351
357
try {
352
358
const result = await this . #doWorkInternal( ) ;
353
359
360
+ if ( result . reason !== "no_message_dequeued" ) {
361
+ logger . debug ( "SharedQueueConsumer doWorkInternal result" , { result } ) ;
362
+ }
363
+
354
364
this . _reasonStats [ result . reason ] = ( this . _reasonStats [ result . reason ] ?? 0 ) + 1 ;
355
365
this . _outcomeStats [ result . outcome ] = ( this . _outcomeStats [ result . outcome ] ?? 0 ) + 1 ;
356
366
@@ -371,6 +381,9 @@ export class SharedQueueConsumer {
371
381
if ( result . error ) {
372
382
span . recordException ( result . error ) ;
373
383
span . setStatus ( { code : SpanStatusCode . ERROR } ) ;
384
+ this . _currentSpan ?. recordException ( result . error ) ;
385
+ this . _currentSpan ?. setStatus ( { code : SpanStatusCode . ERROR } ) ;
386
+ this . _endSpanInNextIteration = true ;
374
387
}
375
388
376
389
if ( typeof result . interval === "number" ) {
@@ -755,7 +768,7 @@ export class SharedQueueConsumer {
755
768
) ;
756
769
757
770
if ( ! queue ) {
758
- logger . debug ( "SharedQueueConsumer queue not found, so nacking message" , {
771
+ logger . debug ( "SharedQueueConsumer queue not found, so acking message" , {
759
772
queueMessage : message ,
760
773
taskRunQueue : lockedTaskRun . queue ,
761
774
runtimeEnvironmentId : lockedTaskRun . runtimeEnvironmentId ,
@@ -876,33 +889,49 @@ export class SharedQueueConsumer {
876
889
machinePresetFromRun ( lockedTaskRun ) ??
877
890
machinePresetFromConfig ( lockedTaskRun . lockedBy ?. machineConfig ?? { } ) ;
878
891
879
- await this . #startActiveSpan( "scheduleAttemptOnProvider" , async ( span ) => {
880
- await this . _providerSender . send ( "BACKGROUND_WORKER_MESSAGE" , {
881
- backgroundWorkerId : worker . friendlyId ,
882
- data : {
883
- type : "SCHEDULE_ATTEMPT" ,
884
- image : imageReference ,
885
- version : deployment . version ,
886
- machine,
887
- nextAttemptNumber,
888
- // identifiers
889
- id : "placeholder" , // TODO: Remove this completely in a future release
890
- envId : lockedTaskRun . runtimeEnvironment . id ,
891
- envType : lockedTaskRun . runtimeEnvironment . type ,
892
- orgId : lockedTaskRun . runtimeEnvironment . organizationId ,
893
- projectId : lockedTaskRun . runtimeEnvironment . projectId ,
894
- runId : lockedTaskRun . id ,
895
- } ,
892
+ return await this . #startActiveSpan( "scheduleAttemptOnProvider" , async ( span ) => {
893
+ span . setAttributes ( {
894
+ run_id : lockedTaskRun . id ,
896
895
} ) ;
897
- } ) ;
898
896
899
- return {
900
- action : "noop" ,
901
- reason : "scheduled_attempt" ,
902
- attrs : {
903
- next_attempt_number : nextAttemptNumber ,
904
- } ,
905
- } ;
897
+ if ( await this . _providerSender . validateCanSendMessage ( ) ) {
898
+ await this . _providerSender . send ( "BACKGROUND_WORKER_MESSAGE" , {
899
+ backgroundWorkerId : worker . friendlyId ,
900
+ data : {
901
+ type : "SCHEDULE_ATTEMPT" ,
902
+ image : imageReference ,
903
+ version : deployment . version ,
904
+ machine,
905
+ nextAttemptNumber,
906
+ // identifiers
907
+ id : "placeholder" , // TODO: Remove this completely in a future release
908
+ envId : lockedTaskRun . runtimeEnvironment . id ,
909
+ envType : lockedTaskRun . runtimeEnvironment . type ,
910
+ orgId : lockedTaskRun . runtimeEnvironment . organizationId ,
911
+ projectId : lockedTaskRun . runtimeEnvironment . projectId ,
912
+ runId : lockedTaskRun . id ,
913
+ } ,
914
+ } ) ;
915
+
916
+ return {
917
+ action : "noop" ,
918
+ reason : "scheduled_attempt" ,
919
+ attrs : {
920
+ next_attempt_number : nextAttemptNumber ,
921
+ } ,
922
+ } ;
923
+ } else {
924
+ return {
925
+ action : "nack_and_do_more_work" ,
926
+ reason : "provider_not_connected" ,
927
+ attrs : {
928
+ run_id : lockedTaskRun . id ,
929
+ } ,
930
+ interval : this . _options . nextTickInterval ,
931
+ retryInMs : 5_000 ,
932
+ } ;
933
+ }
934
+ } ) ;
906
935
}
907
936
} catch ( e ) {
908
937
// We now need to unlock the task run and delete the task run attempt
@@ -929,6 +958,8 @@ export class SharedQueueConsumer {
929
958
action : "nack_and_do_more_work" ,
930
959
reason : "failed_to_schedule_attempt" ,
931
960
error : e instanceof Error ? e : String ( e ) ,
961
+ interval : this . _options . nextTickInterval ,
962
+ retryInMs : 5_000 ,
932
963
} ;
933
964
}
934
965
}
0 commit comments