From 77f6423435c59392f121ae314269f8f396e58b64 Mon Sep 17 00:00:00 2001 From: hveldstra Date: Wed, 19 Jul 2023 13:59:37 +0100 Subject: [PATCH] refactor: make sure beforeExit handlers are always run --- packages/artillery/lib/cmds/run.js | 39 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/packages/artillery/lib/cmds/run.js b/packages/artillery/lib/cmds/run.js index 185ccbc737..807a3b1ab0 100644 --- a/packages/artillery/lib/cmds/run.js +++ b/packages/artillery/lib/cmds/run.js @@ -321,22 +321,8 @@ RunCommand.runCommandImplementation = async function (flags, argv, args) { ); } - for (const e of global.artillery.extensionEvents) { - const ps = []; - const testInfo = { endTime: Date.now() }; - if (e.ext === 'beforeExit') { - ps.push( - e.method({ - report, - flags, - runnerOpts, - testInfo - }) - ); - } - await Promise.allSettled(ps); - } - + // This is used in the beforeExit event handler in gracefulShutdown + finalReport = report; await gracefulShutdown(); }); @@ -372,12 +358,11 @@ RunCommand.runCommandImplementation = async function (flags, argv, args) { launcher.run(); - // TODO: Extract this + let finalReport = {}; let shuttingDown = false; process.once('SIGINT', gracefulShutdown); process.once('SIGTERM', gracefulShutdown); - // TODO: beforeExit event handlers need to fire here async function gracefulShutdown(opts = { exitCode: 0 }) { debug('shutting down 🦑'); if (shuttingDown) { @@ -389,8 +374,24 @@ RunCommand.runCommandImplementation = async function (flags, argv, args) { shuttingDown = true; global.artillery.globalEvents.emit('shutdown:start', opts); + // Run beforeExit first, and then onShutdown + + const ps = []; for (const e of global.artillery.extensionEvents) { - const ps = []; + const testInfo = { endTime: Date.now() }; + if (e.ext === 'beforeExit') { + ps.push( + e.method({ + report: finalReport, + flags, + runnerOpts, + testInfo + }) + ); + } + } + await Promise.allSettled(ps); + if (e.ext === 'onShutdown') { ps.push(e.method(opts)); }