Skip to content

Commit 290296c

Browse files
haochaoc
authored andcommitted
ci: harden TTS asset downloads for release
1 parent 02b1b65 commit 290296c

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

scripts/prepare_tts_assets.mjs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
import { createWriteStream, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
2+
import { createWriteStream, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
33
import { dirname, join } from 'node:path'
44
import { pipeline } from 'node:stream/promises'
55

@@ -18,18 +18,43 @@ const assets = [
1818
['voices/zm_009.bin', 'voices/zm_009.bin']
1919
]
2020

21+
function sleep(ms) {
22+
return new Promise((resolve) => setTimeout(resolve, ms))
23+
}
24+
2125
async function download(url, output) {
2226
mkdirSync(dirname(output), { recursive: true })
2327
if (existsSync(output)) {
2428
console.log(`[prepare-tts-assets] exists ${output}`)
2529
return
2630
}
27-
console.log(`[prepare-tts-assets] downloading ${url}`)
28-
const response = await fetch(url)
29-
if (!response.ok || !response.body) {
30-
throw new Error(`Failed to download ${url}: HTTP ${response.status}`)
31+
const partial = `${output}.part`
32+
let lastError
33+
for (let attempt = 1; attempt <= 5; attempt += 1) {
34+
try {
35+
rmSync(partial, { force: true })
36+
console.log(`[prepare-tts-assets] downloading ${url} (attempt ${attempt}/5)`)
37+
const response = await fetch(url, {
38+
headers: {
39+
'User-Agent': 'GoAgent-release-assets/1.0'
40+
}
41+
})
42+
if (!response.ok || !response.body) {
43+
throw new Error(`HTTP ${response.status}`)
44+
}
45+
await pipeline(response.body, createWriteStream(partial))
46+
renameSync(partial, output)
47+
return
48+
} catch (error) {
49+
lastError = error
50+
rmSync(partial, { force: true })
51+
if (attempt < 5) {
52+
console.warn(`[prepare-tts-assets] retrying ${url}: ${error instanceof Error ? error.message : String(error)}`)
53+
await sleep(1500 * attempt)
54+
}
55+
}
3156
}
32-
await pipeline(response.body, createWriteStream(output))
57+
throw new Error(`Failed to download ${url}: ${lastError instanceof Error ? lastError.message : String(lastError)}`)
3358
}
3459

3560
function normalizeTokenizerJson(path) {

0 commit comments

Comments
 (0)