refactor: process and signals
This commit is contained in:
parent
94a606c5d1
commit
08898a5e19
|
@ -14,7 +14,7 @@
|
|||
"dox:serve": "flecks dox docusaurus && cd website && DOCUSAURUS_GENERATED_FILES_DIR_NAME=node_modules/.cache/docusaurus node_modules/.bin/docusaurus build --no-minify --out-dir ../dox-tmp && node_modules/.bin/docusaurus serve --dir ../dox-tmp",
|
||||
"dox": "flecks dox docusaurus && cd website && DOCUSAURUS_GENERATED_FILES_DIR_NAME=node_modules/.cache/docusaurus node_modules/.bin/docusaurus",
|
||||
"lint": "node build/tasks lint",
|
||||
"test": "node build/tasks -- test -t 60000"
|
||||
"test": "node build/tasks -- test -t 300000"
|
||||
},
|
||||
"devDependencies": {
|
||||
"husky": "^9.0.7",
|
||||
|
|
|
@ -18,6 +18,7 @@ const {
|
|||
const D = require('@flecks/core/build/debug');
|
||||
const {
|
||||
add,
|
||||
binaryPath,
|
||||
lockFile,
|
||||
spawnWith,
|
||||
} = require('@flecks/core/src/server');
|
||||
|
@ -178,7 +179,7 @@ exports.commands = (program, flecks) => {
|
|||
debug('Building...', opts);
|
||||
const webpackConfig = await flecks.resolveBuildConfig('fleckspack.config.js');
|
||||
const cmd = [
|
||||
'npx', 'webpack',
|
||||
await binaryPath('webpack'),
|
||||
...((watch || hot) ? ['watch'] : []),
|
||||
'--config', webpackConfig,
|
||||
'--mode', (production && !hot) ? 'production' : 'development',
|
||||
|
@ -210,7 +211,7 @@ exports.commands = (program, flecks) => {
|
|||
.map((pkg) => join(process.cwd(), pkg))
|
||||
.map(async (cwd) => {
|
||||
const cmd = [
|
||||
'npx', 'eslint',
|
||||
await binaryPath('eslint'),
|
||||
'--config', await flecks.resolveBuildConfig('eslint.config.js'),
|
||||
'.',
|
||||
];
|
||||
|
|
|
@ -1,10 +1,22 @@
|
|||
const {spawn} = require('child_process');
|
||||
const {exec, spawn} = require('child_process');
|
||||
|
||||
const D = require('../../build/debug');
|
||||
|
||||
const debug = D('@flecks/core/server');
|
||||
const debugSilly = debug.extend('silly');
|
||||
|
||||
exports.binaryPath = (binary) => (
|
||||
new Promise((resolve, reject) => {
|
||||
exec(`npx which ${binary}`, (error, stdout) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve(stdout.trim());
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
exports.processCode = (child) => new Promise((resolve, reject) => {
|
||||
child.on('error', reject);
|
||||
child.on('exit', (code) => {
|
||||
|
@ -13,6 +25,8 @@ exports.processCode = (child) => new Promise((resolve, reject) => {
|
|||
});
|
||||
});
|
||||
|
||||
const children = [];
|
||||
|
||||
exports.spawnWith = (cmd, opts = {}) => {
|
||||
debug("spawning: '%s'", cmd.join(' '));
|
||||
debugSilly('with options: %O', opts);
|
||||
|
@ -24,5 +38,31 @@ exports.spawnWith = (cmd, opts = {}) => {
|
|||
...opts.env,
|
||||
},
|
||||
});
|
||||
children.push(child);
|
||||
child.on('exit', () => {
|
||||
children.splice(children.indexOf(child), 1);
|
||||
});
|
||||
return child;
|
||||
};
|
||||
|
||||
let killed = false;
|
||||
|
||||
function handleTerminationEvent(signal) {
|
||||
// Clean up on exit.
|
||||
process.on(signal, () => {
|
||||
if (killed) {
|
||||
return;
|
||||
}
|
||||
killed = true;
|
||||
children.forEach((child) => {
|
||||
child.kill();
|
||||
});
|
||||
if ('exit' !== signal) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
handleTerminationEvent('exit');
|
||||
handleTerminationEvent('SIGINT');
|
||||
handleTerminationEvent('SIGTERM');
|
||||
|
|
|
@ -58,7 +58,7 @@ module.exports = async (env, argv, flecks) => {
|
|||
NODE_PRESERVE_SYMLINKS: flecks.roots.some(([path, request]) => path !== request) ? 1 : 0,
|
||||
},
|
||||
exec: 'index.js',
|
||||
killOnExit: !!hot,
|
||||
killOnExit: !hot,
|
||||
// Bail hard on unhandled rejections and report.
|
||||
nodeArgs: [...nodeArgs, '--unhandled-rejections=strict', '--trace-uncaught'],
|
||||
// HMR.
|
||||
|
|
|
@ -16,6 +16,12 @@ class StartServerPlugin {
|
|||
signal: false,
|
||||
...('string' === typeof options ? {name: options} : options),
|
||||
};
|
||||
['exit', 'SIGINT', 'SIGTERM']
|
||||
.forEach((event) => {
|
||||
process.on(event, () => {
|
||||
this.worker.kill('exit' === event ? 'SIGKILL' : event);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
|
@ -85,23 +91,21 @@ class StartServerPlugin {
|
|||
args,
|
||||
...(inspectPort && {inspectPort}),
|
||||
});
|
||||
const setupListeners = (worker) => {
|
||||
this.worker = cluster.fork(env);
|
||||
if (killOnExit) {
|
||||
worker.on('exit', () => {
|
||||
process.exit();
|
||||
this.worker.on('exit', (code) => {
|
||||
process.exit(code);
|
||||
});
|
||||
}
|
||||
worker.on('message', (message) => {
|
||||
if ('hmr-restart' === message) {
|
||||
else {
|
||||
this.worker.on('disconnect', () => {
|
||||
if (this.worker.exitedAfterDisconnect) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('[HMR] Restarting application...');
|
||||
this.worker = cluster.fork(env);
|
||||
setupListeners(this.worker);
|
||||
}
|
||||
});
|
||||
};
|
||||
this.worker = cluster.fork(env);
|
||||
setupListeners(this.worker);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
this.worker.on('error', reject);
|
||||
this.worker.on('online', resolve);
|
||||
|
|
|
@ -35,7 +35,7 @@ import {D, Flecks} from '@flecks/core';
|
|||
if (module.hot) {
|
||||
module.hot.accept('./runtime', () => {
|
||||
if (cluster.isWorker) {
|
||||
cluster.worker.send('hmr-restart');
|
||||
cluster.worker.disconnect();
|
||||
const error = new Error('Restart requested!');
|
||||
error.stack = '';
|
||||
throw error;
|
||||
|
|
|
@ -2,7 +2,7 @@ import {cp, mkdir} from 'fs/promises';
|
|||
import {join} from 'path';
|
||||
|
||||
import {rimraf} from '@flecks/build/server';
|
||||
import {processCode, spawnWith} from '@flecks/core/server';
|
||||
import {binaryPath, processCode, spawnWith} from '@flecks/core/server';
|
||||
|
||||
import {listen} from './listen';
|
||||
|
||||
|
@ -21,9 +21,9 @@ export async function createApplicationAt(path) {
|
|||
return qualified;
|
||||
}
|
||||
|
||||
export function build(path, {args = [], opts = {}} = {}) {
|
||||
return processCode(spawnWith(
|
||||
['npx', 'flecks', 'build', ...args],
|
||||
export async function buildChild(path, {args = [], opts = {}} = {}) {
|
||||
return spawnWith(
|
||||
[await binaryPath('flecks'), 'build', ...args],
|
||||
{
|
||||
...opts,
|
||||
env: {
|
||||
|
@ -33,7 +33,11 @@ export function build(path, {args = [], opts = {}} = {}) {
|
|||
...opts.env,
|
||||
},
|
||||
},
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
export async function build(path, {args = [], opts = {}} = {}) {
|
||||
return processCode(await buildChild(path, {args, opts}));
|
||||
}
|
||||
|
||||
export async function serverActions(path, actions) {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import cluster from 'cluster';
|
||||
import {createConnection} from 'net';
|
||||
|
||||
const {
|
||||
|
@ -18,7 +19,12 @@ export const hooks = {
|
|||
if (!FLECKS_SERVER_TEST_SOCKET) {
|
||||
return;
|
||||
}
|
||||
const socket = createConnection({path: FLECKS_SERVER_TEST_SOCKET});
|
||||
const socket = createConnection(FLECKS_SERVER_TEST_SOCKET);
|
||||
if (cluster.isWorker) {
|
||||
cluster.worker.on('disconnect', () => {
|
||||
socket.end();
|
||||
});
|
||||
}
|
||||
flecks.socket = socket;
|
||||
socket.on('connect', () => {
|
||||
socket.on('data', (data) => {
|
||||
|
|
|
@ -174,7 +174,7 @@ exports.hooks = {
|
|||
'--hot',
|
||||
'--config', await flecks.resolveBuildConfig('fleckspack.config.js'),
|
||||
];
|
||||
const child = spawnWith(
|
||||
spawnWith(
|
||||
cmd,
|
||||
{
|
||||
env: {
|
||||
|
@ -182,10 +182,6 @@ exports.hooks = {
|
|||
},
|
||||
},
|
||||
);
|
||||
// Clean up on exit.
|
||||
process.on('exit', () => {
|
||||
child.kill();
|
||||
});
|
||||
// Remove the build config since we're handing off to WDS.
|
||||
delete configs.web;
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue
Block a user