Skip to content

Commit 1abf91a

Browse files
steve-c-thompsonSteve Thompson
andauthored
Warmup lambdas added for off-hours warming (#20)
* Warmup added to serverless, for off-hours warming. Small cleanup of story format detection and messaging. --------- Co-authored-by: Steve Thompson <[email protected]>
1 parent 6433a02 commit 1abf91a

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

serverless.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ functions:
9090
slack:
9191
provisionedConcurrency: 1
9292
concurrencyAutoscaling: ${self:custom.lambda.concurrencyAutoscaling}
93+
warmup:
94+
outOfOfficeHoursWarmer:
95+
enabled: true
9396
handler: src/app.handler
9497
events:
9598
- http:
@@ -98,6 +101,9 @@ functions:
98101
worker:
99102
provisionedConcurrency: 1
100103
concurrencyAutoscaling: ${self:custom.lambda.concurrencyAutoscaling}
104+
warmup:
105+
outOfOfficeHoursWarmer:
106+
enabled: true
101107
handler: src/worker.handler
102108
events:
103109
- http:
@@ -108,6 +114,7 @@ plugins:
108114
- serverless-offline
109115
- serverless-express
110116
- serverless-provisioned-concurrency-autoscaling
117+
- serverless-plugin-warmup
111118
package:
112119
excludeDevDependencies: true
113120
individually: true
@@ -135,4 +142,15 @@ custom:
135142
schedule: "cron(0 21 ? * MON-FRI *)"
136143
action:
137144
maximum: 0
138-
minimum: 0
145+
minimum: 0
146+
warmup:
147+
outOfOfficeHoursWarmer:
148+
enabled:
149+
- prod
150+
events:
151+
# 4-11:59 UTC is 9pm - 4:59am MST
152+
- schedule: cron(0/5 4-11 ? * MON-FRI *)
153+
- schedule: cron(0/5 * ? * SAT-SUN *)
154+
concurrency: 1
155+
verbose: false
156+
memorySize: 128

src/app.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ const init = async () => {
153153
// Delegate processing to a worker
154154
await delegateToWorker(body, context, signingSecret, logger);
155155

156-
// TODO maybe update the view with a "processing" message
157-
158156
// ack the request
159157
await ack();
160158
if(timerEnabled) {
@@ -168,7 +166,7 @@ const init = async () => {
168166
* ack() the request and then forward the request to another lambda function.
169167
*/
170168
app.action({block_id: blockId}, async ({ack, body, client, logger, context}) => {
171-
// logger.info("Action received: ", JSON.stringify(body, null, 2));
169+
logger.info("Action received: ", JSON.stringify(body, null, 2));
172170
let timer = new Timer();
173171
if(timerEnabled) {
174172
timer.startTimer();

src/bot/BotViewBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class BotViewBuilder {
3939
* `12345` YES
4040
* @private
4141
*/
42-
private storySearchRegex = new RegExp(/`((SC-)?(\d{5}))`/, "g");
42+
private storySearchRegex = new RegExp(/`((SC-)?(\d{5}))`/, "gi");
4343

4444
/**
4545
* Build the primary input view using block kit.
@@ -71,7 +71,7 @@ export class BotViewBuilder {
7171
elements: [
7272
{
7373
type: "mrkdwn",
74-
text: "Five-digit numbers surrounded by backticks `` and displayed as `code` will be linked to Shortcut stories.",
74+
text: "Five-digit numbers surrounded by backticks `` to display `code` can link to Shortcut stories. For example, `12345` or `SC-12345` or `sc-12345`",
7575
},
7676
]
7777
},

src/bot/SlackBot.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export class SlackBot {
7676

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

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

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

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

185187
// post as the user who requested
186188
return {

src/utils/lambdautils.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,24 @@ function createPayloadString(body: string) {
3636

3737
/**
3838
* Send a lambda request
39+
*
40+
* https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-lambda/
41+
*
3942
* @param data
4043
* @param logger
4144
*/
42-
function executeLambdaSend(data: { headers: any; path: string; resource: string; body: string; httpMethod: string }, logger: Logger) {
45+
async function executeLambdaSend(data: { headers: any; path: string; resource: string; body: string; httpMethod: string }, logger: Logger) {
4346
// logger.info("LAMBDA FORWARDING to " + workerLambdaName + " with data: " + JSON.stringify(data, null, 2));
4447
try {
45-
appContext.lambdaClient.send(new InvokeCommand({
48+
const result = await appContext.lambdaClient.send(new InvokeCommand({
4649
FunctionName: workerLambdaName,
4750
LogType: LogType.Tail,
4851
InvocationType: InvocationType.Event,
4952
Payload: Buffer.from(JSON.stringify(data)),
50-
}), (err, result) => {
51-
if (err) {
52-
logger.error(err);
53-
return;
54-
}
55-
// logger.info("LAMBDA RETURNED " + JSON.stringify(result, null, 2));
56-
});
53+
}));
54+
// logger.info("Lambda result: " + JSON.stringify(result, null, 2));
5755
} catch (error) {
5856
logger.error("Lambda Error " + JSON.stringify(error, null, 2));
59-
logger.error(error);
6057
}
6158
}
6259

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

8582
const data = createWorkerLambdaRequest(fullPlayload, context);
86-
executeLambdaSend(data, logger);
83+
await executeLambdaSend(data, logger);
8784
}
8885

8986
/**

0 commit comments

Comments
 (0)