Skip to content

Commit

Permalink
fix(core): rework the daemon process communication to use a queue and…
Browse files Browse the repository at this point in the history
… a long running connection
  • Loading branch information
vsavkin committed Sep 13, 2022
1 parent aaad9a4 commit a3e480b
Show file tree
Hide file tree
Showing 18 changed files with 441 additions and 283 deletions.
10 changes: 5 additions & 5 deletions docs/generated/cli/daemon.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`

## Options

### background
### help

Type: boolean

Default: true
Show help

### help
### start

Type: boolean

Show help
Default: false

### start
### stop

Type: boolean

Expand Down
9 changes: 5 additions & 4 deletions docs/generated/cli/migrate.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
title: "migrate - CLI command"
description: "Creates a migrations file or runs migrations from the migrations file.
- Migrate packages and create migrations.json (e.g., nx migrate @nrwl/workspace@latest)
- Run migrations (e.g., nx migrate --run-migrations=migrations.json)"
title: 'migrate - CLI command'
description:
'Creates a migrations file or runs migrations from the migrations file.
- Migrate packages and create migrations.json (e.g., nx migrate @nrwl/workspace@latest)
- Run migrations (e.g., nx migrate --run-migrations=migrations.json)'
---

# migrate
Expand Down
4 changes: 2 additions & 2 deletions docs/generated/packages/nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"name": "daemon",
"id": "daemon",
"file": "generated/cli/daemon",
"content": "---\ntitle: 'daemon - CLI command'\ndescription: 'Prints information about the Nx Daemon process or starts a daemon process'\n---\n\n# daemon\n\nPrints information about the Nx Daemon process or starts a daemon process\n\n## Usage\n\n```bash\nnx daemon\n```\n\nInstall `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpx nx`.\n\n## Options\n\n### background\n\nType: boolean\n\nDefault: true\n\n### help\n\nType: boolean\n\nShow help\n\n### start\n\nType: boolean\n\nDefault: false\n\n### version\n\nType: boolean\n\nShow version number\n"
"content": "---\ntitle: 'daemon - CLI command'\ndescription: 'Prints information about the Nx Daemon process or starts a daemon process'\n---\n\n# daemon\n\nPrints information about the Nx Daemon process or starts a daemon process\n\n## Usage\n\n```bash\nnx daemon\n```\n\nInstall `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpx nx`.\n\n## Options\n\n### help\n\nType: boolean\n\nShow help\n\n### start\n\nType: boolean\n\nDefault: false\n\n### stop\n\nType: boolean\n\nDefault: false\n\n### version\n\nType: boolean\n\nShow version number\n"
},
{
"name": "graph",
Expand Down Expand Up @@ -94,7 +94,7 @@
"name": "migrate",
"id": "migrate",
"file": "generated/cli/migrate",
"content": "---\ntitle: \"migrate - CLI command\"\ndescription: \"Creates a migrations file or runs migrations from the migrations file.\n- Migrate packages and create migrations.json (e.g., nx migrate @nrwl/workspace@latest)\n- Run migrations (e.g., nx migrate --run-migrations=migrations.json)\"\n---\n\n# migrate\n\nCreates a migrations file or runs migrations from the migrations file.\n\n- Migrate packages and create migrations.json (e.g., nx migrate @nrwl/workspace@latest)\n- Run migrations (e.g., nx migrate --run-migrations=migrations.json)\n\n## Usage\n\n```bash\nnx migrate [packageAndVersion]\n```\n\nInstall `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpx nx`.\n\n### Examples\n\nUpdate @nrwl/workspace to \"next\". This will update other packages and will generate migrations.json:\n\n```bash\nnx migrate next\n```\n\nUpdate @nrwl/workspace to \"9.0.0\". This will update other packages and will generate migrations.json:\n\n```bash\nnx migrate 9.0.0\n```\n\nUpdate @nrwl/workspace and generate the list of migrations starting with version 8.0.0 of @nrwl/workspace and @nrwl/node, regardless of what installed locally:\n\n```bash\nnx migrate @nrwl/[email protected] --from=\"@nrwl/[email protected],@nrwl/[email protected]\"\n```\n\nUpdate @nrwl/workspace to \"9.0.0\". If it tries to update @nrwl/react or @nrwl/angular, use version \"9.0.1\":\n\n```bash\nnx migrate @nrwl/[email protected] --to=\"@nrwl/[email protected],@nrwl/[email protected]\"\n```\n\nUpdate another-package to \"12.0.0\". This will update other packages and will generate migrations.json file:\n\n```bash\nnx migrate [email protected]\n```\n\nRun migrations from the provided migrations.json file. You can modify migrations.json and run this command many times:\n\n```bash\nnx migrate --run-migrations=migrations.json\n```\n\nCreate a dedicated commit for each successfully completed migration. You can customize the prefix used for each commit by additionally setting --commit-prefix=\"PREFIX_HERE \":\n\n```bash\nnx migrate --run-migrations --create-commits\n```\n\n## Options\n\n### commitPrefix\n\nType: string\n\nDefault: chore: [nx migration]\n\nCommit prefix to apply to the commit for each migration, when --create-commits is enabled\n\n### createCommits\n\nType: boolean\n\nDefault: false\n\nAutomatically create a git commit after each migration runs\n\n### from\n\nType: string\n\nUse the provided versions for packages instead of the ones installed in node_modules (e.g., --from=\"@nrwl/react:12.0.0,@nrwl/js:12.0.0\")\n\n### help\n\nType: boolean\n\nShow help\n\n### packageAndVersion\n\nType: string\n\nThe target package and version (e.g, @nrwl/[email protected])\n\n### runMigrations\n\nType: string\n\nExecute migrations from a file (when the file isn't provided, execute migrations from migrations.json)\n\n### to\n\nType: string\n\nUse the provided versions for packages instead of the ones calculated by the migrator (e.g., --to=\"@nrwl/react:12.0.0,@nrwl/js:12.0.0\")\n\n### version\n\nType: boolean\n\nShow version number\n"
"content": "---\ntitle: 'migrate - CLI command'\ndescription:\n 'Creates a migrations file or runs migrations from the migrations file.\n - Migrate packages and create migrations.json (e.g., nx migrate @nrwl/workspace@latest)\n - Run migrations (e.g., nx migrate --run-migrations=migrations.json)'\n---\n\n# migrate\n\nCreates a migrations file or runs migrations from the migrations file.\n\n- Migrate packages and create migrations.json (e.g., nx migrate @nrwl/workspace@latest)\n- Run migrations (e.g., nx migrate --run-migrations=migrations.json)\n\n## Usage\n\n```bash\nnx migrate [packageAndVersion]\n```\n\nInstall `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpx nx`.\n\n### Examples\n\nUpdate @nrwl/workspace to \"next\". This will update other packages and will generate migrations.json:\n\n```bash\nnx migrate next\n```\n\nUpdate @nrwl/workspace to \"9.0.0\". This will update other packages and will generate migrations.json:\n\n```bash\nnx migrate 9.0.0\n```\n\nUpdate @nrwl/workspace and generate the list of migrations starting with version 8.0.0 of @nrwl/workspace and @nrwl/node, regardless of what installed locally:\n\n```bash\nnx migrate @nrwl/[email protected] --from=\"@nrwl/[email protected],@nrwl/[email protected]\"\n```\n\nUpdate @nrwl/workspace to \"9.0.0\". If it tries to update @nrwl/react or @nrwl/angular, use version \"9.0.1\":\n\n```bash\nnx migrate @nrwl/[email protected] --to=\"@nrwl/[email protected],@nrwl/[email protected]\"\n```\n\nUpdate another-package to \"12.0.0\". This will update other packages and will generate migrations.json file:\n\n```bash\nnx migrate [email protected]\n```\n\nRun migrations from the provided migrations.json file. You can modify migrations.json and run this command many times:\n\n```bash\nnx migrate --run-migrations=migrations.json\n```\n\nCreate a dedicated commit for each successfully completed migration. You can customize the prefix used for each commit by additionally setting --commit-prefix=\"PREFIX_HERE \":\n\n```bash\nnx migrate --run-migrations --create-commits\n```\n\n## Options\n\n### commitPrefix\n\nType: string\n\nDefault: chore: [nx migration]\n\nCommit prefix to apply to the commit for each migration, when --create-commits is enabled\n\n### createCommits\n\nType: boolean\n\nDefault: false\n\nAutomatically create a git commit after each migration runs\n\n### from\n\nType: string\n\nUse the provided versions for packages instead of the ones installed in node_modules (e.g., --from=\"@nrwl/react:12.0.0,@nrwl/js:12.0.0\")\n\n### help\n\nType: boolean\n\nShow help\n\n### packageAndVersion\n\nType: string\n\nThe target package and version (e.g, @nrwl/[email protected])\n\n### runMigrations\n\nType: string\n\nExecute migrations from a file (when the file isn't provided, execute migrations from migrations.json)\n\n### to\n\nType: string\n\nUse the provided versions for packages instead of the ones calculated by the migrator (e.g., --to=\"@nrwl/react:12.0.0,@nrwl/js:12.0.0\")\n\n### version\n\nType: boolean\n\nShow version number\n"
},
{
"name": "report",
Expand Down
8 changes: 4 additions & 4 deletions packages/nx/bin/compute-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { buildProjectGraphWithoutDaemon } from '../src/project-graph/project-gra
import { workspaceRoot } from '../src/utils/workspace-root';
import { fileExists } from '../src/utils/fileutils';
import { join } from 'path';
import { isServerAvailable, stop } from '../src/daemon/client/client';
import { daemonClient } from '../src/daemon/client/client';

(async () => {
try {
if (fileExists(join(workspaceRoot, 'nx.json'))) {
if (await isServerAvailable()) {
await stop();
}
try {
await daemonClient.stop();
} catch (e) {}
const b = new Date();
await buildProjectGraphWithoutDaemon();
const a = new Date();
Expand Down
12 changes: 5 additions & 7 deletions packages/nx/src/command-line/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ import { generateDaemonHelpOutput } from '../daemon/client/generate-help-output'

export async function daemonHandler(args: Arguments) {
if (args.start) {
const { startInBackground, startInCurrentProcess } = await import(
'../daemon/client/client'
);
if (!args.background) {
return startInCurrentProcess();
}
const pid = await startInBackground();
const { daemonClient } = await import('../daemon/client/client');
const pid = await daemonClient.startInBackground();
output.log({
title: `Daemon Server - Started in a background process...`,
bodyLines: [
Expand All @@ -20,6 +15,9 @@ export async function daemonHandler(args: Arguments) {
)} ${DAEMON_OUTPUT_LOG_FILE}\n`,
],
});
} else if (args.stop) {
const { daemonClient } = await import('../daemon/client/client');
daemonClient.stop();
} else {
console.log(generateDaemonHelpOutput());
}
Expand Down
107 changes: 74 additions & 33 deletions packages/nx/src/command-line/nx-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ export const commandsObject = yargs
withAffectedOptions(withPlainOption(yargs)),
'affected:apps'
),
handler: async (args) =>
(await import('./affected')).affected('apps', { ...args }),
handler: async (args) => {
await (await import('./affected')).affected('apps', { ...args });
process.exit(0);
},
})
.command({
command: 'affected:libs',
Expand All @@ -158,10 +160,14 @@ export const commandsObject = yargs
withAffectedOptions(withPlainOption(yargs)),
'affected:libs'
),
handler: async (args) =>
(await import('./affected')).affected('libs', {
handler: async (args) => {
await (
await import('./affected')
).affected('libs', {
...args,
}),
});
process.exit(0);
},
})
.command({
command: 'affected:graph',
Expand All @@ -172,10 +178,14 @@ export const commandsObject = yargs
withAffectedOptions(withDepGraphOptions(yargs)),
'affected:graph'
),
handler: async (args) =>
(await import('./affected')).affected('graph', {
handler: async (args) => {
await (
await import('./affected')
).affected('graph', {
...args,
}),
});
process.exit(0);
},
})
.command({
command: 'print-affected',
Expand All @@ -186,18 +196,19 @@ export const commandsObject = yargs
withAffectedOptions(withPrintAffectedOptions(yargs)),
'print-affected'
),
handler: async (args) =>
(await import('./affected')).affected(
'print-affected',
withOverrides(args)
),
handler: async (args) => {
await (
await import('./affected')
).affected('print-affected', withOverrides(args));
process.exit(0);
},
})
.command({
command: 'daemon',
describe:
'Prints information about the Nx Daemon process or starts a daemon process',
builder: (yargs) =>
linkToNxDevAndExamples(withDaemonStartOptions(yargs), 'daemon'),
linkToNxDevAndExamples(withDaemonOptions(yargs), 'daemon'),
handler: async (args) => (await import('./daemon')).daemonHandler(args),
})

Expand All @@ -207,29 +218,40 @@ export const commandsObject = yargs
aliases: ['dep-graph'],
builder: (yargs) =>
linkToNxDevAndExamples(withDepGraphOptions(yargs), 'dep-graph'),
handler: async (args) =>
(await import('./dep-graph')).generateGraph(args as any, []),
handler: async (args) => {
await (await import('./dep-graph')).generateGraph(args as any, []);
process.exit(0);
},
})

.command({
command: 'format:check',
describe: 'Check for un-formatted files',
builder: (yargs) =>
linkToNxDevAndExamples(withFormatOptions(yargs), 'format:check'),
handler: async (args) => (await import('./format')).format('check', args),
handler: async (args) => {
await (await import('./format')).format('check', args);
process.exit(0);
},
})
.command({
command: 'format:write',
describe: 'Overwrite un-formatted files',
aliases: ['format'],
builder: (yargs) =>
linkToNxDevAndExamples(withFormatOptions(yargs), 'format:write'),
handler: async (args) => (await import('./format')).format('write', args),
handler: async (args) => {
await (await import('./format')).format('write', args);
process.exit(0);
},
})
.command({
command: 'workspace-lint [files..]',
describe: 'Lint nx specific workspace files (nx.json, workspace.json)',
handler: async () => (await import('./lint')).workspaceLint(),
handler: async () => {
await (await import('./lint')).workspaceLint();
process.exit(0);
},
})

.command({
Expand All @@ -241,37 +263,51 @@ export const commandsObject = yargs
await withWorkspaceGeneratorOptions(yargs),
'workspace-generator'
),
handler: async () =>
(await import('./workspace-generators')).workspaceGenerators(
process.argv.slice(3)
),
handler: async () => {
await (
await import('./workspace-generators')
).workspaceGenerators(process.argv.slice(3));
process.exit(0);
},
})
.command({
command: 'migrate [packageAndVersion]',
describe: `Creates a migrations file or runs migrations from the migrations file.
- Migrate packages and create migrations.json (e.g., nx migrate @nrwl/workspace@latest)
- Run migrations (e.g., nx migrate --run-migrations=migrations.json)`,
- Migrate packages and create migrations.json (e.g., nx migrate @nrwl/workspace@latest)
- Run migrations (e.g., nx migrate --run-migrations=migrations.json)`,
builder: (yargs) =>
linkToNxDevAndExamples(withMigrationOptions(yargs), 'migrate'),
handler: () => runMigration(),
handler: () => {
runMigration();
process.exit(0);
},
})
.command({
command: 'report',
describe:
'Reports useful version numbers to copy into the Nx issue template',
handler: async () => (await import('./report')).reportHandler(),
handler: async () => {
await (await import('./report')).reportHandler();
process.exit(0);
},
})
.command({
command: 'init',
describe: 'Adds nx.json file and installs nx if not installed already',
handler: async () => (await import('./init')).initHandler(),
handler: async () => {
await (await import('./init')).initHandler();
process.exit(0);
},
})
.command({
command: 'list [plugin]',
describe:
'Lists installed plugins, capabilities of installed plugins and other available plugins.',
builder: (yargs) => withListOptions(yargs),
handler: async (args: any) => (await import('./list')).listHandler(args),
handler: async (args: any) => {
await (await import('./list')).listHandler(args);
process.exit(0);
},
})
.command({
command: 'reset',
Expand All @@ -284,8 +320,10 @@ export const commandsObject = yargs
command: 'connect-to-nx-cloud',
describe: `Makes sure the workspace is connected to Nx Cloud`,
builder: (yargs) => linkToNxDevAndExamples(yargs, 'connect-to-nx-cloud'),
handler: async () =>
(await import('./connect-to-nx-cloud')).connectToNxCloudCommand(),
handler: async () => {
await (await import('./connect-to-nx-cloud')).connectToNxCloudCommand();
process.exit(0);
},
})
.command({
command: 'new [_..]',
Expand Down Expand Up @@ -342,12 +380,15 @@ function withFormatOptions(yargs: yargs.Argv): yargs.Argv {
});
}

function withDaemonStartOptions(yargs: yargs.Argv): yargs.Argv {
function withDaemonOptions(yargs: yargs.Argv): yargs.Argv {
return yargs
.option('background', { type: 'boolean', default: true })
.option('start', {
type: 'boolean',
default: false,
})
.option('stop', {
type: 'boolean',
default: false,
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/reset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { removeSync } from 'fs-extra';
import { stop as stopDaemon } from '../daemon/client/client';
import { daemonClient } from '../daemon/client/client';
import { cacheDir, projectGraphCacheDirectory } from '../utils/cache-directory';
import { output } from '../utils/output';

Expand All @@ -8,7 +8,7 @@ export function resetHandler() {
title: 'Resetting the Nx workspace cache and stopping the Nx Daemon.',
bodyLines: [`This might take a few minutes.`],
});
stopDaemon();
daemonClient.stop();
removeSync(cacheDir);
if (projectGraphCacheDirectory !== cacheDir) {
removeSync(projectGraphCacheDirectory);
Expand Down
Loading

0 comments on commit a3e480b

Please sign in to comment.