Skip to content

Commit

Permalink
feat: 文件列表支持增量更新
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Mar 26, 2024
1 parent 9097f2a commit 71b20a3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
20 changes: 15 additions & 5 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import nodeCluster from 'cluster'
import colors from 'colors/safe.js'
import {HTTPError} from 'got'
import {max} from 'lodash-es'
import ms from 'ms'
import {join} from 'path'
import {fileURLToPath} from 'url'
import {Cluster} from './cluster.js'
import {config} from './config.js'
import {logger} from './logger.js'
import {TokenManager} from './token.js'
import {IFileList} from './types.js'

// eslint-disable-next-line @typescript-eslint/naming-convention
const __dirname = fileURLToPath(new URL('.', import.meta.url))
Expand Down Expand Up @@ -56,7 +58,9 @@ export async function bootstrap(version: string): Promise<void> {
process.send('ready')
}

checkFileInterval = setTimeout(() => checkFile, ms('10m'))
checkFileInterval = setTimeout(() => {
void checkFile(files)
}, ms('10s'))
} catch (e) {
logger.fatal(e)
if (process.env.NODE_ENV === 'development') {
Expand All @@ -65,19 +69,25 @@ export async function bootstrap(version: string): Promise<void> {
cluster.exit(1)
}
}
async function checkFile(): Promise<void> {
async function checkFile(lastFileList: IFileList): Promise<void> {
logger.debug('refresh files')
try {
const files = await cluster.getFileList()
const lastModified = max(lastFileList.files.map((file) => file.mtime))
const fileList = await cluster.getFileList(lastModified)
if (fileList.files.length === 0) {
logger.debug('没有新文件')
return
}
lastFileList = fileList
const configuration = await cluster.getConfiguration()
await cluster.syncFiles(files, configuration.sync)
} finally {
checkFileInterval = setTimeout(() => {
checkFile().catch((e) => {
checkFile(lastFileList).catch((e) => {
console.error('check file error')
console.error(e)
})
}, ms('10m'))
}, ms('10s'))
}
}

Expand Down
26 changes: 12 additions & 14 deletions src/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {decompress} from '@mongodb-js/zstd'
import avsc, {type schema} from 'avsc'
import Bluebird from 'bluebird'
import {ChildProcess, spawn} from 'child_process'
import {MultiBar} from 'cli-progress'
Expand All @@ -26,6 +25,7 @@ import {connect, Socket} from 'socket.io-client'
import {Tail} from 'tail'
import {fileURLToPath} from 'url'
import {config, type OpenbmclapiAgentConfiguration, OpenbmclapiAgentConfigurationSchema} from './config.js'
import {FileListSchema} from './constants.js'
import {validateFile} from './file.js'
import {logger} from './logger.js'
import MeasureRouteFactory from './measure.route.js'
Expand Down Expand Up @@ -123,25 +123,22 @@ export class Cluster {
}
}

public async getFileList(): Promise<IFileList> {
const fileListSchema = avsc.Type.forSchema({
type: 'array',
items: {
type: 'record',
fields: [
{name: 'path', type: 'string'},
{name: 'hash', type: 'string'},
{name: 'size', type: 'long'},
],
} as schema.RecordType,
})
public async getFileList(lastModified?: number): Promise<IFileList> {
const res = await this.got.get('openbmclapi/files', {
responseType: 'buffer',
cache: this.requestCache,
searchParams: {
lastModified,
},
})
if (res.statusCode === 304) {
return {
files: [],
}
}
const decompressed = await decompress(res.body)
return {
files: fileListSchema.fromBuffer(Buffer.from(decompressed)) as IFileList['files'],
files: FileListSchema.fromBuffer(Buffer.from(decompressed)) as IFileList['files'],
}
}

Expand Down Expand Up @@ -433,6 +430,7 @@ export class Cluster {
path: `/download/${hash}`,
hash,
size: res.body.length,
mtime: Date.now(),
})
}

Expand Down
15 changes: 15 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import avsc from 'avsc'

export const FileListSchema = avsc.Type.forSchema({
type: 'array',
items: {
name: 'FileListEntry',
type: 'record',
fields: [
{name: 'path', type: 'string'},
{name: 'hash', type: 'string'},
{name: 'size', type: 'long'},
{name: 'mtime', type: 'long'},
],
},
})
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface IFileInfo {
path: string
hash: string
size: number
mtime: number
}

0 comments on commit 71b20a3

Please sign in to comment.