diff --git a/packages/doorman-api/functions/api/door/notify.js b/packages/doorman-api/functions/api/door/notify.js index b974660..b3c6def 100644 --- a/packages/doorman-api/functions/api/door/notify.js +++ b/packages/doorman-api/functions/api/door/notify.js @@ -39,7 +39,7 @@ exports.handler = async function(context, event, callback) { setTimeout(() => { console.log("Ungraceful finish: running out of time"); callback(null, response); - }, 5000); + }, 9500); await Promise.all(promises); return callback(null, response); diff --git a/packages/doorman-api/functions/common/discord.private.js b/packages/doorman-api/functions/common/discord.private.js index 1bc0fe1..d785b83 100644 --- a/packages/doorman-api/functions/common/discord.private.js +++ b/packages/doorman-api/functions/common/discord.private.js @@ -4,10 +4,13 @@ const { Client, GatewayIntentBits } = require("discord.js"); +// TODO: cache these at top level in handler code let conn; +let userCache = {}; exports.getDiscordClient = async (context) => { if (!conn) { + console.log("[DiscordClientCache] cache miss for discord"); const client = new Client({ intents: [GatewayIntentBits.DirectMessages], rest: { @@ -17,6 +20,8 @@ exports.getDiscordClient = async (context) => { await client.login(context.DISCORD_BOT_TOKEN); conn = client; + } else { + console.log("[DiscordClientCache] cache hit for discord"); } return conn; @@ -30,7 +35,13 @@ exports.sendMessageToUser = async ( ) => { try { const client = await exports.getDiscordClient(context); - const user = await client.users.fetch(userId); + if (userCache[userId] === undefined) { + console.log("[UserCache] cache miss for", userId); + userCache[userId] = await client.users.fetch(userId); + } else { + console.log("[UserCache] cache hit for", userId); + } + const user = userCache[userId]; return user.send(msg); } catch (e) { console.log(e); diff --git a/packages/doorman-client/functions/buzzer-activated.js b/packages/doorman-client/functions/buzzer-activated.js index fce9afb..7029c7c 100644 --- a/packages/doorman-client/functions/buzzer-activated.js +++ b/packages/doorman-client/functions/buzzer-activated.js @@ -93,7 +93,7 @@ exports.handler = async function(context, event, callback) { msg + u + ')' ); - notifyDiscord(context, msgs, config.discordUsers, []); + await notifyDiscord(context, msgs, config.discordUsers, []); let discordLock = false; let intervals = [];