|
1 | | -import type { Id, IPitcherClient } from "@codesandbox/pitcher-client"; |
2 | | -import { listenOnce } from "@codesandbox/pitcher-common/dist/event"; |
| 1 | +import { |
| 2 | + Barrier, |
| 3 | + type IPitcherClient, |
| 4 | + type protocol, |
| 5 | +} from "@codesandbox/pitcher-client"; |
3 | 6 |
|
4 | 7 | import { Disposable } from "../../utils/disposable"; |
5 | 8 | import { Emitter } from "../../utils/event"; |
| 9 | +import { DEFAULT_SHELL_SIZE } from "./terminals"; |
6 | 10 |
|
7 | 11 | export class Setup { |
8 | 12 | private disposable = new Disposable(); |
9 | | - private readonly onSetupProgressUpdateEmitter = this.disposable.addDisposable( |
10 | | - new Emitter<SetupProgress>() |
| 13 | + private steps: Promise<Step[]>; |
| 14 | + private setupProgress: protocol.setup.SetupProgress; |
| 15 | + private readonly onSetupProgressChangeEmitter = this.disposable.addDisposable( |
| 16 | + new Emitter<void>() |
11 | 17 | ); |
12 | | - /** |
13 | | - * Emitted when the setup progress is updated. |
14 | | - */ |
15 | | - public readonly onSetupProgressUpdate = |
16 | | - this.onSetupProgressUpdateEmitter.event; |
17 | | - |
| 18 | + public readonly onSetupProgressChange = |
| 19 | + this.onSetupProgressChangeEmitter.event; |
| 20 | + get status() { |
| 21 | + return this.setupProgress.state; |
| 22 | + } |
| 23 | + get currentStepIndex() { |
| 24 | + return this.setupProgress.currentStepIndex; |
| 25 | + } |
18 | 26 | constructor( |
19 | 27 | sessionDisposable: Disposable, |
20 | 28 | private pitcherClient: IPitcherClient |
21 | 29 | ) { |
22 | 30 | sessionDisposable.onWillDispose(() => { |
23 | 31 | this.disposable.dispose(); |
24 | 32 | }); |
| 33 | + |
| 34 | + // We have a race condition where we might not have the steps yet and need |
| 35 | + // an event to tell us when they have started. But we might also have all the steps, |
| 36 | + // where no new event will arrive. So we use a barrier to manage this |
| 37 | + const initialStepsBarrier = new Barrier<Step[]>(); |
| 38 | + |
| 39 | + this.setupProgress = this.pitcherClient.clients.setup.getProgress(); |
| 40 | + this.steps = initialStepsBarrier |
| 41 | + .wait() |
| 42 | + .then((result) => (result.status === "resolved" ? result.value : [])); |
| 43 | + |
| 44 | + let hasInitializedSteps = Boolean(this.setupProgress.steps.length); |
| 45 | + |
| 46 | + if (hasInitializedSteps) { |
| 47 | + initialStepsBarrier.open( |
| 48 | + this.setupProgress.steps.map( |
| 49 | + (step, index) => new Step(index, step, pitcherClient) |
| 50 | + ) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
25 | 54 | this.disposable.addDisposable( |
26 | 55 | pitcherClient.clients.setup.onSetupProgressUpdate((progress) => { |
27 | | - this.onSetupProgressUpdateEmitter.fire(progress); |
| 56 | + if (!hasInitializedSteps) { |
| 57 | + hasInitializedSteps = true; |
| 58 | + initialStepsBarrier.open( |
| 59 | + progress.steps.map( |
| 60 | + (step, index) => new Step(index, step, pitcherClient) |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + this.setupProgress = progress; |
| 66 | + this.onSetupProgressChangeEmitter.fire(); |
28 | 67 | }) |
29 | 68 | ); |
30 | 69 | } |
31 | 70 |
|
32 | | - /** |
33 | | - * Run the setup tasks, this will prepare the docker image, and run the user defined |
34 | | - * setup steps. This will automatically run when a sandbox is started. |
35 | | - */ |
36 | | - async run(): Promise<SetupProgress> { |
37 | | - return this.pitcherClient.clients.setup.init(); |
| 71 | + getSteps() { |
| 72 | + return this.steps; |
38 | 73 | } |
39 | 74 |
|
40 | | - /** |
41 | | - * Returns the current progress of the setup tasks. |
42 | | - */ |
43 | | - async getProgress(): Promise<SetupProgress> { |
44 | | - await this.pitcherClient.clients.setup.readyPromise; |
45 | | - return this.pitcherClient.clients.setup.getProgress(); |
| 75 | + async run(): Promise<void> { |
| 76 | + await this.pitcherClient.clients.setup.init(); |
46 | 77 | } |
47 | 78 |
|
48 | | - async waitForFinish(): Promise<SetupProgress> { |
49 | | - const progress = await this.getProgress(); |
50 | | - if (progress.state === "FINISHED") { |
51 | | - return Promise.resolve(progress); |
| 79 | + async waitForFinish(): Promise<void> { |
| 80 | + if (this.setupProgress.state === "STOPPED") { |
| 81 | + throw new Error("Setup Failed"); |
52 | 82 | } |
53 | 83 |
|
54 | | - return listenOnce(this.onSetupProgressUpdate, (progress) => { |
55 | | - return progress.state === "FINISHED"; |
| 84 | + if (this.setupProgress.state === "FINISHED") { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + return new Promise<void>((resolve, reject) => { |
| 89 | + const disposer = this.onSetupProgressChange(() => { |
| 90 | + if (this.setupProgress.state === "FINISHED") { |
| 91 | + disposer.dispose(); |
| 92 | + resolve(); |
| 93 | + } else if (this.setupProgress.state === "STOPPED") { |
| 94 | + disposer.dispose(); |
| 95 | + reject(new Error("Setup Failed")); |
| 96 | + } |
| 97 | + }); |
56 | 98 | }); |
57 | 99 | } |
58 | 100 | } |
59 | 101 |
|
60 | | -export type SetupProgress = { |
61 | | - state: "IDLE" | "IN_PROGRESS" | "FINISHED" | "STOPPED"; |
62 | | - steps: Step[]; |
63 | | - currentStepIndex: number; |
64 | | -}; |
| 102 | +export class Step { |
| 103 | + private disposable = new Disposable(); |
| 104 | + // TODO: differentiate between stdout and stderr, also send back bytes instead of |
| 105 | + // strings |
| 106 | + private onOutputEmitter = this.disposable.addDisposable( |
| 107 | + new Emitter<string>() |
| 108 | + ); |
| 109 | + public readonly onOutput = this.onOutputEmitter.event; |
| 110 | + private onStatusChangeEmitter = this.disposable.addDisposable( |
| 111 | + new Emitter<string>() |
| 112 | + ); |
| 113 | + public readonly onStatusChange = this.onStatusChangeEmitter.event; |
| 114 | + private output: string[] = []; |
| 115 | + |
| 116 | + get name(): string { |
| 117 | + return this.step.name; |
| 118 | + } |
| 119 | + |
| 120 | + get command() { |
| 121 | + return this.step.command; |
| 122 | + } |
65 | 123 |
|
66 | | -export type SetupShellStatus = "SUCCEEDED" | "FAILED" | "SKIPPED"; |
| 124 | + get status() { |
| 125 | + return this.step.finishStatus || "IDLE"; |
| 126 | + } |
| 127 | + |
| 128 | + constructor( |
| 129 | + stepIndex: number, |
| 130 | + private step: protocol.setup.Step, |
| 131 | + private pitcherClient: IPitcherClient |
| 132 | + ) { |
| 133 | + this.disposable.addDisposable( |
| 134 | + this.pitcherClient.clients.setup.onSetupProgressUpdate((progress) => { |
| 135 | + const oldStep = this.step; |
| 136 | + const newStep = progress.steps[stepIndex]; |
67 | 137 |
|
68 | | -export type Step = { |
69 | | - name: string; |
70 | | - command: string; |
71 | | - shellId: Id | null; |
72 | | - finishStatus: SetupShellStatus | null; |
73 | | -}; |
| 138 | + this.step = newStep; |
| 139 | + |
| 140 | + if (newStep.finishStatus !== oldStep.finishStatus) { |
| 141 | + this.onStatusChangeEmitter.fire(newStep.finishStatus || "IDLE"); |
| 142 | + } |
| 143 | + }) |
| 144 | + ); |
| 145 | + this.disposable.addDisposable( |
| 146 | + this.pitcherClient.clients.shell.onShellOut(({ shellId, out }) => { |
| 147 | + if (shellId === this.step.shellId) { |
| 148 | + this.onOutputEmitter.fire(out); |
| 149 | + |
| 150 | + this.output.push(out); |
| 151 | + if (this.output.length > 1000) { |
| 152 | + this.output.shift(); |
| 153 | + } |
| 154 | + } |
| 155 | + }) |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + async open(dimensions = DEFAULT_SHELL_SIZE): Promise<string> { |
| 160 | + const open = async (shellId: protocol.shell.ShellId) => { |
| 161 | + const shell = await this.pitcherClient.clients.shell.open( |
| 162 | + shellId, |
| 163 | + dimensions |
| 164 | + ); |
| 165 | + |
| 166 | + this.output = shell.buffer; |
| 167 | + |
| 168 | + return this.output.join("\n"); |
| 169 | + }; |
| 170 | + |
| 171 | + if (this.step.shellId) { |
| 172 | + return open(this.step.shellId); |
| 173 | + } |
| 174 | + |
| 175 | + return new Promise<string>((resolve) => { |
| 176 | + const disposable = this.onStatusChange(() => { |
| 177 | + if (this.step.shellId) { |
| 178 | + disposable.dispose(); |
| 179 | + resolve(open(this.step.shellId)); |
| 180 | + } |
| 181 | + }); |
| 182 | + }); |
| 183 | + } |
| 184 | + |
| 185 | + async waitForFinish() { |
| 186 | + if (this.step.finishStatus === "FAILED") { |
| 187 | + throw new Error("Step Failed"); |
| 188 | + } |
| 189 | + |
| 190 | + if ( |
| 191 | + this.step.finishStatus === "SUCCEEDED" || |
| 192 | + this.step.finishStatus === "SKIPPED" |
| 193 | + ) { |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + return new Promise<void>((resolve, reject) => { |
| 198 | + const disposable = this.onStatusChange((status) => { |
| 199 | + if (status === "SUCCEEDED" || status === "SKIPPED") { |
| 200 | + disposable.dispose(); |
| 201 | + resolve(); |
| 202 | + } else if (status === "FAILED") { |
| 203 | + disposable.dispose(); |
| 204 | + reject(new Error("Step Failed")); |
| 205 | + } |
| 206 | + }); |
| 207 | + }); |
| 208 | + } |
| 209 | +} |
0 commit comments