clear residual timers
This commit is contained in:
parent
757b120e42
commit
6607b306d0
@ -72,21 +72,23 @@ exports.handler = async function(context, event, callback) {
|
|||||||
));
|
));
|
||||||
|
|
||||||
let discordLock = false;
|
let discordLock = false;
|
||||||
|
let intervals = [];
|
||||||
|
let timeouts = [];
|
||||||
|
|
||||||
const unlockPromise = new Promise((resolve, reject) => {
|
const unlockPromise = new Promise((resolve, reject) => {
|
||||||
const interval = setInterval(() => {
|
intervals.push(setInterval(() => {
|
||||||
fetch(context.DOORMAN_URL + `/api/door/status?door=${config.door}`)
|
fetch(context.DOORMAN_URL + `/api/door/status?door=${config.door}`)
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
clearInterval(interval);
|
clearInterval(intervals[0]);
|
||||||
const body = await res.json();
|
const body = await res.json();
|
||||||
const twiml = new Twilio.twiml.VoiceResponse();
|
const twiml = new Twilio.twiml.VoiceResponse();
|
||||||
doorOpenTwiml(twiml, config);
|
doorOpenTwiml(twiml, config);
|
||||||
if (!discordLock) {
|
if (!discordLock) {
|
||||||
|
discordLock = true;
|
||||||
console.log(
|
console.log(
|
||||||
"UnlockPromise: I was the fastest, so I will attempt to notify discord users before resolving with unlock"
|
"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));
|
await notifyAllDiscord(context, config, `🔓 Doorman buzzed someone up @ Door "${config.door}"`, JSON.stringify(body.fingerprint));
|
||||||
resolve(twiml);
|
resolve(twiml);
|
||||||
} else {
|
} else {
|
||||||
@ -96,19 +98,20 @@ exports.handler = async function(context, event, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).catch(err => console.log(err));
|
}).catch(err => console.log(err));
|
||||||
}, 500)
|
}, 500));
|
||||||
});
|
});
|
||||||
|
|
||||||
const gracefulFallbackPromise = new Promise((resolve, reject) => {
|
const gracefulFallbackPromise = new Promise((resolve, reject) => {
|
||||||
setTimeout(async () => {
|
timeouts.push(setTimeout(async () => {
|
||||||
const twiml = new Twilio.twiml.VoiceResponse();
|
const twiml = new Twilio.twiml.VoiceResponse();
|
||||||
dialFallbackTwiml(twiml, config);
|
dialFallbackTwiml(twiml, config);
|
||||||
|
|
||||||
if (!discordLock) {
|
if (!discordLock) {
|
||||||
|
discordLock = true;
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"GracefulFallbackPromise: I was the fastest, so I will attempt to notify discord users before resolving with a call"
|
"GracefulFallbackPromise: I was the fastest, so I will attempt to notify discord users before resolving with a call"
|
||||||
);
|
);
|
||||||
discordLock = true;
|
|
||||||
await notifyAllDiscord(
|
await notifyAllDiscord(
|
||||||
context,
|
context,
|
||||||
config,
|
config,
|
||||||
@ -120,20 +123,23 @@ exports.handler = async function(context, event, callback) {
|
|||||||
"GracefulFallbackPromise: dropping out of the race, unlock is already notifying discord users"
|
"GracefulFallbackPromise: dropping out of the race, unlock is already notifying discord users"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}, 6000);
|
}, 6000));
|
||||||
});
|
});
|
||||||
|
|
||||||
const ungracefulFallbackPromise = new Promise((resolve, reject) => {
|
const ungracefulFallbackPromise = new Promise((resolve, reject) => {
|
||||||
setTimeout(async () => {
|
timeouts.push(setTimeout(async () => {
|
||||||
const twiml = new Twilio.twiml.VoiceResponse();
|
const twiml = new Twilio.twiml.VoiceResponse();
|
||||||
dialFallbackTwiml(twiml, config);
|
dialFallbackTwiml(twiml, config);
|
||||||
console.error(
|
console.error(
|
||||||
"UngracefulFallbackPromise: Cutting it too close to timeout! Skipping notifying users and calling fallback"
|
"UngracefulFallbackPromise: Cutting it too close to timeout! Skipping notifying users and calling fallback"
|
||||||
);
|
);
|
||||||
resolve(twiml);
|
resolve(twiml);
|
||||||
}, 8000);
|
}, 8000));
|
||||||
});
|
});
|
||||||
|
|
||||||
const twiml = await Promise.race([unlockPromise, gracefulFallbackPromise, ungracefulFallbackPromise]);
|
const twiml = await Promise.race([unlockPromise, gracefulFallbackPromise, ungracefulFallbackPromise]);
|
||||||
|
console.log("Race ended, clearing residual timers");
|
||||||
|
timeouts.forEach(timeout => clearTimeout(timeout));
|
||||||
|
intervals.forEach(interval => clearInterval(interval));
|
||||||
callback(null, twiml);
|
callback(null, twiml);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user