Skip to content

Commit

Permalink
feat(bootstrap): remove exitOnUncaught (#694)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `exitOnUncaught` is removed. Use Node.js `--unhandled-rejections` instead
  • Loading branch information
Hoishin committed Jan 2, 2024
1 parent 9d5d93b commit 0c33f5a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 71 deletions.
78 changes: 22 additions & 56 deletions src/server/bootstrap.ts
Expand Up @@ -29,72 +29,38 @@ if (!process.env.NODECG_ROOT) {
}

// Ours
import { pjson, asyncExitHook } from './util';
import { pjson } from './util';
import NodeCGServer from './server';
import { gracefulExit } from './util/exit-hook';
import exitHook from './util/exit-hook';

process.title = 'NodeCG';
global.exitOnUncaught = true;

process.title += ` - ${String(pjson.version)}`;

process.on('uncaughtException', (err) => {
if (!global.sentryEnabled) {
if (global.exitOnUncaught) {
console.error('UNCAUGHT EXCEPTION! NodeCG will now exit.');
} else {
console.error('UNCAUGHT EXCEPTION!');
}

console.error(err);
if (global.exitOnUncaught) {
gracefulExit(1);
}
}
});

process.on('unhandledRejection', (err) => {
if (!global.sentryEnabled) {
console.error('UNHANDLED PROMISE REJECTION!');
console.error(err);
}
});

const server = new NodeCGServer();
server.on('error', () => {
gracefulExit(1);
});
server.on('stopped', () => {
if (!process.exitCode) {
gracefulExit(0);
}
});
server.start().catch((error) => {
console.error(error);
process.nextTick(() => {
gracefulExit(1);
});
});
void server.start();

asyncExitHook(
async () => {
await server.stop();
},
{
minimumWait: 100,
},
);
exitHook(() => {
server.stop();
});

// Check for updates
fetch('https://registry.npmjs.org/nodecg/latest')
.then((res: any) => res.json())
.then((body: any) => {
.then((res) => res.json())
.then((body) => {
if (!body || typeof body !== 'object' || !('version' in body) || typeof body.version !== 'string') {
return;
}
if (semver.gt(body.version, pjson.version)) {
console.warn('An update is available for NodeCG: %s (current: %s)', JSON.parse(body).version, pjson.version);
console.warn('An update is available for NodeCG: %s (current: %s)', body.version, pjson.version);
}
})
.catch(
/* istanbul ignore next */ () => {
// Discard errors.
},
);
.catch((error) => {
console.error('Failed to check for updates', error);
});

const hoge = async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
throw new Error('hoge');
};

void hoge();
2 changes: 0 additions & 2 deletions src/server/config/loader.ts
Expand Up @@ -25,8 +25,6 @@ function getConfigSchema(userConfig: Record<string, any>) {
"If you use a reverse proxy, you'll likely need to set this value.",
),

exitOnUncaught: Joi.boolean().default(true).description('Whether or not to exit on uncaught exceptions.'),

logging: Joi.object({
console: Joi.object({
enabled: Joi.boolean().default(true).description('Whether to enable console logging.'),
Expand Down
10 changes: 1 addition & 9 deletions src/server/server/index.ts
Expand Up @@ -6,7 +6,6 @@ import { config, filteredConfig } from '../config';
import '../util/sentry-config';
import { pjson } from '../util';

global.exitOnUncaught = config.exitOnUncaught;
if (config.sentry?.enabled) {
Sentry.init({
dsn: config.sentry.dsn,
Expand Down Expand Up @@ -368,17 +367,10 @@ export default class NodeCGServer extends TypedEmitter<EventMap> {
});
}

async stop(): Promise<void> {
stop() {
this._extensionManager?.emitToAllInstances('serverStopping');
this._io.disconnectSockets(true);

await new Promise<void>((resolve) => {
// Also closes the underlying HTTP server.
this._io.close(() => {
resolve();
});
});

if (this._replicator) {
this._replicator.saveAllReplicants();
}
Expand Down
1 change: 0 additions & 1 deletion src/server/types/augment-node-global.d.ts
Expand Up @@ -6,6 +6,5 @@ declare global {
* It'd be good to refactor the program to not need these, if possible.
* But, they aren't really hurting anything.
*/
var exitOnUncaught: boolean;
var sentryEnabled: boolean;
}
1 change: 0 additions & 1 deletion src/server/util/index.ts
Expand Up @@ -5,4 +5,3 @@ export { default as authCheck } from './authcheck';
export { default as throttleName } from './throttle-name';
export { default as pjson } from './pjson';
export { default as sendFile } from './sendFile';
export { asyncExitHook } from './exit-hook';
1 change: 0 additions & 1 deletion src/types/nodecg.ts
Expand Up @@ -216,7 +216,6 @@ export namespace NodeCG {
host: string;
port: number;
baseURL: string;
exitOnUncaught: boolean;
logging: {
console: {
enabled: boolean;
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/server.ts
Expand Up @@ -52,7 +52,7 @@ export const setup = (nodecgConfigName = 'nodecg.json'): void => {

test.after.always(() => {
if (server) {
void server.stop();
server.stop();
}
});

Expand Down

0 comments on commit 0c33f5a

Please sign in to comment.