Merge pull request #555 from hashicorp/ICU-1335-close-all-sessions-on-exit

feat: Prompt before quitting inprogress session on desktop client quit
This commit is contained in:
Randall Morey authored and GitHub committed 2021-05-19 12:52:19 -04:00
commit d72b02ad29
5 files changed
+44 -6

No files matched your search

+4
View File
@@ -4,6 +4,10 @@ Canonical reference for changes, improvements, and bugfixes for Boundary Desktop
## Next
### New
- Add prompt to close sessions when quitting client. ([PR](https://github.com/hashicorp/boundary-ui/pull/555))
### Bug Fixes
- Close spawned boundary process on session cancel. ([PR](https://github.com/hashicorp/boundary-ui/pull/549))
+26 -1
View File
@@ -7,9 +7,9 @@ const {
const {
session,
app,
dialog,
protocol,
BrowserWindow,
ipcMain,
Menu,
MenuItem,
} = require('electron');
@@ -17,6 +17,7 @@ require('./ipc/handlers.js');
const runtimeSettings = require('./services/runtime-settings.js');
const { generateCSPHeader } = require('./config/content-security-policy.js');
const sessionManager = require('./services/session-manager.js');
const menu = require('./config/menu.js');
const appUpdater = require('./helpers/app-updater.js');
@@ -183,6 +184,30 @@ app.on('ready', async () => {
});
});
/**
* Prompt for closing spawned processes
*/
app.on('before-quit', (event) => {
if (sessionManager.hasRunningSessions) {
const dialogOpts = {
type: 'question',
buttons: ['Close', 'Cancel'],
detail: 'Close sessions before quitting?',
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
switch (returnValue.response) {
case 0:
sessionManager.stopAll();
break;
default:
event.preventDefault();
break;
}
});
}
});
// Handle an unhandled error in the main thread
//
// Note that 'uncaughtException' is a crude mechanism for exception handling intended to
@@ -3,6 +3,14 @@ const Session = require('./session.js');
class SessionManager {
#sessions = [];
/**
* Checks for running sessions
* @returns {boolean}
*/
get hasRunningSessions() {
return Boolean(this.#sessions.find((session) => session.isRunning));
}
/**
* Start a session and track it.
* Returns session proxy details if successfully started.
@@ -23,10 +31,11 @@ class SessionManager {
*/
stopById(session_id) {
const session = this.#sessions.find((session) => session.id === session_id);
return session.stop();
return session?.stop?.();
}
stopAll() {
this.#sessions.forEach((session) => session.stop());
}
}
@@ -36,7 +36,7 @@ class Session {
/**
* @return {boolean}
*/
get isActive() {
get isRunning() {
return !this.#process.killed;
}
@@ -60,12 +60,12 @@ class Session {
*/
stop() {
return new Promise((resolve, reject) => {
if (this.isActive) {
if (this.isRunning) {
this.#process.on('close', () => resolve());
this.#process.on('error', (e) => reject(e));
this.#process.kill();
} else {
// Do nothing when process isn't active
// Do nothing when process isn't running
resolve();
}
});
@@ -21,7 +21,7 @@ module.exports = {
urlValidate: (str) => {
try {
const url = new URL(str);
return `${url.protocol}://${url.host}`;
return `${url.protocol}//${url.host}`;
} catch (e) {
// Parse error
throw new Error(`URL ${str} could not be validated.`);