Refactor download queue

This commit is contained in:
Geomitron
2020-05-10 12:18:25 -04:00
parent 6f8f69c087
commit 1cfb5f4e93
15 changed files with 678 additions and 490 deletions

View File

@@ -8,104 +8,150 @@ import { promisify } from 'util'
import { randomBytes as _randomBytes } from 'crypto'
import { mkdir as _mkdir } from 'fs'
import { ProgressType, NewDownload } from 'src/electron/shared/interfaces/download.interface'
import { downloadHandler } from './DownloadHandler'
import { googleTimer } from './GoogleTimer'
import { DriveFile } from 'src/electron/shared/interfaces/songDetails.interface'
import { FileTransfer } from './FileTransfer'
const randomBytes = promisify(_randomBytes)
const mkdir = promisify(_mkdir)
type EventCallback = {
/** Note: this will not be the last event if `retry()` is called. */
'error': () => void
'complete': () => void
}
type Callbacks = { [E in keyof EventCallback]: EventCallback[E] }
export type DownloadError = { header: string; body: string }
export class ChartDownload {
// This changes if the user needs to click 'retry' or 'continue'
run: () => void | Promise<void> = this.beginDownload.bind(this)
cancel: () => void
isArchive: boolean
title: string
header: string
description: string
percent = 0
type: ProgressType
private retryFn: () => void | Promise<void>
private cancelFn: () => void
private callbacks = {} as Callbacks
private files: DriveFile[]
allFilesProgress = 0
individualFileProgressPortion: number
private percent = 0 // Needs to be stored here because errors won't know the exact percent
private readonly individualFileProgressPortion: number
private readonly destinationFolderName: string
private _allFilesProgress = 0
get allFilesProgress() { return this._allFilesProgress }
private _hasFailed = false
/** If this chart download needs to be retried */
get hasFailed() { return this._hasFailed }
get isArchive() { return this.data.driveData.isArchive }
constructor(public versionID: number, private data: NewDownload) {
this.updateGUI('', 'Waiting for other downloads to finish...', 'good')
this.files = data.driveData.files
this.isArchive = data.driveData.isArchive
this.individualFileProgressPortion = 80 / this.files.length
this.destinationFolderName = sanitizeFilename(`${this.data.artist} - ${this.data.avTagName} (${this.data.charter})`)
}
/**
* Changes this download to reflect that it is waiting in the download queue.
* Calls `callback` when `event` fires. (no events will be fired after `this.cancel()` is called)
*/
setInQueue() {
this.title = `${this.data.avTagName} - ${this.data.artist}`
this.header = ''
this.description = 'Waiting for other downloads to finish...'
this.type = 'good'
this.cancel = () => { /* do nothing */ }
emitIPCEvent('download-updated', this)
on<E extends keyof EventCallback>(event: E, callback: EventCallback[E]) {
this.callbacks[event] = callback
}
/**
* Retries the last failed step if it is running.
*/
retry() { // Only allow it to be called once
if (this.retryFn != undefined) {
this._hasFailed = false
const retryFn = this.retryFn
this.retryFn = undefined
retryFn()
}
}
/**
* Cancels the download if it is running.
*/
cancel() { // Only allow it to be called once
if (this.cancelFn != undefined) {
const cancelFn = this.cancelFn
this.cancelFn = undefined
cancelFn()
}
}
/**
* Updates the GUI with new information about this chart download.
*/
private updateGUI(header: string, description: string, type: ProgressType) {
emitIPCEvent('download-updated', {
versionID: this.versionID,
title: `${this.data.avTagName} - ${this.data.artist}`,
header: header,
description: description,
percent: this.percent,
type: type
})
}
/**
* Save the retry function, update the GUI, and call the `error` callback.
*/
private handleError(err: DownloadError, retry: () => void) {
this._hasFailed = true
this.retryFn = retry
this.updateGUI(err.header, err.body, 'error')
this.callbacks.error()
}
/**
* Starts the download process.
*/
async beginDownload() {
// Create a temporary folder to store the downloaded files
// CREATE DOWNLOAD DIRECTORY
let chartPath: string
try {
chartPath = await this.createDownloadFolder()
} catch (e) {
this.run = this.beginDownload.bind(this) // Retry action
this.error('Access Error', e.message)
} catch (err) {
this.retryFn = () => this.beginDownload()
this.updateGUI('Access Error', err.message, 'error')
return
}
// For each download link in <this.files>, download the file to <chartPath>
// DOWNLOAD FILES
for (let i = 0; i < this.files.length; i++) {
// INITIALIZE DOWNLOADER
const downloader = new FileDownloader(this.files[i].webContentLink, chartPath)
this.cancelFn = () => downloader.cancelDownload()
const downloadComplete = this.addDownloadEventListeners(downloader, i)
// DOWNLOAD THE NEXT FILE
// Wait for google rate limit
this.header = `[${this.files[i].name}] (file ${i + 1}/${this.files.length})`
googleTimer.onTimerUpdate((remainingTime, totalTime) => {
this.description = `Waiting for Google rate limit... (${remainingTime}s)`
this.percent = this.allFilesProgress + interpolate(remainingTime, totalTime, 0, 0, this.individualFileProgressPortion / 2)
this.type = 'good'
emitIPCEvent('download-updated', this)
})
this.cancel = () => {
googleTimer.removeCallbacks()
this.onDownloadStop()
downloader.cancelDownload()
}
await new Promise<void>(resolve => googleTimer.onTimerReady(resolve))
this.cancel = () => {
this.onDownloadStop()
downloader.cancelDownload()
}
downloader.beginDownload()
await downloadComplete // Wait for this download to finish
await downloadComplete
}
// INITIALIZE FILE EXTRACTOR
const destinationFolderName = sanitizeFilename(`${this.data.artist} - ${this.data.avTagName} (${this.data.charter})`)
const extractor = new FileExtractor(chartPath, this.isArchive, destinationFolderName)
this.cancel = () => extractor.cancelExtract() // Make cancel button cancel the file extraction
this.addExtractorEventListeners(extractor)
// EXTRACT FILES
if (this.isArchive) {
const extractor = new FileExtractor(chartPath)
this.cancelFn = () => extractor.cancelExtract()
// EXTRACT THE DOWNLOADED ARCHIVE
extractor.beginExtract()
const extractComplete = this.addExtractorEventListeners(extractor)
extractor.beginExtract()
await extractComplete
}
// TRANSFER FILES
const transfer = new FileTransfer(chartPath, this.destinationFolderName)
this.cancelFn = () => transfer.cancelTransfer()
const transferComplete = this.addTransferEventListeners(transfer)
transfer.beginTransfer()
await transferComplete
this.callbacks.complete()
}
/**
* Attempts to create a unique folder in Bridge's data paths. Throws an error if this fails.
* Attempts to create a unique folder in Bridge's data paths.
* @returns the new folder's path.
* @throws an error if this fails.
*/
private async createDownloadFolder() {
let retryCount = 0
@@ -125,71 +171,38 @@ export class ChartDownload {
throw new Error(`Bridge was unable to create a directory at [${chartPath}]`)
}
/**
* Stops the download and displays an error message.
*/
private error(header: string, description: string) {
this.header = header
this.description = description
this.type = 'error'
this.onDownloadStop()
emitIPCEvent('download-updated', this)
}
/**
* If this was a google download, allows a new google download to start.
*/
private onDownloadStop() {
downloadHandler.isGoogleDownloading = false
downloadHandler.updateQueue()
}
/**
* Defines what happens in response to `FileDownloader` events.
* @returns a `Promise` that resolves when the download finishes.
*/
private addDownloadEventListeners(downloader: FileDownloader, fileIndex: number) {
let downloadHeader = `[${this.files[fileIndex].name}] (file ${fileIndex + 1}/${this.files.length})`
let fileProgress = 0
downloader.on('request', () => {
this.description = 'Sending request...'
fileProgress = this.individualFileProgressPortion / 2
this.percent = this.allFilesProgress + fileProgress
this.type = 'good'
emitIPCEvent('download-updated', this)
downloader.on('waitProgress', (remainingSeconds: number, totalSeconds: number) => {
this.percent = this._allFilesProgress + interpolate(remainingSeconds, totalSeconds, 0, 0, this.individualFileProgressPortion / 2)
this.updateGUI(downloadHeader, `Waiting for Google rate limit... (${remainingSeconds}s)`, 'good')
})
downloader.on('warning', (continueAnyway) => {
this.description = 'WARNING'
this.run = continueAnyway
this.type = 'warning'
this.onDownloadStop()
emitIPCEvent('download-updated', this)
downloader.on('requestSent', () => {
fileProgress = this.individualFileProgressPortion / 2
this.percent = this._allFilesProgress + fileProgress
this.updateGUI(downloadHeader, 'Sending request...', 'good')
})
downloader.on('downloadProgress', (bytesDownloaded) => {
downloadHeader = `[${this.files[fileIndex].name}] (file ${fileIndex + 1}/${this.files.length})`
const size = Number(this.files[fileIndex].size)
this.header = `[${this.files[fileIndex].name}] (file ${fileIndex + 1}/${this.files.length})`
this.description = `Downloading... (${Math.round(1000 * bytesDownloaded / size) / 10}%)`
fileProgress = interpolate(bytesDownloaded, 0, size, this.individualFileProgressPortion / 2, this.individualFileProgressPortion)
this.percent = this.allFilesProgress + fileProgress
this.type = 'fastUpdate'
emitIPCEvent('download-updated', this)
this.percent = this._allFilesProgress + fileProgress
this.updateGUI(downloadHeader, `Downloading... (${Math.round(1000 * bytesDownloaded / size) / 10}%)`, 'fastUpdate')
})
downloader.on('error', (error, retry) => {
this.header = error.header
this.description = error.body
this.type = 'error'
this.run = () => { retry() }
this.onDownloadStop()
emitIPCEvent('download-updated', this)
})
downloader.on('error', this.handleError.bind(this))
// Returns a promise that resolves when the download is finished
return new Promise<void>(resolve => {
downloader.on('complete', () => {
this.allFilesProgress += this.individualFileProgressPortion
emitIPCEvent('download-updated', this)
this._allFilesProgress += this.individualFileProgressPortion
resolve()
})
})
@@ -197,49 +210,51 @@ export class ChartDownload {
/**
* Defines what happens in response to `FileExtractor` events.
* @returns a `Promise` that resolves when the extraction finishes.
*/
private addExtractorEventListeners(extractor: FileExtractor) {
let archive = ''
extractor.on('extract', (filename) => {
extractor.on('start', (filename) => {
archive = filename
this.header = `[${archive}]`
this.description = 'Extracting...'
this.type = 'good'
emitIPCEvent('download-updated', this)
this.updateGUI(`[${archive}]`, 'Extracting...', 'good')
})
extractor.on('extractProgress', (percent, filecount) => {
this.header = `[${archive}] (${filecount} file${filecount == 1 ? '' : 's'} extracted)`
this.description = `Extracting... (${percent}%)`
this.percent = interpolate(percent, 0, 100, 80, 95)
this.type = 'fastUpdate'
emitIPCEvent('download-updated', this)
this.updateGUI(`[${archive}] (${filecount} file${filecount == 1 ? '' : 's'} extracted)`, `Extracting... (${percent}%)`, 'fastUpdate')
})
extractor.on('transfer', (filepath) => {
this.header = 'Moving files to library folder...'
this.description = filepath
this.percent = 95
this.type = 'good'
emitIPCEvent('download-updated', this)
extractor.on('error', this.handleError.bind(this))
return new Promise<void>(resolve => {
extractor.on('complete', () => {
this.percent = 95
resolve()
})
})
}
/**
* Defines what happens in response to `FileTransfer` events.
* @returns a `Promise` that resolves when the transfer finishes.
*/
private addTransferEventListeners(transfer: FileTransfer) {
let destinationFolder: string
transfer.on('start', (_destinationFolder) => {
destinationFolder = _destinationFolder
this.updateGUI('Moving files to library folder...', destinationFolder, 'good')
})
extractor.on('error', (error, retry) => {
this.header = error.header
this.description = error.body
this.type = 'error'
this.run = retry
emitIPCEvent('download-updated', this)
})
transfer.on('error', this.handleError.bind(this))
extractor.on('complete', (filepath) => {
this.header = 'Download complete.'
this.description = filepath
this.percent = 100
this.type = 'done'
this.onDownloadStop()
emitIPCEvent('download-updated', this)
return new Promise<void>(resolve => {
transfer.on('complete', () => {
this.percent = 100
this.updateGUI('Download complete.', destinationFolder, 'done')
resolve()
})
})
}
}