mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: bring back static hook timeout
This commit is contained in:
@@ -165,19 +165,35 @@ async function fireStaticHook(hook, hookList, params) {
|
|||||||
if (hookFn.constructor && hookFn.constructor.name !== 'AsyncFunction') {
|
if (hookFn.constructor && hookFn.constructor.name !== 'AsyncFunction') {
|
||||||
hookFn = util.promisify(hookFn);
|
hookFn = util.promisify(hookFn);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
await hookFn(params);
|
await timeout(hookFn(params), 5000, 'timeout');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
winston.error(`[plugins] Error executing '${hook}' in plugin '${hookObj.id}'\n${err.stack}`);
|
if (err && err.message === 'timeout') {
|
||||||
if (!noErrorHooks.includes(hook)) {
|
winston.warn(`[plugins] Callback timed out, hook '${hook}' in plugin '${hookObj.id}'`);
|
||||||
throw err;
|
} 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) {
|
async function fireResponseHook(hook, hookList, params) {
|
||||||
if (!Array.isArray(hookList) || !hookList.length) {
|
if (!Array.isArray(hookList) || !hookList.length) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -135,6 +135,21 @@ describe('Plugins', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should register and timeout a static hook returning a promise but takes too long', (done) => {
|
||||||
|
async function method(data) {
|
||||||
|
assert.equal(data.bar, 'test');
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
setTimeout(resolve, 6000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
plugins.hooks.register('test-plugin', { hook: 'static:test.hook', method: method });
|
||||||
|
plugins.hooks.fire('static:test.hook', { bar: 'test' }, (err) => {
|
||||||
|
assert.ifError(err);
|
||||||
|
plugins.hooks.unregister('test-plugin', 'static:test.hook', method);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should get plugin data from nbbpm', (done) => {
|
it('should get plugin data from nbbpm', (done) => {
|
||||||
plugins.get('nodebb-plugin-markdown', (err, data) => {
|
plugins.get('nodebb-plugin-markdown', (err, data) => {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
|
|||||||
Reference in New Issue
Block a user