@@ -11,22 +11,13 @@ import { Job, JobStatus } from '../../../src/entities/job';
11
11
import { ECSContainer } from '../../../src/services/containerServices' ;
12
12
import { SQSConnector } from '../../../src/services/queue' ;
13
13
import { Batch } from '../../../src/services/batch' ;
14
- import { APIGatewayEvent , APIGatewayProxyResult , SQSEvent , SQSRecord } from 'aws-lambda' ;
15
14
16
- export const TriggerLocalBuild = async ( event : APIGatewayEvent ) : Promise < APIGatewayProxyResult > => {
15
+ export const TriggerLocalBuild = async ( event : any = { } , context : any = { } ) : Promise < any > => {
16
+ const client = new mongodb . MongoClient ( c . get ( 'dbUrl' ) ) ;
17
+ await client . connect ( ) ;
18
+ const db = client . db ( c . get ( 'dbName' ) ) ;
17
19
const consoleLogger = new ConsoleLogger ( ) ;
18
20
const sqs = new SQSConnector ( consoleLogger , c ) ;
19
-
20
- if ( ! event . body ) {
21
- const err = 'Trigger local build does not have a body in event payload' ;
22
- consoleLogger . error ( 'TriggerLocalBuildError' , err ) ;
23
- return {
24
- statusCode : 400 ,
25
- headers : { 'Content-Type' : 'text/plain' } ,
26
- body : err ,
27
- } ;
28
- }
29
-
30
21
const body = JSON . parse ( event . body ) ;
31
22
try {
32
23
consoleLogger . info ( body . jobId , 'enqueuing Job' ) ;
@@ -47,23 +38,26 @@ export const TriggerLocalBuild = async (event: APIGatewayEvent): Promise<APIGate
47
38
}
48
39
} ;
49
40
50
- export const HandleJobs = async ( event : SQSEvent ) : Promise < void > => {
41
+ // TODO: use @types /aws-lambda
42
+ export const HandleJobs = async ( event : any = { } ) : Promise < any > => {
51
43
/**
52
44
* Check the status of the incoming jobs
53
45
* if it is inqueue start a task
54
46
* if it is inprogress call NotifyBuildProgress
55
47
* if it is completed call NotifyBuildSummary
56
48
*/
57
- const messages = event . Records ;
49
+ const messages : JobQueueMessage [ ] = event . Records ;
58
50
await Promise . all (
59
- messages . map ( async ( message : SQSRecord ) => {
51
+ messages . map ( async ( message : any ) => {
60
52
const consoleLogger = new ConsoleLogger ( ) ;
61
53
const body = JSON . parse ( message . body ) ;
54
+ let queueUrl = '' ;
62
55
const jobId = body [ 'jobId' ] ;
63
56
const jobStatus = body [ 'jobStatus' ] ;
64
57
try {
65
58
switch ( jobStatus ) {
66
59
case JobStatus [ JobStatus . inQueue ] :
60
+ queueUrl = c . get ( 'jobsQueueUrl' ) ;
67
61
await NotifyBuildProgress ( jobId ) ;
68
62
// start the task , don't start the process before processing the notification
69
63
const ecsServices = new ECSContainer ( c , consoleLogger ) ;
@@ -74,6 +68,7 @@ export const HandleJobs = async (event: SQSEvent): Promise<void> => {
74
68
consoleLogger . info ( jobId , JSON . stringify ( res ) ) ;
75
69
break ;
76
70
case JobStatus [ JobStatus . inProgress ] :
71
+ queueUrl = c . get ( 'jobUpdatesQueueUrl' ) ;
77
72
await NotifyBuildProgress ( jobId ) ;
78
73
break ;
79
74
case JobStatus [ JobStatus . timedOut ] :
@@ -85,7 +80,9 @@ export const HandleJobs = async (event: SQSEvent): Promise<void> => {
85
80
break ;
86
81
case JobStatus [ JobStatus . failed ] :
87
82
case JobStatus [ JobStatus . completed ] :
88
- await Promise . all ( [ NotifyBuildSummary ( jobId ) , SubmitArchiveJob ( jobId ) ] ) ;
83
+ queueUrl = c . get ( 'jobUpdatesQueueUrl' ) ;
84
+ await NotifyBuildSummary ( jobId ) ;
85
+ await SubmitArchiveJob ( jobId ) ;
89
86
break ;
90
87
default :
91
88
consoleLogger . error ( jobId , 'Invalid status' ) ;
@@ -137,35 +134,42 @@ async function stopECSTask(taskId: string, consoleLogger: ConsoleLogger) {
137
134
await ecs . stopZombieECSTask ( taskId ) ;
138
135
}
139
136
140
- async function NotifyBuildSummary ( jobId : string ) : Promise < void > {
137
+ async function retry ( message : JobQueueMessage , consoleLogger : ConsoleLogger , url : string ) : Promise < any > {
138
+ try {
139
+ const tries = message . tries ;
140
+ // TODO: c.get('maxRetries') is of type 'Unknown', needs validation
141
+ if ( tries < c . get ( 'maxRetries' ) ) {
142
+ const sqs = new SQSConnector ( consoleLogger , c ) ;
143
+ message [ 'tries' ] += 1 ;
144
+ let retryDelay = 10 ;
145
+ if ( c . get ( 'retryDelay' ) ) {
146
+ retryDelay = c . get ( 'retryDelay' ) ;
147
+ }
148
+ await sqs . sendMessage ( message , url , retryDelay * tries ) ;
149
+ }
150
+ } catch ( err ) {
151
+ consoleLogger . error ( message [ 'jobId' ] , err ) ;
152
+ }
153
+ }
154
+ async function NotifyBuildSummary ( jobId : string ) : Promise < any > {
141
155
const consoleLogger = new ConsoleLogger ( ) ;
142
156
const client = new mongodb . MongoClient ( c . get ( 'dbUrl' ) ) ;
143
157
await client . connect ( ) ;
144
158
const db = client . db ( c . get ( 'dbName' ) ) ;
145
159
const env = c . get < string > ( 'env' ) ;
146
160
147
161
const jobRepository = new JobRepository ( db , c , consoleLogger ) ;
162
+ // TODO: Make fullDocument be of type Job, validate existence
148
163
const fullDocument = await jobRepository . getJobById ( jobId ) ;
149
-
150
- if ( ! fullDocument ) {
151
- consoleLogger . error (
152
- `NotifyBuildSummary_${ jobId } ` ,
153
- `Notify build summary failed. Job does not exist for Job ID: ${ jobId } `
154
- ) ;
155
- return ;
156
- }
157
-
158
164
const repoName = fullDocument . payload . repoName ;
159
165
const username = fullDocument . user ;
160
166
const slackConnector = new SlackConnector ( consoleLogger , c ) ;
161
167
const repoEntitlementRepository = new RepoEntitlementsRepository ( db , c , consoleLogger ) ;
162
168
const entitlement = await repoEntitlementRepository . getSlackUserIdByGithubUsername ( username ) ;
163
-
164
169
if ( ! entitlement ?. [ 'slack_user_id' ] ) {
165
170
consoleLogger . error ( username , 'Entitlement failed' ) ;
166
171
return ;
167
172
}
168
-
169
173
await slackConnector . sendMessage (
170
174
await prepSummaryMessage (
171
175
env ,
@@ -177,6 +181,9 @@ async function NotifyBuildSummary(jobId: string): Promise<void> {
177
181
) ,
178
182
entitlement [ 'slack_user_id' ]
179
183
) ;
184
+ return {
185
+ statusCode : 200 ,
186
+ } ;
180
187
}
181
188
182
189
export const extractUrlFromMessage = ( fullDocument ) : string [ ] => {
@@ -194,11 +201,12 @@ async function prepSummaryMessage(
194
201
failed = false
195
202
) : Promise < string > {
196
203
const urls = extractUrlFromMessage ( fullDocument ) ;
197
- let mms_urls : Array < string | null > = [ null , null ] ;
204
+ let mms_urls = [ null , null ] ;
198
205
// mms-docs needs special handling as it builds two sites (cloudmanager & ops manager)
199
206
// so we need to extract both URLs
200
207
if ( repoName === 'mms-docs' ) {
201
208
if ( urls . length >= 2 ) {
209
+ // TODO: Type 'string[]' is not assignable to type 'null[]'.
202
210
mms_urls = urls . slice ( - 2 ) ;
203
211
}
204
212
}
@@ -249,24 +257,15 @@ function prepProgressMessage(
249
257
}
250
258
}
251
259
252
- async function NotifyBuildProgress ( jobId : string ) : Promise < void > {
260
+ async function NotifyBuildProgress ( jobId : string ) : Promise < any > {
253
261
const consoleLogger = new ConsoleLogger ( ) ;
254
262
const client = new mongodb . MongoClient ( c . get ( 'dbUrl' ) ) ;
255
263
await client . connect ( ) ;
256
264
const db = client . db ( c . get ( 'dbName' ) ) ;
257
265
const slackConnector = new SlackConnector ( consoleLogger , c ) ;
258
266
const jobRepository = new JobRepository ( db , c , consoleLogger ) ;
259
-
267
+ // TODO: Make fullDocument be of type Job, validate existence
260
268
const fullDocument = await jobRepository . getJobById ( jobId ) ;
261
-
262
- if ( ! fullDocument ) {
263
- consoleLogger . error (
264
- `NotifyBuildProgress_${ jobId } ` ,
265
- `Notify build progress failed. Job does not exist for Job ID: ${ jobId } `
266
- ) ;
267
- return ;
268
- }
269
-
270
269
const jobTitle = fullDocument . title ;
271
270
const username = fullDocument . user ;
272
271
const repoEntitlementRepository = new RepoEntitlementsRepository ( db , c , consoleLogger ) ;
@@ -275,8 +274,7 @@ async function NotifyBuildProgress(jobId: string): Promise<void> {
275
274
consoleLogger . error ( username , 'Entitlement Failed' ) ;
276
275
return ;
277
276
}
278
-
279
- await slackConnector . sendMessage (
277
+ const resp = await slackConnector . sendMessage (
280
278
prepProgressMessage (
281
279
c . get ( 'dashboardUrl' ) ,
282
280
jobId ,
@@ -286,6 +284,9 @@ async function NotifyBuildProgress(jobId: string): Promise<void> {
286
284
) ,
287
285
entitlement [ 'slack_user_id' ]
288
286
) ;
287
+ return {
288
+ statusCode : 200 ,
289
+ } ;
289
290
}
290
291
291
292
function getMongoClient ( config : IConfig ) : mongodb . MongoClient {
@@ -316,15 +317,6 @@ async function SubmitArchiveJob(jobId: string) {
316
317
branches : new BranchRepository ( db , c , consoleLogger ) ,
317
318
} ;
318
319
const job = await models . jobs . getJobById ( jobId ) ;
319
-
320
- if ( ! job ) {
321
- consoleLogger . error (
322
- `SubmitArchiveJob_${ jobId } ` ,
323
- `Submit archive job failed. Job does not exist for Job ID: ${ jobId } `
324
- ) ;
325
- return ;
326
- }
327
-
328
320
const repo = await models . branches . getRepo ( job . payload . repoName ) ;
329
321
330
322
/* NOTE
0 commit comments