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(js): unique node execution #13813

Merged
merged 1 commit into from Dec 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions packages/js/src/executors/node/node.impl.ts
Expand Up @@ -7,6 +7,7 @@ import {
} from '@nrwl/devkit';
import { calculateProjectDependencies } from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { ChildProcess, fork } from 'child_process';
import { randomUUID } from 'crypto';
import { HashingImpl } from 'nx/src/hasher/hashing-impl';
import * as treeKill from 'tree-kill';
import { promisify } from 'util';
Expand All @@ -25,16 +26,17 @@ export async function* nodeExecutor(
options: NodeExecutorOptions,
context: ExecutorContext
) {
const uniqueKey = randomUUID();
process.on('SIGTERM', async () => {
await killCurrentProcess(options, 'SIGTERM');
await killCurrentProcess(uniqueKey, options, 'SIGTERM');
process.exit(128 + 15);
});
process.on('SIGINT', async () => {
await killCurrentProcess(options, 'SIGINT');
await killCurrentProcess(uniqueKey, options, 'SIGINT');
process.exit(128 + 2);
});
process.on('SIGHUP', async () => {
await killCurrentProcess(options, 'SIGHUP');
await killCurrentProcess(uniqueKey, options, 'SIGHUP');
process.exit(128 + 1);
});

Expand All @@ -55,7 +57,7 @@ export async function* nodeExecutor(
logger.error('There was an error with the build. See above.');
logger.info(`${event.outfile} was not restarted.`);
}
await handleBuildEvent(event, options, mappings);
await handleBuildEvent(uniqueKey, event, options, mappings);
yield event;
}
}
Expand All @@ -81,14 +83,17 @@ function calculateResolveMappings(
}

async function runProcess(
uniqueKey: string,
event: ExecutorEvent,
options: NodeExecutorOptions,
mappings: { [project: string]: string }
) {
const execArgv = getExecArgv(options);

const hashed = hasher.hashArray(execArgv.concat(options.args));
hashedMap.set(options.args, hashed);

const hashedKey = [uniqueKey, ...options.args];
hashedMap.set(hashedKey, hashed);

const subProcess = fork(
joinPathFragments(__dirname, 'node-with-require-overrides'),
Expand Down Expand Up @@ -138,28 +143,31 @@ function getExecArgv(options: NodeExecutorOptions) {
}

async function handleBuildEvent(
uniqueKey: string,
event: ExecutorEvent,
options: NodeExecutorOptions,
mappings: { [project: string]: string }
) {
// Don't kill previous run unless new build is successful.
if (options.watch && event.success) {
await killCurrentProcess(options);
await killCurrentProcess(uniqueKey, options);
}

if (event.success) {
await runProcess(event, options, mappings);
await runProcess(uniqueKey, event, options, mappings);
}
}

const promisifiedTreeKill: (pid: number, signal: string) => Promise<void> =
promisify(treeKill);

async function killCurrentProcess(
uniqueKey: string,
options: NodeExecutorOptions,
signal: string = 'SIGTERM'
) {
const currentProcessKey = hashedMap.get(options.args);
const hashedKey = [uniqueKey, ...options.args];
const currentProcessKey = hashedMap.get(hashedKey);
if (!currentProcessKey) return;

const currentProcess = processMap.get(currentProcessKey);
Expand All @@ -182,7 +190,7 @@ async function killCurrentProcess(
}
} finally {
processMap.delete(currentProcessKey);
hashedMap.delete(options.args);
hashedMap.delete(hashedKey);
}
}

Expand Down