feat: bring back static hook timeout

This commit is contained in:
Barış Soner Uşaklı
2021-03-11 22:56:14 -05:00
parent 56b0bfd518
commit 46270f9f20
2 changed files with 35 additions and 4 deletions

View File

@@ -165,19 +165,35 @@ async function fireStaticHook(hook, hookList, params) {
if (hookFn.constructor && hookFn.constructor.name !== 'AsyncFunction') {
hookFn = util.promisify(hookFn);
}
try {
// eslint-disable-next-line
await hookFn(params);
await timeout(hookFn(params), 5000, 'timeout');
} catch (err) {
winston.error(`[plugins] Error executing '${hook}' in plugin '${hookObj.id}'\n${err.stack}`);
if (!noErrorHooks.includes(hook)) {
throw err;
if (err && err.message === 'timeout') {
winston.warn(`[plugins] Callback timed out, hook '${hook}' in plugin '${hookObj.id}'`);
} else {
winston.error(`[plugins] Error executing '${hook}' in plugin '${hookObj.id}'\n${err.stack}`);
if (!noErrorHooks.includes(hook)) {
throw err;
}
}
}
}
}
}
// https://advancedweb.hu/how-to-add-timeout-to-a-promise-in-javascript/
const timeout = (prom, time, error) => {
let timer;
return Promise.race([
prom,
new Promise((resolve, reject) => {
timer = setTimeout(reject, time, new Error(error));
}),
]).finally(() => clearTimeout(timer));
};
async function fireResponseHook(hook, hookList, params) {
if (!Array.isArray(hookList) || !hookList.length) {
return;