Skip to content

Commit

Permalink
fix(js): unique node execution (#13813)
Browse files Browse the repository at this point in the history
  • Loading branch information
wSedlacek committed Dec 14, 2022
1 parent a854660 commit 82d44ad
Showing 1 changed file with 17 additions and 9 deletions.
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

0 comments on commit 82d44ad

Please sign in to comment.