Skip to content

Commit 5598377

Browse files
ports fallback
1 parent 04ee301 commit 5598377

File tree

2 files changed

+118
-93
lines changed

2 files changed

+118
-93
lines changed

src/Session/tasks.ts

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ export class Tasks {
2727
*/
2828
async getAll(): Promise<Task[]> {
2929
if (!this.cachedTasks) {
30-
const tasks = await this.agentClient.tasks.getTasks();
30+
const [tasks, ports] = await Promise.all([
31+
this.agentClient.tasks.getTasks(),
32+
this.agentClient.ports.getPorts(),
33+
]);
3134

3235
this.cachedTasks = Object.values(tasks.tasks).map(
33-
(task) => new Task(this.agentClient, task)
36+
(task) => new Task(this.agentClient, task, ports)
3437
);
3538
}
3639

@@ -78,59 +81,82 @@ export class Task {
7881
return Boolean(this.data.runAtStart);
7982
}
8083
get ports() {
84+
const configuredPort = this.data.preview?.port;
85+
86+
// If we have configured a specific port, we only return that port. This is used where
87+
// the environment is not able to assign port automatically (e.g. Next JS)
88+
if (configuredPort) {
89+
return this._ports.filter((port) => port.port === configuredPort);
90+
}
91+
92+
// Otherwise, we return the ports assigned to the task
8193
return this.data.ports;
8294
}
8395
get status() {
8496
return this.shell?.status || "IDLE";
8597
}
8698
constructor(
8799
private agentClient: IAgentClient,
88-
private data: protocol.task.TaskDTO
100+
private data: protocol.task.TaskDTO,
101+
private _ports: protocol.port.Port[]
89102
) {
90-
agentClient.tasks.onTaskUpdate(async (task) => {
91-
if (task.id !== this.id) {
92-
return;
93-
}
94-
95-
const lastStatus = this.status;
96-
const lastShellId = this.shell?.shellId;
97-
98-
this.data = task;
99-
100-
if (lastStatus !== this.status) {
101-
this.onStatusChangeEmitter.fire(this.status);
102-
}
103-
104-
if (
105-
this.openedShell &&
106-
task.shell &&
107-
task.shell.shellId !== lastShellId
108-
) {
109-
const openedShell = await this.agentClient.shells.open(
110-
task.shell.shellId,
111-
this.openedShell.dimensions
112-
);
113-
114-
this.openedShell = {
115-
shellId: openedShell.shellId,
116-
output: openedShell.buffer,
117-
dimensions: this.openedShell.dimensions,
118-
};
119-
120-
this.onOutputEmitter.fire("\x1B[2J\x1B[3J\x1B[1;1H");
121-
openedShell.buffer.forEach((out) => this.onOutputEmitter.fire(out));
122-
}
123-
});
124-
125-
this.agentClient.shells.onShellOut(({ shellId, out }) => {
126-
if (!this.shell || this.shell.shellId !== shellId || !this.openedShell) {
127-
return;
128-
}
103+
this.disposable.addDisposable(
104+
agentClient.ports.onPortsUpdated((ports) => {
105+
this._ports = ports;
106+
})
107+
);
108+
this.disposable.addDisposable(
109+
agentClient.tasks.onTaskUpdate(async (task) => {
110+
if (task.id !== this.id) {
111+
return;
112+
}
113+
114+
const lastStatus = this.status;
115+
const lastShellId = this.shell?.shellId;
116+
117+
this.data = task;
118+
119+
if (lastStatus !== this.status) {
120+
this.onStatusChangeEmitter.fire(this.status);
121+
}
122+
123+
if (
124+
this.openedShell &&
125+
task.shell &&
126+
task.shell.shellId !== lastShellId
127+
) {
128+
const openedShell = await this.agentClient.shells.open(
129+
task.shell.shellId,
130+
this.openedShell.dimensions
131+
);
132+
133+
this.openedShell = {
134+
shellId: openedShell.shellId,
135+
output: openedShell.buffer,
136+
dimensions: this.openedShell.dimensions,
137+
};
138+
139+
this.onOutputEmitter.fire("\x1B[2J\x1B[3J\x1B[1;1H");
140+
openedShell.buffer.forEach((out) => this.onOutputEmitter.fire(out));
141+
}
142+
})
143+
);
129144

130-
// Update output for shell
131-
this.openedShell.output.push(out);
132-
this.onOutputEmitter.fire(out);
133-
});
145+
this.disposable.addDisposable(
146+
this.agentClient.shells.onShellOut(({ shellId, out }) => {
147+
if (
148+
!this.shell ||
149+
this.shell.shellId !== shellId ||
150+
!this.openedShell
151+
) {
152+
return;
153+
}
154+
155+
// Update output for shell
156+
this.openedShell.output.push(out);
157+
this.onOutputEmitter.fire(out);
158+
})
159+
);
134160
}
135161
async open(dimensions = DEFAULT_SHELL_SIZE) {
136162
if (!this.shell) {
@@ -169,6 +195,7 @@ export class Task {
169195
resolve(task.ports[0]);
170196
}
171197
});
198+
this.disposable.addDisposable(disposer);
172199
}),
173200
new Promise<protocol.port.Port>((resolve, reject) => {
174201
setTimeout(() => {
@@ -191,4 +218,7 @@ export class Task {
191218
await this.agentClient.tasks.stopTask(this.id);
192219
}
193220
}
221+
dispose() {
222+
this.disposable.dispose();
223+
}
194224
}

src/bin/commands/build.ts

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -372,61 +372,56 @@ export const buildCommand: yargs.CommandModule<
372372
}
373373
);
374374

