handle HIEC multiple externally , process two files at a time , using a double the size of that as worker pool also use png as it didn't crash for two parralel files and add a timeout to heic conversion

This commit is contained in:
Abhinav 2022-03-09 11:10:20 +05:30
parent 0ced60ac05
commit ec15d63a7f
6 changed files with 66 additions and 36 deletions

View file

@ -1,31 +0,0 @@
import QueueProcessor from 'services/queueProcessor';
import { ConvertWorker } from 'utils/comlink';
import { CustomError } from 'utils/error';
import { logError } from 'utils/sentry';
class HEICConverter {
private convertProcessor = new QueueProcessor<Blob>(5);
private worker = null;
async convert(fileBlob: Blob, format = 'JPEG'): Promise<Blob> {
if (!this.worker) {
this.worker = await new ConvertWorker();
}
const response = this.convertProcessor.queueUpRequest(
async () => await this.worker.convertHEIC(fileBlob, format)
);
try {
return await response.promise;
} catch (e) {
if (e.message === CustomError.REQUEST_CANCELLED) {
// ignore
return null;
} else {
logError(e, 'heic conversion failed');
throw e;
}
}
}
}
export default new HEICConverter();

View file

@ -1,10 +1,15 @@
import * as HeicConvert from 'heic-convert';
const WAIT_TIME_IN_MICROSECONDS = 60 * 1000;
export async function convertHEIC(
fileBlob: Blob,
format: string
): Promise<Blob> {
const filedata = new Uint8Array(await fileBlob.arrayBuffer());
setTimeout(() => {
throw Error('wait time exceeded');
}, WAIT_TIME_IN_MICROSECONDS);
const result = await HeicConvert({ buffer: filedata, format });
const convertedFileData = new Uint8Array(result);
const convertedFileBlob = new Blob([convertedFileData]);

View file

@ -0,0 +1,56 @@
import QueueProcessor from 'services/queueProcessor';
import { getDedicatedConvertWorker } from 'utils/comlink';
import { CustomError } from 'utils/error';
import { logError } from 'utils/sentry';
const WORKER_POOL_SIZE = 4;
const MAX_CONVERSION_IN_PARALLEL = 2;
class HEICConverter {
private convertProcessor = new QueueProcessor<Blob>(
MAX_CONVERSION_IN_PARALLEL
);
private workerPool: any[];
private ready: Promise<void>;
constructor() {
this.ready = this.init();
}
async init() {
this.workerPool = [];
for (let i = 0; i < WORKER_POOL_SIZE; i++) {
const worker = getDedicatedConvertWorker().comlink;
if (!worker) {
return;
}
this.workerPool.push({
id: i,
worker: await new worker(),
});
}
}
async convert(fileBlob: Blob, format = 'PNG'): Promise<Blob> {
await this.ready;
const response = this.convertProcessor.queueUpRequest(async () => {
const { id, worker } = this.workerPool.shift();
try {
return await worker.convertHEIC(fileBlob, format);
} finally {
this.workerPool.push({ id, worker });
}
});
try {
return await response.promise;
} catch (e) {
if (e.message === CustomError.REQUEST_CANCELLED) {
// ignore
return null;
} else {
logError(e, 'heic conversion failed');
throw e;
}
}
}
}
export default new HEICConverter();

View file

@ -7,7 +7,7 @@ import { convertToHumanReadable } from 'utils/billing';
import { isFileHEIC } from 'utils/file';
import { FileTypeInfo } from 'types/upload';
import { getUint8ArrayView } from '../readerService';
import HEICConverter from 'services/HEICConverter';
import HEICConverter from 'services/heicConverter/heicConverterService';
import { getFileNameSize, logUploadInfo } from 'utils/upload';
const MAX_THUMBNAIL_DIMENSION = 720;

View file

@ -23,7 +23,7 @@ import {
VISIBILITY_STATE,
} from 'constants/file';
import PublicCollectionDownloadManager from 'services/publicCollectionDownloadManager';
import HEICConverter from 'services/HEICConverter';
import HEICConverter from 'services/heicConverter/heicConverterService';
import ffmpegService from 'services/ffmpeg/ffmpegService';
export function downloadAsFile(filename: string, content: string) {

View file

@ -1,9 +1,9 @@
import * as Comlink from 'comlink';
import { convertHEIC } from 'utils/file/convertHEIC';
import { convertHEIC } from 'services/heicConverter/heicConverterClient';
export class Convert {
async convertHEIC(format, file) {
return convertHEIC(format, file);
async convertHEIC(fileBlob, format) {
return convertHEIC(fileBlob, format);
}
}