Skip to content

Commit

Permalink
feat: 下载失败时给个log
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Dec 23, 2022
1 parent 01a5e90 commit 5325b3a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import colors from 'colors/safe'
import {HTTPError} from 'got'
import ms from 'ms'
import {join} from 'path'
import {Cluster} from './cluster'
Expand All @@ -17,7 +18,14 @@ export async function bootstrap(version: string): Promise<void> {

const files = await cluster.getFileList()
console.log(colors.green(`${files.files.length} files`))
await cluster.syncFiles(files)
try {
await cluster.syncFiles(files)
} catch (e) {
if (e instanceof HTTPError) {
console.error(colors.red(e.response.url))
}
throw e
}

await cluster.connect()
const proto = process.env.CLUSTER_BYOC !== 'true' ? 'https' : 'http'
Expand Down
14 changes: 7 additions & 7 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Cluster {
public nginxProcess: ChildProcess | undefined

private keepAliveError = 0
private readonly prefixUrl = process.env.CLUSTER_BMCLAPI || 'https://openbmclapi.bangbang93.com'
private readonly prefixUrl = process.env.CLUSTER_BMCLAPI ?? 'https://openbmclapi.bangbang93.com'
private readonly cacheDir = join(cwd(), 'cache')
private readonly host: string | undefined
private _port: number | string
Expand All @@ -56,10 +56,6 @@ export class Cluster {

private server?: Server

public get port(): number | string {
return this._port
}

public constructor(
private readonly clusterId: string,
private readonly clusterSecret: string,
Expand All @@ -82,6 +78,10 @@ export class Cluster {
})
}

public get port(): number | string {
return this._port
}

public async getFileList(): Promise<IFileList> {
const FileListSchema = Type.forSchema({
type: 'array',
Expand Down Expand Up @@ -117,14 +117,14 @@ export class Cluster {
})
const sortedFiles = files.sort((a, b) => a.path > b.path ? 1 : 0)
for (const file of sortedFiles) {
const path = join(this.cacheDir, file.hash.substr(0, 2), file.hash)
const path = join(this.cacheDir, file.hash.substring(0, 2), file.hash)
if (process.stderr.isTTY) {
bar.interrupt(`${colors.green('downloading')} ${colors.underline(file.path)}`)
} else {
console.log(`${colors.green('downloading')} ${colors.underline(file.path)}`)
}
let lastProgress = 0
const res = await this.got.get<Buffer>(file.path.substr(1))
const res = await this.got.get<Buffer>(file.path.substring(1))
.on('downloadProgress', (progress) => {
bar.tick(progress.transferred - lastProgress)
lastProgress = progress.transferred
Expand Down

0 comments on commit 5325b3a

Please sign in to comment.