Skip to content

Commit e22a831

Browse files
committed
fix(cli): resend the stems request with its key until the service answers
The stems service answers once the stems are made, which can outlast the connection, and answers 409 request_in_progress to a resend while the first request runs. A failed job's reason is failure_message.
1 parent 08f3491 commit e22a831

2 files changed

Lines changed: 53 additions & 28 deletions

File tree

‎packages/cli/src/audio/read/read.test.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ const bed = (t: number) =>
4747
let server: Server;
4848
let stalledPath: string | null = null;
4949
let onStalled = () => {};
50-
let polls = 0;
5150
let stemCalls = 0;
5251
let stemRequest: unknown = null;
52+
const stemKeys: unknown[] = [];
5353
const stemFiles: Record<string, string> = {};
5454

5555
async function body(req: IncomingMessage) {
@@ -74,10 +74,9 @@ const routes = new Map<string, (reply: Reply, req: IncomingMessage, bytes: Buffe
7474
expect(req.headers["x-api-key"]).toBe("test-key");
7575
stemCalls++;
7676
stemRequest = JSON.parse(bytes.toString());
77-
reply(202, { data: { id: "job-1", status: "processing" } });
78-
},
79-
"GET /v3/audio/stems/job-1": (reply) => {
80-
polls++;
77+
stemKeys.push(req.headers["idempotency-key"]);
78+
if (stemCalls === 1)
79+
return reply(409, { error: { code: "request_in_progress", message: "Retry shortly." } });
8180
reply(200, {
8281
data: {
8382
id: "job-1",
@@ -177,7 +176,8 @@ describe.skipIf(!ffmpeg)("readAudio", () => {
177176
quality: "best",
178177
});
179178
expect(stemRequest).toEqual({ version: 1, audio: { type: "asset_id", asset_id: "episode" } });
180-
expect(polls).toBe(1);
179+
expect(stemKeys).toHaveLength(2);
180+
expect(stemKeys[1]).toBe(stemKeys[0]);
181181
expect(analysis).toMatchObject({
182182
separation: "stems",
183183
stems: ["bass", "drums", "piano", "vocals"],
@@ -193,7 +193,7 @@ describe.skipIf(!ffmpeg)("readAudio", () => {
193193
expect(readFileSync(textPath, "utf8")).toContain("effect boom (S1): low");
194194
expect(existsSync(join(cacheDir, "stems"))).toBe(true);
195195
await readAudio(path, "episode.wav", { cacheDir, quality: "best" });
196-
expect(stemCalls).toBe(1);
196+
expect(stemCalls).toBe(2);
197197
});
198198

199199
it.each(["/put", "/v3/audio/stems"])(

‎packages/cli/src/audio/read/stems.ts‎

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,23 @@ import {
2323
* `{ version: 1, audio: { type: "asset_id", asset_id } }`. The recording is uploaded
2424
* first as 192 kb/s stereo MP3 through the same origin's asset direct-upload routes.
2525
*
26-
* Response: `{ data: StemsJob }`, or a bare `StemsJob`. While `status` is `pending` or `processing`,
27-
* `GET <that url>/<id>` returns the job again. A `completed` job lists one URL per stem;
28-
* the names are vocals, drums, bass, guitar, piano and other.
26+
* Response: `{ data: StemsJob }` once the stems are made, which can take minutes. A failure is an HTTP error
27+
* whose body is `{ error: { code, message } }`. The same key and body resent answer the same job, and 409
28+
* `request_in_progress` while the first request still runs. A `completed` job lists one URL per stem; the
29+
* names are vocals, drums, bass, guitar, piano and other.
2930
*/
3031
interface StemsJob {
3132
id: string;
32-
status: "pending" | "processing" | "completed" | "failed";
33+
status: "pending" | "completed" | "failed";
3334
stems?: { name: string; url: string }[];
34-
error?: { message?: string };
35+
failure_message?: string;
3536
}
3637

3738
const URL_ENV = "HYPERFRAMES_AUDIO_STEMS_URL";
3839
const KEY_ENV = "HYPERFRAMES_AUDIO_API_KEY";
3940
const TIMEOUT_ENV = "HYPERFRAMES_AUDIO_STEMS_TIMEOUT_MS";
4041
const DEFAULT_TIMEOUT_MS = 30 * 60_000;
41-
const POLL_MS = 5000;
42+
const RETRY_MS = 5000;
4243
const DONE = "stems.json";
4344
const STEM_NAMES = ["vocals", "drums", "bass", "guitar", "piano", "other"] as const;
4445

@@ -62,7 +63,25 @@ export function cachedStems(dir: string): Record<string, string> | null {
6263
return Object.values(paths).every((p) => existsSync(p)) ? paths : null;
6364
}
6465

66+
class StillRunning extends Error {}
67+
68+
const DROPPED = new Set(["UND_ERR_HEADERS_TIMEOUT", "UND_ERR_SOCKET"]);
69+
70+
const dropped = (error: unknown) =>
71+
error instanceof TypeError &&
72+
DROPPED.has((error.cause as { code?: string } | undefined)?.code ?? "");
73+
74+
async function stillRunning(response: Response): Promise<boolean> {
75+
if (response.status !== 409) return false;
76+
const body = (await response
77+
.clone()
78+
.json()
79+
.catch(() => null)) as { error?: { code?: string } } | null;
80+
return body?.error?.code === "request_in_progress";
81+
}
82+
6583
async function job(response: Response): Promise<StemsJob> {
84+
if (await stillRunning(response)) throw new StillRunning();
6685
if (!response.ok) throw await serviceFailure(response, "the stems service");
6786
const body = (await response.json()) as { data?: StemsJob } | StemsJob | null;
6887
const value = (body && "data" in body ? body.data : body) as StemsJob | undefined;
@@ -82,6 +101,21 @@ async function encoded(path: string, dir: string, signal: AbortSignal): Promise<
82101
return out;
83102
}
84103

104+
async function separated(
105+
url: string,
106+
request: RequestInit,
107+
signal: AbortSignal,
108+
): Promise<StemsJob> {
109+
for (;;) {
110+
try {
111+
return await job(await fetch(url, { ...request, signal }));
112+
} catch (error) {
113+
if (!(error instanceof StillRunning || dropped(error))) throw error;
114+
await sleep(RETRY_MS, undefined, { signal });
115+
}
116+
}
117+
}
118+
85119
/** Separate the recording through the configured service and save each stem into `dir`. */
86120
export async function fetchStems(
87121
path: string,
@@ -106,32 +140,23 @@ export async function fetchStems(
106140
idempotencyKey: randomUUID(),
107141
signal,
108142
});
109-
let current = await job(
110-
await fetch(url, {
143+
const current = await separated(
144+
url,
145+
{
111146
method: "POST",
112-
signal,
113147
redirect: "error",
114148
headers: {
115149
...headers,
116150
"idempotency-key": randomUUID(),
117151
"content-type": "application/json",
118152
},
119153
body: JSON.stringify({ version: 1, audio: { type: "asset_id", asset_id: asset.asset_id } }),
120-
}),
154+
},
155+
signal,
121156
);
122-
while (current.status === "pending" || current.status === "processing") {
123-
await sleep(POLL_MS, undefined, { signal });
124-
current = await job(
125-
await fetch(`${url.replace(/\/$/, "")}/${encodeURIComponent(current.id)}`, {
126-
redirect: "error",
127-
headers,
128-
signal,
129-
}),
130-
);
131-
}
132157
if (current.status !== "completed")
133158
throw new Error(
134-
`stem separation failed${current.error?.message ? `: ${current.error.message}` : ""}`,
159+
`stem separation failed${current.failure_message ? `: ${current.failure_message}` : ""}`,
135160
);
136161
const stems = current.stems ?? [];
137162
if (!stems.some((s) => s.name === VOICE_STEM))

0 commit comments

Comments
 (0)