@@ -3,6 +3,7 @@ import { workerQueue } from "~/services/worker.server";
3
3
import { marqs } from "~/v3/marqs/index.server" ;
4
4
import { BaseService } from "./baseService.server" ;
5
5
import { logger } from "~/services/logger.server" ;
6
+ import { env } from "~/env.server" ;
6
7
7
8
export class ExecuteTasksWaitingForDeployService extends BaseService {
8
9
public async call ( backgroundWorkerId : string ) {
@@ -17,7 +18,11 @@ export class ExecuteTasksWaitingForDeployService extends BaseService {
17
18
organization : true ,
18
19
} ,
19
20
} ,
20
- tasks : true ,
21
+ tasks : {
22
+ select : {
23
+ slug : true ,
24
+ } ,
25
+ } ,
21
26
} ,
22
27
} ) ;
23
28
@@ -26,6 +31,8 @@ export class ExecuteTasksWaitingForDeployService extends BaseService {
26
31
return ;
27
32
}
28
33
34
+ const maxCount = env . LEGACY_RUN_ENGINE_WAITING_FOR_DEPLOY_BATCH_SIZE ;
35
+
29
36
const runsWaitingForDeploy = await this . _prisma . taskRun . findMany ( {
30
37
where : {
31
38
runtimeEnvironmentId : backgroundWorker . runtimeEnvironmentId ,
@@ -36,8 +43,16 @@ export class ExecuteTasksWaitingForDeployService extends BaseService {
36
43
} ,
37
44
} ,
38
45
orderBy : {
39
- number : "asc" ,
46
+ createdAt : "asc" ,
40
47
} ,
48
+ select : {
49
+ id : true ,
50
+ status : true ,
51
+ taskIdentifier : true ,
52
+ concurrencyKey : true ,
53
+ queue : true ,
54
+ } ,
55
+ take : maxCount + 1 ,
41
56
} ) ;
42
57
43
58
if ( ! runsWaitingForDeploy . length ) {
@@ -63,50 +78,28 @@ export class ExecuteTasksWaitingForDeployService extends BaseService {
63
78
} ) ;
64
79
}
65
80
66
- if ( ! marqs ) {
67
- return ;
68
- }
69
-
70
- const enqueues : Promise < any > [ ] = [ ] ;
71
- let i = 0 ;
72
-
73
81
for ( const run of runsWaitingForDeploy ) {
74
- enqueues . push (
75
- marqs . enqueueMessage (
76
- backgroundWorker . runtimeEnvironment ,
77
- run . queue ,
78
- run . id ,
79
- {
80
- type : "EXECUTE" ,
81
- taskIdentifier : run . taskIdentifier ,
82
- projectId : backgroundWorker . runtimeEnvironment . projectId ,
83
- environmentId : backgroundWorker . runtimeEnvironment . id ,
84
- environmentType : backgroundWorker . runtimeEnvironment . type ,
85
- } ,
86
- run . concurrencyKey ?? undefined ,
87
- Date . now ( ) + i * 5 // slight delay to help preserve order
88
- )
82
+ await marqs ?. enqueueMessage (
83
+ backgroundWorker . runtimeEnvironment ,
84
+ run . queue ,
85
+ run . id ,
86
+ {
87
+ type : "EXECUTE" ,
88
+ taskIdentifier : run . taskIdentifier ,
89
+ projectId : backgroundWorker . runtimeEnvironment . projectId ,
90
+ environmentId : backgroundWorker . runtimeEnvironment . id ,
91
+ environmentType : backgroundWorker . runtimeEnvironment . type ,
92
+ } ,
93
+ run . concurrencyKey ?? undefined
89
94
) ;
90
-
91
- i ++ ;
92
95
}
93
96
94
- const settled = await Promise . allSettled ( enqueues ) ;
95
-
96
- if ( settled . some ( ( s ) => s . status === "rejected" ) ) {
97
- const rejectedRuns : { id : string ; reason : any } [ ] = [ ] ;
98
-
99
- runsWaitingForDeploy . forEach ( ( run , i ) => {
100
- if ( settled [ i ] . status === "rejected" ) {
101
- const rejected = settled [ i ] as PromiseRejectedResult ;
102
-
103
- rejectedRuns . push ( { id : run . id , reason : rejected . reason } ) ;
104
- }
105
- } ) ;
106
-
107
- logger . error ( "Failed to requeue task runs for immediate execution" , {
108
- rejectedRuns,
109
- } ) ;
97
+ if ( runsWaitingForDeploy . length > maxCount ) {
98
+ await ExecuteTasksWaitingForDeployService . enqueue (
99
+ backgroundWorkerId ,
100
+ this . _prisma ,
101
+ new Date ( Date . now ( ) + env . LEGACY_RUN_ENGINE_WAITING_FOR_DEPLOY_BATCH_STAGGER_MS )
102
+ ) ;
110
103
}
111
104
}
112
105
0 commit comments