@@ -11,22 +11,13 @@ import { Job, JobStatus } from '../../../src/entities/job';
1111import { ECSContainer } from '../../../src/services/containerServices' ;
1212import { SQSConnector } from '../../../src/services/queue' ;
1313import { Batch } from '../../../src/services/batch' ;
14- import { APIGatewayEvent , APIGatewayProxyResult , SQSEvent , SQSRecord } from 'aws-lambda' ;
1514
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' ) ) ;
1719 const consoleLogger = new ConsoleLogger ( ) ;
1820 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-
3021 const body = JSON . parse ( event . body ) ;
3122 try {
3223 consoleLogger . info ( body . jobId , 'enqueuing Job' ) ;
@@ -47,23 +38,26 @@ export const TriggerLocalBuild = async (event: APIGatewayEvent): Promise<APIGate
4738 }
4839} ;
4940
50- export const HandleJobs = async ( event : SQSEvent ) : Promise < void > => {
41+ // TODO: use @types /aws-lambda
42+ export const HandleJobs = async ( event : any = { } ) : Promise < any > => {
5143 /**
5244 * Check the status of the incoming jobs
5345 * if it is inqueue start a task
5446 * if it is inprogress call NotifyBuildProgress
5547 * if it is completed call NotifyBuildSummary
5648 */
57- const messages = event . Records ;
49+ const messages : JobQueueMessage [ ] = event . Records ;
5850 await Promise . all (
59- messages . map ( async ( message : SQSRecord ) => {
51+ messages . map ( async ( message : any ) => {
6052 const consoleLogger = new ConsoleLogger ( ) ;
6153 const body = JSON . parse ( message . body ) ;
54+ let queueUrl = '' ;
6255 const jobId = body [ 'jobId' ] ;
6356 const jobStatus = body [ 'jobStatus' ] ;
6457 try {
6558 switch ( jobStatus ) {
6659 case JobStatus [ JobStatus . inQueue ] :
60+ queueUrl = c . get ( 'jobsQueueUrl' ) ;
6761 await NotifyBuildProgress ( jobId ) ;
6862 // start the task , don't start the process before processing the notification
6963 const ecsServices = new ECSContainer ( c , consoleLogger ) ;
@@ -74,6 +68,7 @@ export const HandleJobs = async (event: SQSEvent): Promise<void> => {
7468 consoleLogger . info ( jobId , JSON . stringify ( res ) ) ;
7569 break ;
7670 case JobStatus [ JobStatus . inProgress ] :
71+ queueUrl = c . get ( 'jobUpdatesQueueUrl' ) ;
7772 await NotifyBuildProgress ( jobId ) ;
7873 break ;
7974 case JobStatus [ JobStatus . timedOut ] :
@@ -85,7 +80,9 @@ export const HandleJobs = async (event: SQSEvent): Promise<void> => {
8580 break ;
8681 case JobStatus [ JobStatus . failed ] :
8782 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 ) ;
8986 break ;
9087 default :
9188 consoleLogger . error ( jobId , 'Invalid status' ) ;
@@ -137,35 +134,42 @@ async function stopECSTask(taskId: string, consoleLogger: ConsoleLogger) {
137134 await ecs . stopZombieECSTask ( taskId ) ;
138135}
139136
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 > {
141155 const consoleLogger = new ConsoleLogger ( ) ;
142156 const client = new mongodb . MongoClient ( c . get ( 'dbUrl' ) ) ;
143157 await client . connect ( ) ;
144158 const db = client . db ( c . get ( 'dbName' ) ) ;
145159 const env = c . get < string > ( 'env' ) ;
146160
147161 const jobRepository = new JobRepository ( db , c , consoleLogger ) ;
162+ // TODO: Make fullDocument be of type Job, validate existence
148163 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-
158164 const repoName = fullDocument . payload . repoName ;
159165 const username = fullDocument . user ;
160166 const slackConnector = new SlackConnector ( consoleLogger , c ) ;
161167 const repoEntitlementRepository = new RepoEntitlementsRepository ( db , c , consoleLogger ) ;
162168 const entitlement = await repoEntitlementRepository . getSlackUserIdByGithubUsername ( username ) ;
163-
164169 if ( ! entitlement ?. [ 'slack_user_id' ] ) {
165170 consoleLogger . error ( username , 'Entitlement failed' ) ;
166171 return ;
167172 }
168-
169173 await slackConnector . sendMessage (
170174 await prepSummaryMessage (
171175 env ,
@@ -177,6 +181,9 @@ async function NotifyBuildSummary(jobId: string): Promise<void> {
177181 ) ,
178182 entitlement [ 'slack_user_id' ]
179183 ) ;
184+ return {
185+ statusCode : 200 ,
186+ } ;
180187}
181188
182189export const extractUrlFromMessage = ( fullDocument ) : string [ ] => {
@@ -194,11 +201,12 @@ async function prepSummaryMessage(
194201 failed = false
195202) : Promise < string > {
196203 const urls = extractUrlFromMessage ( fullDocument ) ;
197- let mms_urls : Array < string | null > = [ null , null ] ;
204+ let mms_urls = [ null , null ] ;
198205 // mms-docs needs special handling as it builds two sites (cloudmanager & ops manager)
199206 // so we need to extract both URLs
200207 if ( repoName === 'mms-docs' ) {
201208 if ( urls . length >= 2 ) {
209+ // TODO: Type 'string[]' is not assignable to type 'null[]'.
202210 mms_urls = urls . slice ( - 2 ) ;
203211 }
204212 }
@@ -249,24 +257,15 @@ function prepProgressMessage(
249257 }
250258}
251259
252- async function NotifyBuildProgress ( jobId : string ) : Promise < void > {
260+ async function NotifyBuildProgress ( jobId : string ) : Promise < any > {
253261 const consoleLogger = new ConsoleLogger ( ) ;
254262 const client = new mongodb . MongoClient ( c . get ( 'dbUrl' ) ) ;
255263 await client . connect ( ) ;
256264 const db = client . db ( c . get ( 'dbName' ) ) ;
257265 const slackConnector = new SlackConnector ( consoleLogger , c ) ;
258266 const jobRepository = new JobRepository ( db , c , consoleLogger ) ;
259-
267+ // TODO: Make fullDocument be of type Job, validate existence
260268 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-
270269 const jobTitle = fullDocument . title ;
271270 const username = fullDocument . user ;
272271 const repoEntitlementRepository = new RepoEntitlementsRepository ( db , c , consoleLogger ) ;
@@ -275,8 +274,7 @@ async function NotifyBuildProgress(jobId: string): Promise<void> {
275274 consoleLogger . error ( username , 'Entitlement Failed' ) ;
276275 return ;
277276 }
278-
279- await slackConnector . sendMessage (
277+ const resp = await slackConnector . sendMessage (
280278 prepProgressMessage (
281279 c . get ( 'dashboardUrl' ) ,
282280 jobId ,
@@ -286,6 +284,9 @@ async function NotifyBuildProgress(jobId: string): Promise<void> {
286284 ) ,
287285 entitlement [ 'slack_user_id' ]
288286 ) ;
287+ return {
288+ statusCode : 200 ,
289+ } ;
289290}
290291
291292function getMongoClient ( config : IConfig ) : mongodb . MongoClient {
@@ -316,15 +317,6 @@ async function SubmitArchiveJob(jobId: string) {
316317 branches : new BranchRepository ( db , c , consoleLogger ) ,
317318 } ;
318319 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-
328320 const repo = await models . branches . getRepo ( job . payload . repoName ) ;
329321
330322 /* NOTE
0 commit comments