Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reset TTY on exit #685

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/openactive-broker-microservice/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/openactive-broker-microservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"axios": "^0.19.2",
"chai": "^4.3.4",
"chalk": "^4.1.0",
"cli-progress": "^3.9.0",
"cli-progress": "^3.12.0",
"config": "^3.2.4",
"debug": "~2.6.9",
"express": "^4.18.2",
Expand All @@ -41,6 +41,7 @@
"lodash": "^4.17.21",
"mkdirp": "^1.0.4",
"morgan": "~1.9.0",
"node-cleanup": "^2.1.2",
"p-memoize": "^3.1.0",
"ramda": "^0.28.0",
"remarkable": "^2.0.1",
Expand Down
9 changes: 9 additions & 0 deletions packages/openactive-broker-microservice/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { isNil, partialRight } = require('lodash');
const { createFeedContext, progressFromContext, harvestRPDELossless } = require('@openactive/harvesting-utils');
const { partial } = require('lodash');
const path = require('path');
const nodeCleanup = require('node-cleanup');

const { CriteriaOrientedOpportunityIdCache } = require('./util/criteria-oriented-opportunity-id-cache');
const { logError, logErrorDuringHarvest, log, logCharacter } = require('./util/log');
Expand Down Expand Up @@ -68,6 +69,14 @@ const { getSampleOpportunities } = require('./util/sample-opportunities');

const markdown = new Remarkable();

// This is run when the process exits
nodeCleanup(function () {
if (state.multibar !== null) {
// Restores terminal cursor settings before exiting
state.multibar.stop();
}
});

/**
* @param {import('express').Request} req
* @param {import('express').Response} res
Expand Down
13 changes: 11 additions & 2 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ let microservice = null;
let integrationTests = null;
let prompt;

// Note this is triggered in CI mode when Ctrl+C is pressed, as well as when the process receives a SIGINT signal
nodeCleanup(function (exitCode, signal) {
// The first time Ctrl+C is pressed in CI mode, kill the microservice and integrationTests,
// and uninstall the cleanup handler, then wait for the parent process to exit naturally
// once both child processes have exited.
if (microservice !== null) microservice.kill();
microservice = null;
if (integrationTests !== null) integrationTests.kill();
nodeCleanup.uninstall(); // don't call this cleanup handler again
return false; // don't exit yet, wait for child processes to exit
});

function setupEscapeKey()
Expand Down Expand Up @@ -82,7 +88,7 @@ if (!IS_RUNNING_IN_CI) {
`);
}

// Setup escape key to cancel running tests
// Setup escape key to cancel running tests, and handle Ctrl+C in interactive mode
readline.emitKeypressEvents(process.stdin);
process.stdin.on('keypress', (ch, key) => {
if (!key) {
Expand All @@ -91,7 +97,10 @@ if (!IS_RUNNING_IN_CI) {
if (key.name === 'escape') {
if (integrationTests !== null) integrationTests.kill();
} else if (key.ctrl && key.name === 'c') {
process.exit(); // eslint-disable-line unicorn/no-process-exit
// In interactive mode, a clean exit is achieved by killing the microservice first, which will then kill the integration tests:
// microservice.kill() -> integrationTests.kill() + process.exit()
// This is important as it allows the microservice to reset the terminal before it loses access to stdout at process.exit()
if (microservice !== null) microservice.kill();
}
});
setupEscapeKey();
Expand Down
Loading