Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure LocalFile handles get closed #489

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions packages/apollo-collaboration-server/src/files/files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ export class FilesService {
return fileStream.pipe(gunzip)
}

private fileHandleCache: Record<string, GenericFilehandle | undefined> = {}

getFileHandle(file: FileDocument): GenericFilehandle {
const fileUploadFolder = this.configService.get('FILE_UPLOAD_FOLDER', {
infer: true,
Expand All @@ -96,13 +94,7 @@ export class FilesService {
switch (file.type) {
case 'text/x-fai':
case 'application/x-gzi': {
const fileHandleCacheHit = this.fileHandleCache[fileName]
if (fileHandleCacheHit) {
return fileHandleCacheHit
}
const fh = new LocalFileGzip(fileName)
this.fileHandleCache[fileName] = fh
return fh
return new LocalFileGzip(fileName)
}
case 'application/x-bgzip-fasta':
case 'text/x-gff3':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,30 @@ export class SequenceService {
}

if (assemblyDoc?.fileIds?.fai) {
const { fa, fai, gzi } = assemblyDoc.fileIds
this.logger.debug(
`Local fasta file = ${fa}, Local fasta index file = ${fai}, Local gzi index file = ${gzi}`,
)
const faDoc = await this.fileModel.findById(fa)
const { fa: faId, fai: faiId, gzi: gziId } = assemblyDoc.fileIds
const faDoc = await this.fileModel.findById(faId)
if (!faDoc) {
throw new Error(`No checksum for file document ${fa}`)
throw new Error(`No checksum for file document ${faId}`)
}

const faiDoc = await this.fileModel.findById(fai)
const faiDoc = await this.fileModel.findById(faiId)
if (!faiDoc) {
throw new Error(`File document not found for ${fai}`)
throw new Error(`File document not found for ${faiId}`)
}

const gziDoc = await this.fileModel.findById(gzi)
const gziDoc = await this.fileModel.findById(gziId)
if (!gziDoc) {
throw new Error(`File document not found for ${gzi}`)
throw new Error(`File document not found for ${gziId}`)
}

const sequenceAdapter = gzi
? new BgzipIndexedFasta({
fasta: this.filesService.getFileHandle(faDoc),
fai: this.filesService.getFileHandle(faiDoc),
gzi: this.filesService.getFileHandle(gziDoc),
})
: new IndexedFasta({
fasta: this.filesService.getFileHandle(faDoc),
fai: this.filesService.getFileHandle(faiDoc),
})
const fasta = this.filesService.getFileHandle(faDoc)
const fai = this.filesService.getFileHandle(faiDoc)
const gzi = gziId ? this.filesService.getFileHandle(gziDoc) : undefined
const sequenceAdapter = gziId
? new BgzipIndexedFasta({ fasta, fai, gzi })
: new IndexedFasta({ fasta, fai })
const sequence = await sequenceAdapter.getSequence(name, start, end)
await Promise.all([fasta.close(), fai.close(), gzi?.close()])
if (sequence === undefined) {
throw new Error('Sequence not found')
}
Expand Down
18 changes: 9 additions & 9 deletions packages/apollo-shared/src/Changes/AddAssemblyFromFileChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class AddAssemblyFromFileChange extends FromFileBaseChange {
throw new Error('No FILE_UPLOAD_FOLDER found in .env file')
}

const { fa, fai, gzi } = fileIds
const { fa: faId, fai: faiId, gzi: gziId } = fileIds
const {
assemblyModel,
checkModel,
Expand All @@ -97,30 +97,30 @@ export class AddAssemblyFromFileChange extends FromFileBaseChange {
user,
} = backend

const faDoc = await fileModel.findById(fa)
const faDoc = await fileModel.findById(faId)
const faChecksum = faDoc?.checksum
if (!faChecksum) {
throw new Error(`No checksum for file document ${faDoc}`)
}

const faiDoc = await fileModel.findById(fai)
const faiDoc = await fileModel.findById(faiId)
const faiChecksum = faiDoc?.checksum
if (!faiChecksum) {
throw new Error(`No checksum for file document ${faiDoc}`)
}

const gziDoc = await fileModel.findById(gzi)
const gziDoc = await fileModel.findById(gziId)
const gziChecksum = gziDoc?.checksum
if (!gziChecksum) {
throw new Error(`No checksum for file document ${gziDoc}`)
}

const sequenceAdapter = new BgzipIndexedFasta({
fasta: filesService.getFileHandle(faDoc),
fai: filesService.getFileHandle(faiDoc),
gzi: filesService.getFileHandle(gziDoc),
})
const fasta = filesService.getFileHandle(faDoc)
const fai = filesService.getFileHandle(faiDoc)
const gzi = filesService.getFileHandle(gziDoc)
const sequenceAdapter = new BgzipIndexedFasta({ fasta, fai, gzi })
const allSequenceSizes = await sequenceAdapter.getSequenceSizes()
await Promise.all([fasta.close(), fai.close(), gzi.close()])

const assemblyDoc = await assemblyModel
.findOne({ name: assemblyName })
Expand Down
Loading