| |
|
|
| import { |
| doneInQueueStore, |
| makeUniqJobId, |
| startInQueueStore, |
| updateProgressInQueueStore, |
| } from "./QueueStore"; |
| import { IJobInfo, IQueueCallbacks } from "./QueueTypes"; |
| import { messagesApi } from "@/Api/Messages"; |
|
|
| |
| |
| |
| |
| |
| export abstract class QueueParent { |
| private _maxTotalProcs: number; |
| private _procsPerJobBatch: number; |
|
|
| |
| |
| |
| private _inputBatches: IJobInfo[][] = []; |
|
|
| |
| private _jobsCurrentlyRunning: { [index: number]: IJobInfo } = {}; |
|
|
| |
| private _outputs: IJobInfo[] = []; |
|
|
| |
| |
| private _queueTimer: any; |
|
|
| |
| private _numProcsCurrentlyRunning = 0; |
|
|
| public _callbacks: IQueueCallbacks | undefined; |
|
|
| |
| protected _numTotalJobs: number; |
|
|
| |
| private _id: string; |
|
|
| private _showInQueue: boolean; |
|
|
| |
| protected jobsCancelling: boolean; |
|
|
| |
| |
| public done: Promise<any>; |
| public _doneResolveFunc: any; |
|
|
| private _onJobAfterQueueDone: boolean; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| constructor( |
| jobTypeId: string, |
| inputs: any[], |
| maxProcs: number, |
| callbacks?: IQueueCallbacks, |
| procsPerJobBatch = 1, |
| simulBatches?: number, |
| batchSize: number | undefined = undefined, |
| showInQueue = true, |
| onJobAfterQueueDone = true |
| ) { |
| this._numTotalJobs = inputs.length; |
| this._procsPerJobBatch = procsPerJobBatch; |
| this._callbacks = callbacks; |
| this.jobsCancelling = false; |
| this._showInQueue = showInQueue; |
| this._onJobAfterQueueDone = onJobAfterQueueDone; |
|
|
| |
| |
| this._maxTotalProcs = maxProcs; |
| this._maxTotalProcs = Math.min( |
| this._maxTotalProcs, |
| this._numTotalJobs * this._procsPerJobBatch |
| ); |
|
|
| if (simulBatches === undefined) { |
| simulBatches = Math.floor(this._maxTotalProcs / this._procsPerJobBatch); |
| } |
| this._maxTotalProcs = Math.min( |
| this._maxTotalProcs, |
| simulBatches * this._procsPerJobBatch |
| ); |
|
|
| |
|
|
| if (batchSize === undefined) { |
| |
| |
| |
| |
| |
| batchSize = Math.ceil(inputs.length / simulBatches); |
| } |
|
|
| this._id = makeUniqJobId(jobTypeId); |
|
|
| this.done = new Promise((resolve) => { |
| this._doneResolveFunc = resolve; |
| }); |
|
|
| if (inputs.length === 0) { |
| |
| this._onQueueDone([]); |
| return; |
| } |
|
|
| this._onQueueStart(); |
|
|
| |
| const inputInfos = inputs.map((input, index) => { |
| return { |
| index, |
| input: input, |
| } as IJobInfo; |
| }); |
|
|
| |
| |
| for (let i = 0; i < inputInfos.length; i += batchSize) { |
| this._inputBatches.push(inputInfos.slice(i, i + batchSize)); |
| } |
|
|
| |
|
|
| this._queueTimer = setInterval(() => { |
| |
| if (this._inputBatches.length === 0) { |
| clearInterval(this._queueTimer); |
| return; |
| } |
|
|
| if (this.jobsCancelling) { |
| |
| |
| return; |
| } |
|
|
| this._fillQueueToCapacity(); |
| }, 250); |
| } |
|
|
| |
| |
| |
| |
| private _reportQueueStatusForDebug() { |
| let lastNumJobsFinished = 0; |
|
|
| setInterval(() => { |
| |
| |
| let numJobsNotStarted = 0; |
| for (const batch of this._inputBatches) { |
| numJobsNotStarted += batch.length; |
| } |
| const jobsRunning = Object.keys(this._jobsCurrentlyRunning).length; |
| const numJobsFinished = this._outputs.length; |
|
|
| if (numJobsFinished === lastNumJobsFinished) { |
| |
| return; |
| } |
| lastNumJobsFinished = numJobsFinished; |
|
|
| |
| |
|
|
| console.log("Jobs not yet started:", numJobsNotStarted); |
| console.log("Jobs running:", jobsRunning); |
| console.log("Jobs finished:", numJobsFinished); |
| }, 100); |
| } |
|
|
| |
| |
| |
| |
| private _fillQueueToCapacity() { |
| |
| |
| while (true) { |
| |
| if (this.jobsCancelling) { |
| return; |
| } |
|
|
| |
| if (this._inputBatches.length === 0) { |
| break; |
| } |
|
|
| if ( |
| this._numProcsCurrentlyRunning + this._procsPerJobBatch > |
| this._maxTotalProcs |
| ) { |
| |
| break; |
| } |
|
|
| const inputBatch = this._inputBatches.shift(); |
| |
| if (!inputBatch) { |
| break; |
| } |
|
|
| |
| for (const jobInfo of inputBatch) { |
| this._jobsCurrentlyRunning[jobInfo.index] = jobInfo; |
| } |
|
|
| this._numProcsCurrentlyRunning += this._procsPerJobBatch; |
| this.runJobBatch(inputBatch, this._procsPerJobBatch) |
| .then((outBatch: any[]) => { |
| this._outputs.push(...outBatch); |
| this._numProcsCurrentlyRunning -= this._procsPerJobBatch; |
|
|
| |
| for (const jobInfo of inputBatch) { |
| delete this._jobsCurrentlyRunning[jobInfo.index]; |
| } |
|
|
| |
| if (this._onJobAfterQueueDone) { |
| for (const jobInfo of inputBatch) { |
| this._onJobDone(jobInfo); |
| } |
| } |
|
|
| |
| this._onProgress(this._outputs.length / this._numTotalJobs); |
|
|
| |
| if ( |
| Object.keys(this._jobsCurrentlyRunning).length === 0 && |
| this._inputBatches.length === 0 |
| ) { |
| |
| this._onQueueDone(this._outputs); |
| } |
|
|
| return; |
| }) |
| .catch((err) => { |
| |
|
|
| |
| console.error("Error running job:", err); |
| this._numProcsCurrentlyRunning -= this._procsPerJobBatch; |
|
|
| |
| this._onError(inputBatch, err); |
|
|
| let msg = err.message; |
| if (msg.indexOf("SharedArrayBuffer") !== -1) { |
| msg += ". Consider updating your Safari browser or using a different browser such as Google Chrome." |
| } |
| if (msg.indexOf("must not be shared") !== -1) { |
| msg += ". This browser (likely Safari) does not support using SharedArrayBuffer with TextDecoder. We recommend using Google Chrome on Desktop for docking." |
| } |
| messagesApi.popupError(msg); |
| throw new Error(msg); |
| }); |
| } |
| } |
|
|
| |
| |
| |
| private _onQueueStart() { |
| if (this._showInQueue) { |
| startInQueueStore(this._id, this._maxTotalProcs, () => { |
| |
| |
|
|
| |
| this.jobsCancelling = true; |
|
|
| |
| clearInterval(this._queueTimer); |
| }); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| private _onJobDone(jobInfo: IJobInfo) { |
| if (this._callbacks && this._callbacks.onJobDone) { |
| this._callbacks.onJobDone(jobInfo.output, jobInfo.index); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| private _onError(jobInfos: IJobInfo[], error: any) { |
| const payloadsOfBatchThatFailed = jobInfos.map((jobInfo) => { |
| return jobInfo.input; |
| }); |
|
|
| if (this._callbacks && this._callbacks.onError) { |
| this._callbacks.onError(payloadsOfBatchThatFailed, error); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| private _onQueueDone(outputJobs: IJobInfo[]) { |
| |
| outputJobs.sort((a, b) => a.index - b.index); |
|
|
| |
| const outputPayloads = outputJobs.map((jobInfo) => jobInfo.output); |
|
|
| if (this._showInQueue) { |
| doneInQueueStore(this._id); |
| } |
|
|
| if (this._callbacks && this._callbacks.onQueueDone) { |
| this._callbacks.onQueueDone(outputPayloads); |
| } |
|
|
| |
| |
| this._doneResolveFunc(outputPayloads); |
| } |
|
|
| |
| |
| |
| |
| |
| protected _onProgress(percent: number) { |
| if (this._showInQueue) { |
| updateProgressInQueueStore(this._id, percent); |
| } |
| if (this._callbacks && this._callbacks.onProgress) { |
| this._callbacks.onProgress(percent); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public abstract runJobBatch( |
| inputBatch: IJobInfo[], |
| procs: number |
| ): Promise<IJobInfo[]>; |
| } |
|
|