Skip to content

Commit cf8861c

Browse files
committed
feat: Parallelized plans
1 parent 5404d44 commit cf8861c

5 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/entities/project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
7373
if (!this.planRequestsCache) {
7474
const { resourceConfigs } = this
7575
const stateOnlyConfigs = this.stateConfigs?.filter((s) =>
76-
resourceConfigs.find((r) => r.id === s.id) === undefined
76+
!resourceConfigs.some((r) => r.id === s.id)
7777
)
7878

7979
const inputRequests = [

src/plugins/message-sender.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IpcMessageV2 } from 'codify-schemas';
22
import { ChildProcess } from 'node:child_process';
3+
import EventEmitter from 'node:events';
34
import { clearTimeout } from 'node:timers';
45

56
import { ctx } from '../events/context.js';
@@ -9,6 +10,8 @@ import { ipcMessageValidator } from './plugin-process.js';
910
type Resolve<T> = (value: T) => void;
1011
type Reject = (reason?: Error) => void;
1112

13+
EventEmitter.defaultMaxListeners = 100;
14+
1215
// Default timeout is 10 minutes after last message, stdout, or stderr
1316
const TIMEOUT = 6_000_000
1417

src/plugins/plugin-manager.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,20 @@ export class PluginManager {
8585

8686
async getPlan(project: Project): Promise<Plan> {
8787
const result = new Array<ResourcePlan>();
88-
for (const id of project.evaluationOrder!) {
89-
const planRequest = project.getPlanRequest(id)!;
88+
await Promise.all(
89+
project.evaluationOrder!.map(async (id) => {
90+
const planRequest = project.getPlanRequest(id)!;
9091

91-
const pluginName = this.resourceToPluginMapping.get(planRequest.type);
92-
if (!pluginName) {
93-
throw new InternalError(`Unable to determine plugin for validated resource: ${planRequest.id}`);
94-
}
92+
const pluginName = this.resourceToPluginMapping.get(planRequest.type);
93+
if (!pluginName) {
94+
throw new InternalError(`Unable to determine plugin for validated resource: ${planRequest.id}`);
95+
}
9596

96-
const planResult = await this.plugins.get(pluginName)!.plan(planRequest);
97+
const planResult = await this.plugins.get(pluginName)!.plan(planRequest);
9798

98-
result.push(planResult);
99-
}
99+
result.push(planResult);
100+
})
101+
)
100102

101103
return new Plan(result, project);
102104
}

src/plugins/plugin-process.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export class PluginProcess {
4141
_process.stdout!.on('data', (message) => ctx.pluginStdout(name, message.toString('utf8')));
4242
_process.stderr!.on('data', (message) => ctx.pluginStderr(name, message.toString('utf8')));
4343
_process.on('exit', (code) => {
44-
throw new Error(`Plugin ${this.name} exited with code ${code}`);
44+
if (code && code !== 0) {
45+
throw new Error(`Plugin ${this.name} exited with code ${code}`);
46+
}
4547
})
4648
PluginProcess.handleSudoRequests(_process, name);
4749

src/utils/performance.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { performance } from 'node:perf_hooks';
2+
3+
export class Performance {
4+
static async measure<T>(fn: () => Promise<T> | T, tag?: string): Promise<T> {
5+
console.log(`${tag ?? 'Function measurement'} started`);
6+
7+
const start = performance.now();
8+
const result = await fn();
9+
10+
console.log(`${tag ?? 'Function measurement'} took ${(performance.now() - start).toFixed(3)} ms to complete`);
11+
return result;
12+
}
13+
14+
}

0 commit comments

Comments
 (0)