Skip to content

Commit

Permalink
fix: 馃悰 EPIPE message on exit again
Browse files Browse the repository at this point in the history
  • Loading branch information
wKich committed May 22, 2020
1 parent 78c3d53 commit a5bb06d
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/creevey.ts
Expand Up @@ -13,7 +13,6 @@ process.on('unhandledRejection', (reason) => {
if (cluster.isWorker) return emitMessage<WorkerMessage>({ type: 'error', payload: { status: 'failed', error } });
if (cluster.isMaster) {
shutdownWorkers();
emitMessage<'shutdown'>('shutdown');
}
});

Expand Down
3 changes: 1 addition & 2 deletions src/server/master/index.ts
Expand Up @@ -5,7 +5,7 @@ import master from './master';
import creeveyServer from './server';
import creeveyApi from './api';
import { Config, Options, isDefined } from '../../types';
import { shutdownWorkers, emitMessage } from '../../utils';
import { shutdownWorkers } from '../../utils';

const copyFileAsync = promisify(copyFile);
const readdirAsync = promisify(readdir);
Expand Down Expand Up @@ -55,7 +55,6 @@ export default async function (config: Config, options: Options): Promise<void>
// TODO output summary
process.exitCode = isSuccess ? 0 : -1;
shutdownWorkers();
emitMessage<'shutdown'>('shutdown');
});
// TODO grep
runner.start(Object.keys(runner.status.tests));
Expand Down
4 changes: 1 addition & 3 deletions src/server/master/master.ts
Expand Up @@ -3,7 +3,7 @@ import { Config, Test, isDefined, ServerTest } from '../../types';
import { loadTestsFromStories } from '../../stories';
import Runner from './runner';
import { startWebpackCompiler } from './stories';
import { shutdownWorkers, emitMessage } from '../../utils';
import { shutdownWorkers } from '../../utils';

function mergeTests(
testsWithReports: Partial<{ [id: string]: Test }>,
Expand Down Expand Up @@ -50,12 +50,10 @@ export default async function master(config: Config): Promise<Runner> {
new Promise((resolve) => runner.once('stop', resolve)),
]).then(() => {
shutdownWorkers();
emitMessage<'shutdown'>('shutdown');
});
runner.stop();
} else {
shutdownWorkers();
emitMessage<'shutdown'>('shutdown');
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/server/master/pool.ts
Expand Up @@ -144,8 +144,8 @@ export default class Pool extends EventEmitter {

private gracefullyKill(worker: Worker): void {
const timeout = setTimeout(() => worker.kill(), 10000);
worker.on('disconnect', () => clearTimeout(timeout));
worker.send('shutdown');
worker.disconnect();
worker.on('disconnect', () => clearTimeout(timeout));
}
}
2 changes: 0 additions & 2 deletions src/server/master/webpack.ts
Expand Up @@ -141,8 +141,6 @@ export default async function compile(config: Config, { debug }: Options): Promi
const watcher = storybookWebpackCompiler.watch({}, handleWebpackBuild);

subscribeOn('shutdown', () => {
console.log('[CreeveyWebpack]: Stop watching and shuting down');
watcher.close(noop);
process.on('exit', () => console.log('[CreeveyWebpack]: process exiting'));
});
}
3 changes: 1 addition & 2 deletions src/server/worker/selenium.ts
Expand Up @@ -280,8 +280,7 @@ export async function getBrowser(config: Config, browserConfig: BrowserConfig):
const browser = await new Builder().usingServer(gridUrl).withCapabilities(capabilities).build();

subscribeOn('shutdown', () => {
console.log('[CreeveyWorker]:', `Closing browser session ${process.pid}`);
browser.quit().then(() => console.log('[CreeveyWorker]:', `Browser session closed ${process.pid}`));
browser.quit();
});

if (viewport) {
Expand Down
22 changes: 13 additions & 9 deletions src/utils.ts
Expand Up @@ -117,13 +117,17 @@ export function subscribeOn(
}

export function shutdownWorkers(): void {
Object.values(cluster.workers)
.filter(isDefined)
.filter((worker) => worker.isConnected())
.forEach((worker) => {
const timeout = setTimeout(() => worker.kill(), 10000);
worker.send('shutdown');
worker.disconnect();
worker.on('disconnect', () => clearTimeout(timeout));
});
emitMessage<'shutdown'>('shutdown');
// NOTE Some workers exit on SIGINT, we need to wait a little to kill the remaining
setTimeout(() =>
Object.values(cluster.workers)
.filter(isDefined)
.filter((worker) => worker.isConnected())
.forEach((worker) => {
const timeout = setTimeout(() => worker.kill(), 10000);
worker.on('disconnect', () => clearTimeout(timeout));
worker.send('shutdown');
worker.disconnect();
}),
);
}

0 comments on commit a5bb06d

Please sign in to comment.