Skip to content

Commit

Permalink
refactor: make sure beforeExit handlers are always run
Browse files Browse the repository at this point in the history
  • Loading branch information
hassy committed Jul 19, 2023
1 parent c4bbde6 commit 77f6423
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions packages/artillery/lib/cmds/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}
Expand Down

0 comments on commit 77f6423

Please sign in to comment.