openai/codex-action
Publicmirrored from https://github.com/openai/codex-actionAvailable
src/readServerInfo.ts
30lines · modeblame
e9d0a695Michael Bolin10 months ago | 1 | import * as core from "@actions/core"; |
| 2 | import * as fs from "fs/promises"; | |
| 3 | | |
| 4 | /** | |
| 5 | * In theory, this is not called until `serverInfoFile` is non-empty, but we | |
| 6 | * will poll in the rare case that it was a partial write. | |
| 7 | */ | |
| 8 | export async function readServerInfo(serverInfoFile: string): Promise<void> { | |
| 9 | for (let attempt = 0; attempt < 100; attempt++) { | |
| 10 | try { | |
| 11 | const contents = await fs.readFile(serverInfoFile, { encoding: "utf8" }); | |
| 12 | const { port } = JSON.parse(contents); | |
| 13 | if (typeof port !== "number") { | |
| 14 | continue; | |
| 15 | } | |
| 16 | | |
| 17 | core.setOutput("port", port.toString()); | |
| 18 | return; | |
| 19 | } catch (error) { | |
| 20 | console.error(`Error reading server info: ${error}`); | |
| 21 | await sleep(100); | |
| 22 | } | |
| 23 | } | |
| 24 | | |
| 25 | throw Error(`Failed to read server info from ${serverInfoFile}`); | |
| 26 | } | |
| 27 | | |
| 28 | async function sleep(ms: number): Promise<void> { | |
| 29 | return new Promise((resolve) => setTimeout(resolve, ms)); | |
| 30 | } |