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