refactor to have 3 promises race instead of timeouts
This commit is contained in:
parent
b27cdb5025
commit
757b120e42
@ -13,7 +13,7 @@ async function getConfig(context, buzzer) {
|
|||||||
|
|
||||||
function notifyDiscord(context, msg, u, optionalJsonStr) {
|
function notifyDiscord(context, msg, u, optionalJsonStr) {
|
||||||
return fetch(context.DOORMAN_URL +
|
return fetch(context.DOORMAN_URL +
|
||||||
`/api/door/notify?discordUser=${u}&msg=${encodeURIComponent(msg)}&json=${encodeURIComponent(optionalJsonStr)}`
|
`/api/door/notify?discordUser=${u}&msg=${encodeURIComponent(msg)}&json=${encodeURIComponent(optionalJsonStr)}`,
|
||||||
).catch(err => console.log(err))
|
).catch(err => console.log(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,8 +42,6 @@ function dialFallbackTwiml(twiml, config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.handler = async function(context, event, callback) {
|
exports.handler = async function(context, event, callback) {
|
||||||
let twiml = new Twilio.twiml.VoiceResponse();
|
|
||||||
|
|
||||||
let config = event.config;
|
let config = event.config;
|
||||||
|
|
||||||
// get by api or parse it out from query
|
// get by api or parse it out from query
|
||||||
@ -59,45 +57,83 @@ exports.handler = async function(context, event, callback) {
|
|||||||
|
|
||||||
// reject the call if this is not configured
|
// reject the call if this is not configured
|
||||||
if (!config || !config.door) {
|
if (!config || !config.door) {
|
||||||
|
let twiml = new Twilio.twiml.VoiceResponse();
|
||||||
twiml.reject();
|
twiml.reject();
|
||||||
callback(null, twiml);
|
callback(null, twiml);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: await on this?
|
|
||||||
// let users know someone is currently buzzing, and allow unlock by discord user
|
// let users know someone is currently buzzing, and allow unlock by discord user
|
||||||
let msg = `🔔 Someone is dialing right now @ Door "${config.door}" [Click to unlock](${context.DOORMAN_URL}/api/door/auth?door=${config.door}&key=`;
|
let msg = `🔔 Someone is dialing right now @ Door "${config.door}" [Click to unlock](${context.DOORMAN_URL}/api/door/auth?door=${config.door}&key=`;
|
||||||
let discordNotifs = Promise.all(config.discordUsers.map((u) =>
|
|
||||||
|
// TODO: should we await for one delivery?
|
||||||
|
Promise.race(config.discordUsers.map((u) =>
|
||||||
notifyDiscord(context, msg + u + ')', u)
|
notifyDiscord(context, msg + u + ')', u)
|
||||||
));
|
));
|
||||||
|
|
||||||
// poll Doorman, to see if we should unlock
|
let discordLock = false;
|
||||||
const interval = setInterval(() => {
|
|
||||||
fetch(context.DOORMAN_URL + `/api/door/status?door=${config.door}`)
|
|
||||||
.then(async res => {
|
|
||||||
if (res.status === 200) {
|
|
||||||
clearInterval(interval);
|
|
||||||
// put this before awaiting notify
|
|
||||||
// so it would be appended above any dial Verb from fallback
|
|
||||||
doorOpenTwiml(twiml, config);
|
|
||||||
|
|
||||||
const body = await res.json();
|
const unlockPromise = new Promise((resolve, reject) => {
|
||||||
await notifyAllDiscord(context, config, `🔓 Doorman buzzed someone up @ Door "${config.door}"`, JSON.stringify(body.fingerprint));
|
const interval = setInterval(() => {
|
||||||
callback(null, twiml);
|
fetch(context.DOORMAN_URL + `/api/door/status?door=${config.door}`)
|
||||||
}
|
.then(async res => {
|
||||||
})
|
if (res.status === 200) {
|
||||||
.catch(err => console.log(err));
|
clearInterval(interval);
|
||||||
}, 500);
|
const body = await res.json();
|
||||||
|
const twiml = new Twilio.twiml.VoiceResponse();
|
||||||
|
doorOpenTwiml(twiml, config);
|
||||||
|
if (!discordLock) {
|
||||||
|
console.log(
|
||||||
|
"UnlockPromise: I was the fastest, so I will attempt to notify discord users before resolving with unlock"
|
||||||
|
);
|
||||||
|
discordLock = true;
|
||||||
|
await notifyAllDiscord(context, config, `🔓 Doorman buzzed someone up @ Door "${config.door}"`, JSON.stringify(body.fingerprint));
|
||||||
|
resolve(twiml);
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
"UnlockPromise: dropping out of the race, graceful fallback is already notifiying discord users"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).catch(err => console.log(err));
|
||||||
|
}, 500)
|
||||||
|
});
|
||||||
|
|
||||||
// redirect to call after 6s
|
const gracefulFallbackPromise = new Promise((resolve, reject) => {
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
dialFallbackTwiml(twiml, config);
|
const twiml = new Twilio.twiml.VoiceResponse();
|
||||||
await notifyAllDiscord(
|
dialFallbackTwiml(twiml, config);
|
||||||
context,
|
|
||||||
config,
|
|
||||||
`📞 Somebody buzzed the door and it dialed through to fallback phone numbers @ Door "${config.door}"`
|
|
||||||
);
|
|
||||||
|
|
||||||
callback(null, twiml);
|
if (!discordLock) {
|
||||||
}, 6000);
|
console.log(
|
||||||
|
"GracefulFallbackPromise: I was the fastest, so I will attempt to notify discord users before resolving with a call"
|
||||||
|
);
|
||||||
|
discordLock = true;
|
||||||
|
await notifyAllDiscord(
|
||||||
|
context,
|
||||||
|
config,
|
||||||
|
`📞 Somebody buzzed the door and it dialed through to fallback phone numbers @ Door "${config.door}"`
|
||||||
|
);
|
||||||
|
resolve(twiml);
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
"GracefulFallbackPromise: dropping out of the race, unlock is already notifying discord users"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, 6000);
|
||||||
|
});
|
||||||
|
|
||||||
|
const ungracefulFallbackPromise = new Promise((resolve, reject) => {
|
||||||
|
setTimeout(async () => {
|
||||||
|
const twiml = new Twilio.twiml.VoiceResponse();
|
||||||
|
dialFallbackTwiml(twiml, config);
|
||||||
|
console.error(
|
||||||
|
"UngracefulFallbackPromise: Cutting it too close to timeout! Skipping notifying users and calling fallback"
|
||||||
|
);
|
||||||
|
resolve(twiml);
|
||||||
|
}, 8000);
|
||||||
|
});
|
||||||
|
|
||||||
|
const twiml = await Promise.race([unlockPromise, gracefulFallbackPromise, ungracefulFallbackPromise]);
|
||||||
|
callback(null, twiml);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user