375-
if (argv.ci) {
376-
await Promise.all(tasks);
377-
spinner.succeed(`\n${spinnerMessages.join("\n")}`);
378-
} else {
379-
const results = await Promise.allSettled(tasks);
375+
const results = await Promise.allSettled(tasks);
380376

381-
const failedSandboxes = sandboxes.filter(
382-
(_, index) => results[index].status === "rejected"
383-
);
377+
const failedSandboxes = sandboxes.filter(
378+
(_, index) => results[index].status === "rejected"
379+
);
384380

385-
if (failedSandboxes.length > 0) {
386-
spinner.info(`\n${spinnerMessages.join("\n")}`);
381+
if (failedSandboxes.length > 0) {
382+
spinner.info(`\n${spinnerMessages.join("\n")}`);
387383

388-
await waitForEnter(
389-
`\nThere was an issue preparing the sandboxes. Verify ${failedSandboxes
390-
.map((sandbox) => sandbox.sandboxId)
391-
.join(", ")} and press ENTER to create snapshot...\n`
392-
);
384+
await waitForEnter(
385+
`\nThere was an issue preparing the sandboxes. Verify ${failedSandboxes
386+
.map((sandbox) => sandbox.sandboxId)
387+
.join(", ")} and press ENTER to create snapshot...\n`
388+
);
393389

394-
failedSandboxes.forEach(({ sandboxId }) => {
395-
updateSpinnerMessage(
396-
sandboxes.findIndex((sandbox) => sandbox.sandboxId === sandboxId),
397-
"Creating snapshot...",
398-
sandboxId
399-
);
400-
});
390+
failedSandboxes.forEach(({ sandboxId }) => {
391+
updateSpinnerMessage(
392+
sandboxes.findIndex((sandbox) => sandbox.sandboxId === sandboxId),
393+
"Creating snapshot...",
394+
sandboxId
395+
);
396+
});
401397

402-
spinner.start(`\n${spinnerMessages.join("\n")}`);
398+
spinner.start(`\n${spinnerMessages.join("\n")}`);
403399

404-
await Promise.all(
405-
failedSandboxes.map(async ({ sandboxId, cluster }) => {
406-
const sdk = new CodeSandbox(API_KEY, {
407-
baseUrl: BASE_URL,
408-
headers: {
409-
"x-pitcher-manager-url": `https://${cluster}/api/v1`,
410-
},
411-
});
400+
await Promise.all(
401+
failedSandboxes.map(async ({ sandboxId, cluster }) => {
402+
const sdk = new CodeSandbox(API_KEY, {
403+
baseUrl: BASE_URL,
404+
headers: {
405+
"x-pitcher-manager-url": `https://${cluster}/api/v1`,
406+
},
407+
});
412408

413-
await sdk.sandboxes.hibernate(sandboxId);
409+
await sdk.sandboxes.hibernate(sandboxId);
414410

415-
spinner.start(
416-
updateSpinnerMessage(
417-
sandboxes.findIndex(
418-
(sandbox) => sandbox.sandboxId === sandboxId
419-
),
420-
"Snapshot created",
421-
sandboxId
422-
)
423-
);
424-
})
425-
);
426-
spinner.succeed(`\n${spinnerMessages.join("\n")}`);
427-
} else {
428-
spinner.succeed(`\n${spinnerMessages.join("\n")}`);
429-
}
411+
spinner.start(
412+
updateSpinnerMessage(
413+
sandboxes.findIndex(
414+
(sandbox) => sandbox.sandboxId === sandboxId
415+
),
416+
"Snapshot created",
417+
sandboxId
418+
)
419+
);
420+
})
421+
);
422+
spinner.succeed(`\n${spinnerMessages.join("\n")}`);
423+
} else {
424+
spinner.succeed(`\n${spinnerMessages.join("\n")}`);
430425
}
431426

432427
const data = handleResponse(

0 commit comments

Comments
 (0)