Skip to content

Commit

Permalink
Warmup lambdas added for off-hours warming (#20)
Browse files Browse the repository at this point in the history
* Warmup added to serverless, for off-hours warming. Small cleanup of story format detection and  messaging.

---------

Co-authored-by: Steve Thompson <scthomps312@hotmail.com>
  • Loading branch information
steve-c-thompson and Steve Thompson committed Feb 26, 2023
1 parent 6433a02 commit 1abf91a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
20 changes: 19 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ functions:
slack:
provisionedConcurrency: 1
concurrencyAutoscaling: ${self:custom.lambda.concurrencyAutoscaling}
warmup:
outOfOfficeHoursWarmer:
enabled: true
handler: src/app.handler
events:
- http:
Expand All @@ -98,6 +101,9 @@ functions:
worker:
provisionedConcurrency: 1
concurrencyAutoscaling: ${self:custom.lambda.concurrencyAutoscaling}
warmup:
outOfOfficeHoursWarmer:
enabled: true
handler: src/worker.handler
events:
- http:
Expand All @@ -108,6 +114,7 @@ plugins:
- serverless-offline
- serverless-express
- serverless-provisioned-concurrency-autoscaling
- serverless-plugin-warmup
package:
excludeDevDependencies: true
individually: true
Expand Down Expand Up @@ -135,4 +142,15 @@ custom:
schedule: "cron(0 21 ? * MON-FRI *)"
action:
maximum: 0
minimum: 0
minimum: 0
warmup:
outOfOfficeHoursWarmer:
enabled:
- prod
events:
# 4-11:59 UTC is 9pm - 4:59am MST
- schedule: cron(0/5 4-11 ? * MON-FRI *)
- schedule: cron(0/5 * ? * SAT-SUN *)
concurrency: 1
verbose: false
memorySize: 128
4 changes: 1 addition & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ const init = async () => {
// Delegate processing to a worker
await delegateToWorker(body, context, signingSecret, logger);

// TODO maybe update the view with a "processing" message

// ack the request
await ack();
if(timerEnabled) {
Expand All @@ -168,7 +166,7 @@ const init = async () => {
* ack() the request and then forward the request to another lambda function.
*/
app.action({block_id: blockId}, async ({ack, body, client, logger, context}) => {
// logger.info("Action received: ", JSON.stringify(body, null, 2));
logger.info("Action received: ", JSON.stringify(body, null, 2));
let timer = new Timer();
if(timerEnabled) {
timer.startTimer();
Expand Down
4 changes: 2 additions & 2 deletions src/bot/BotViewBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class BotViewBuilder {
* `12345` YES
* @private
*/
private storySearchRegex = new RegExp(/`((SC-)?(\d{5}))`/, "g");
private storySearchRegex = new RegExp(/`((SC-)?(\d{5}))`/, "gi");

/**
* Build the primary input view using block kit.
Expand Down Expand Up @@ -71,7 +71,7 @@ export class BotViewBuilder {
elements: [
{
type: "mrkdwn",
text: "Five-digit numbers surrounded by backticks `` and displayed as `code` will be linked to Shortcut stories.",
text: "Five-digit numbers surrounded by backticks `` to display `code` can link to Shortcut stories. For example, `12345` or `SC-12345` or `sc-12345`",
},
]
},
Expand Down
4 changes: 3 additions & 1 deletion src/bot/SlackBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export class SlackBot {

const status = await this.statusDao.getStatusMessage(userId, messageId);

// Query the user so that we have the user's timezone
// TODO may not need this when we have saved the message's timezone offset
const userInfo = await this.queryUser(userId, client);

let blockData = await this.loadSavedStatusMessage(status, pm);
Expand Down Expand Up @@ -180,7 +182,7 @@ export class SlackBot {
memberInfos = await this.queryUsers(viewInput.attendees, client);
}

const blocks = this.viewBuilder.buildChatMessageOutputBlocks(messageType, userInfo, viewInput.yesterday, viewInput.today, viewInput.parkingLot, viewInput.pullRequests, memberInfos);
const blocks = this.viewBuilder.buildChatMessageOutputBlocks(messageType!, userInfo, viewInput.yesterday, viewInput.today, viewInput.parkingLot, viewInput.pullRequests, memberInfos);

// post as the user who requested
return {
Expand Down
19 changes: 8 additions & 11 deletions src/utils/lambdautils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,24 @@ function createPayloadString(body: string) {

/**
* Send a lambda request
*
* https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-lambda/
*
* @param data
* @param logger
*/
function executeLambdaSend(data: { headers: any; path: string; resource: string; body: string; httpMethod: string }, logger: Logger) {
async function executeLambdaSend(data: { headers: any; path: string; resource: string; body: string; httpMethod: string }, logger: Logger) {
// logger.info("LAMBDA FORWARDING to " + workerLambdaName + " with data: " + JSON.stringify(data, null, 2));
try {
appContext.lambdaClient.send(new InvokeCommand({
const result = await appContext.lambdaClient.send(new InvokeCommand({
FunctionName: workerLambdaName,
LogType: LogType.Tail,
InvocationType: InvocationType.Event,
Payload: Buffer.from(JSON.stringify(data)),
}), (err, result) => {
if (err) {
logger.error(err);
return;
}
// logger.info("LAMBDA RETURNED " + JSON.stringify(result, null, 2));
});
}));
// logger.info("Lambda result: " + JSON.stringify(result, null, 2));
} catch (error) {
logger.error("Lambda Error " + JSON.stringify(error, null, 2));
logger.error(error);
}
}

Expand All @@ -83,7 +80,7 @@ export async function delegateToWorker(body: any, context:Context, secret: strin
replaceHeaderValue(context.headers, 'X-Slack-Signature', sig);

const data = createWorkerLambdaRequest(fullPlayload, context);
executeLambdaSend(data, logger);
await executeLambdaSend(data, logger);
}

/**
Expand Down

0 comments on commit 1abf91a

Please sign in to comment.