-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
28 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,38 @@ | ||
import { scaleUp } from './scale-runners/scale-up'; | ||
import { scaleDown } from './scale-runners/scale-down'; | ||
import { SQSEvent, ScheduledEvent, Context, Callback } from 'aws-lambda'; | ||
import { logger } from './scale-runners/logger'; | ||
import { LogFields, logger } from './scale-runners/logger'; | ||
import ScaleError from './scale-runners/ScaleError'; | ||
import 'source-map-support/register'; | ||
|
||
export async function scaleUpHandler(event: SQSEvent, context: Context): Promise<void> { | ||
logger.setSettings({ requestId: context.awsRequestId }); | ||
logger.debug(JSON.stringify(event)); | ||
if (event.Records.length !== 1) { | ||
logger.warn('Event ignored, only one record at the time can be handled, ensure the lambda batch size is set to 1.'); | ||
logger.warn( | ||
'Event ignored, only one record at the time can be handled, ensure the lambda batch size is set to 1.', | ||
LogFields.print(), | ||
); | ||
return new Promise((resolve) => resolve()); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
scaleUp(event.Records[0].eventSource, JSON.parse(event.Records[0].body)) | ||
.then(() => resolve()) | ||
.catch((e: Error) => { | ||
if (e instanceof ScaleError) { | ||
reject(e); | ||
} else { | ||
logger.warn(`Ignoring error: ${e.message}`); | ||
} | ||
}); | ||
}); | ||
try { | ||
await scaleUp(event.Records[0].eventSource, JSON.parse(event.Records[0].body)); | ||
} catch (e) { | ||
if (e instanceof ScaleError) { | ||
throw e; | ||
} else { | ||
logger.warn(`Ignoring error: ${(e as Error).message}`, LogFields.print()); | ||
} | ||
} | ||
} | ||
|
||
export async function scaleDownHandler(event: ScheduledEvent, context: Context): Promise<void> { | ||
logger.setSettings({ requestId: context.awsRequestId }); | ||
|
||
return new Promise((resolve) => { | ||
scaleDown() | ||
.then(() => resolve()) | ||
.catch((e) => { | ||
logger.error(e); | ||
resolve(); | ||
}); | ||
}); | ||
try { | ||
await scaleDown(); | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters