Skip to content

Commit 058f36d

Browse files
committed
feat(core): introduce continuous tasks (#29750)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Summary An RFC about this feature is happening here: #29025. This has the most information about this feature. ## Current Behavior <!-- This is the behavior we have today --> Nx currently does not explicitly handle tasks which run continuously until they are terminated. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> This PR adds the initial support for continuous tasks which run continuously until they are terminated. This adds the ability to depend on continuous tasks. There is some more work to be done but this will be enough as an MVP. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent d62b94f commit 058f36d

29 files changed

+1847
-859
lines changed

docs/generated/devkit/TargetConfiguration.md

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Target's configuration
1515
- [cache](../../devkit/documents/TargetConfiguration#cache): boolean
1616
- [command](../../devkit/documents/TargetConfiguration#command): string
1717
- [configurations](../../devkit/documents/TargetConfiguration#configurations): Object
18+
- [continuous](../../devkit/documents/TargetConfiguration#continuous): boolean
1819
- [defaultConfiguration](../../devkit/documents/TargetConfiguration#defaultconfiguration): string
1920
- [dependsOn](../../devkit/documents/TargetConfiguration#dependson): (string | TargetDependencyConfig)[]
2021
- [executor](../../devkit/documents/TargetConfiguration#executor): string
@@ -55,6 +56,14 @@ Sets of options
5556

5657
---
5758

59+
### continuous
60+
61+
`Optional` **continuous**: `boolean`
62+
63+
Whether this target runs continuously
64+
65+
---
66+
5867
### defaultConfiguration
5968

6069
`Optional` **defaultConfiguration**: `string`

docs/generated/devkit/Task.md

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ A representation of the invocation of an Executor
77
### Properties
88

99
- [cache](../../devkit/documents/Task#cache): boolean
10+
- [continuous](../../devkit/documents/Task#continuous): boolean
1011
- [endTime](../../devkit/documents/Task#endtime): number
1112
- [hash](../../devkit/documents/Task#hash): string
1213
- [hashDetails](../../devkit/documents/Task#hashdetails): Object
@@ -28,6 +29,14 @@ Determines if a given task should be cacheable.
2829

2930
---
3031

32+
### continuous
33+
34+
`Optional` **continuous**: `boolean`
35+
36+
This denotes if the task runs continuously
37+
38+
---
39+
3140
### endTime
3241

3342
`Optional` **endTime**: `number`

docs/generated/devkit/TaskGraph.md

+7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ Graph of Tasks to be executed
66

77
### Properties
88

9+
- [continuousDependencies](../../devkit/documents/TaskGraph#continuousdependencies): Record<string, string[]>
910
- [dependencies](../../devkit/documents/TaskGraph#dependencies): Record<string, string[]>
1011
- [roots](../../devkit/documents/TaskGraph#roots): string[]
1112
- [tasks](../../devkit/documents/TaskGraph#tasks): Record<string, Task>
1213

1314
## Properties
1415

16+
### continuousDependencies
17+
18+
**continuousDependencies**: `Record`\<`string`, `string`[]\>
19+
20+
---
21+
1522
### dependencies
1623

1724
**dependencies**: `Record`\<`string`, `string`[]\>

packages/js/src/utils/buildable-libs-utils.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ describe('calculateDependenciesFromTaskGraph', () => {
394394
'lib3:build': [],
395395
'lib4:build': [],
396396
},
397+
continuousDependencies: {},
397398
roots: [],
398399
tasks: {
399400
'lib1:build': {
@@ -402,34 +403,39 @@ describe('calculateDependenciesFromTaskGraph', () => {
402403
target: { project: 'lib1', target: 'build' },
403404
outputs: [],
404405
parallelism: true,
406+
continuous: false,
405407
},
406408
'lib2:build': {
407409
id: 'lib2:build',
408410
overrides: {},
409411
target: { project: 'lib2', target: 'build' },
410412
outputs: [],
411413
parallelism: true,
414+
continuous: false,
412415
},
413416
'lib2:build-base': {
414417
id: 'lib2:build-base',
415418
overrides: {},
416419
target: { project: 'lib2', target: 'build-base' },
417420
outputs: [],
418421
parallelism: true,
422+
continuous: false,
419423
},
420424
'lib3:build': {
421425
id: 'lib3:build',
422426
overrides: {},
423427
target: { project: 'lib3', target: 'build' },
424428
outputs: [],
425429
parallelism: true,
430+
continuous: false,
426431
},
427432
'lib4:build': {
428433
id: 'lib4:build',
429434
overrides: {},
430435
target: { project: 'lib4', target: 'build' },
431436
outputs: [],
432437
parallelism: true,
438+
continuous: false,
433439
},
434440
},
435441
};
@@ -569,6 +575,7 @@ describe('calculateDependenciesFromTaskGraph', () => {
569575
'lib4:build': ['lib4:build-base'],
570576
'lib4:build-base': [],
571577
},
578+
continuousDependencies: {},
572579
roots: [],
573580
tasks: {
574581
'lib1:build': {
@@ -577,55 +584,63 @@ describe('calculateDependenciesFromTaskGraph', () => {
577584
target: { project: 'lib1', target: 'build' },
578585
outputs: [],
579586
parallelism: true,
587+
continuous: false,
580588
},
581589
'lib1:build-base': {
582590
id: 'lib1:build-base',
583591
overrides: {},
584592
target: { project: 'lib1', target: 'build-base' },
585593
outputs: [],
586594
parallelism: true,
595+
continuous: false,
587596
},
588597
'lib2:build': {
589598
id: 'lib2:build',
590599
overrides: {},
591600
target: { project: 'lib2', target: 'build' },
592601
outputs: [],
593602
parallelism: true,
603+
continuous: false,
594604
},
595605
'lib2:build-base': {
596606
id: 'lib2:build-base',
597607
overrides: {},
598608
target: { project: 'lib2', target: 'build-base' },
599609
outputs: [],
600610
parallelism: true,
611+
continuous: false,
601612
},
602613
'lib3:build': {
603614
id: 'lib3:build',
604615
overrides: {},
605616
target: { project: 'lib3', target: 'build' },
606617
outputs: [],
607618
parallelism: true,
619+
continuous: false,
608620
},
609621
'lib3:build-base': {
610622
id: 'lib3:build-base',
611623
overrides: {},
612624
target: { project: 'lib3', target: 'build-base' },
613625
outputs: [],
614626
parallelism: true,
627+
continuous: false,
615628
},
616629
'lib4:build': {
617630
id: 'lib4:build',
618631
overrides: {},
619632
target: { project: 'lib4', target: 'build' },
620633
outputs: [],
621634
parallelism: true,
635+
continuous: false,
622636
},
623637
'lib4:build-base': {
624638
id: 'lib4:build-base',
625639
overrides: {},
626640
target: { project: 'lib4', target: 'build-base' },
627641
outputs: [],
628642
parallelism: true,
643+
continuous: false,
629644
},
630645
},
631646
};
@@ -717,6 +732,7 @@ describe('calculateDependenciesFromTaskGraph', () => {
717732
// not relevant for this test case
718733
const taskGraph: TaskGraph = {
719734
dependencies: {},
735+
continuousDependencies: {},
720736
roots: [],
721737
tasks: {},
722738
};

packages/nx/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"string-width": "^4.2.3",
6767
"tar-stream": "~2.2.0",
6868
"tmp": "~0.2.1",
69+
"tree-kill": "^1.2.2",
6970
"tsconfig-paths": "^4.1.2",
7071
"tslib": "^2.3.0",
7172
"yaml": "^2.6.0",

packages/nx/src/command-line/graph/graph.ts

+2
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ function getAllTaskGraphsForWorkspace(projectGraph: ProjectGraph): {
980980
taskGraphs[taskId] = {
981981
tasks: {},
982982
dependencies: {},
983+
continuousDependencies: {},
983984
roots: [],
984985
};
985986

@@ -1006,6 +1007,7 @@ function getAllTaskGraphsForWorkspace(projectGraph: ProjectGraph): {
10061007
taskGraphs[taskId] = {
10071008
tasks: {},
10081009
dependencies: {},
1010+
continuousDependencies: {},
10091011
roots: [],
10101012
};
10111013

packages/nx/src/commands-runner/get-command-projects.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ProjectGraph, ProjectGraphProjectNode } from '../config/project-graph';
2-
import { removeIdsFromGraph } from '../tasks-runner/utils';
32
import { NxArgs } from '../utils/command-line-utils';
43
import { CommandGraph } from './command-graph';
54
import { createCommandGraph } from './create-command-graph';
@@ -34,3 +33,35 @@ function getSortedProjects(
3433

3534
return getSortedProjects(newGraph, sortedProjects);
3635
}
36+
37+
function removeIdsFromGraph<T>(
38+
graph: {
39+
roots: string[];
40+
dependencies: Record<string, string[]>;
41+
},
42+
ids: string[],
43+
mapWithIds: Record<string, T>
44+
): {
45+
mapWithIds: Record<string, T>;
46+
roots: string[];
47+
dependencies: Record<string, string[]>;
48+
} {
49+
const filteredMapWithIds = {};
50+
const dependencies = {};
51+
const removedSet = new Set(ids);
52+
for (let id of Object.keys(mapWithIds)) {
53+
if (!removedSet.has(id)) {
54+
filteredMapWithIds[id] = mapWithIds[id];
55+
dependencies[id] = graph.dependencies[id].filter(
56+
(depId) => !removedSet.has(depId)
57+
);
58+
}
59+
}
60+
return {
61+
mapWithIds: filteredMapWithIds,
62+
dependencies: dependencies,
63+
roots: Object.keys(dependencies).filter(
64+
(k) => dependencies[k].length === 0
65+
),
66+
};
67+
}

packages/nx/src/config/task-graph.ts

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export interface Task {
8181
* Determines if a given task should be parallelizable.
8282
*/
8383
parallelism: boolean;
84+
85+
/**
86+
* This denotes if the task runs continuously
87+
*/
88+
continuous?: boolean;
8489
}
8590

8691
/**
@@ -99,4 +104,6 @@ export interface TaskGraph {
99104
* Map of Task IDs to IDs of tasks which the task depends on
100105
*/
101106
dependencies: Record<string, string[]>;
107+
108+
continuousDependencies: Record<string, string[]>;
102109
}

packages/nx/src/config/workspace-json-project-json.ts

+5
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ export interface TargetConfiguration<T = any> {
258258
*/
259259
parallelism?: boolean;
260260

261+
/**
262+
* Whether this target runs continuously
263+
*/
264+
continuous?: boolean;
265+
261266
/**
262267
* List of generators to run before the target to ensure the workspace
263268
* is up to date.

0 commit comments

Comments
 (0)