2018-03-31 03:30:30 +08:00
|
|
|
import {DiskMangerWorker} from './DiskMangerWorker';
|
|
|
|
import {Logger} from '../../Logger';
|
2018-07-29 03:27:05 +08:00
|
|
|
import {RendererInput, ThumbnailWorker} from './ThumbnailWorker';
|
2018-03-31 03:30:30 +08:00
|
|
|
import {ThumbnailProcessingLib} from '../../../common/config/private/IPrivateConfig';
|
2018-12-09 01:17:33 +08:00
|
|
|
import {DirectoryDTO} from '../../../common/entities/DirectoryDTO';
|
|
|
|
import {Utils} from '../../../common/Utils';
|
2017-07-24 04:37:29 +08:00
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
export class Worker {
|
|
|
|
|
|
|
|
|
|
|
|
public static process() {
|
2018-03-31 03:30:30 +08:00
|
|
|
Logger.debug('Worker is waiting for tasks');
|
2018-11-02 17:40:09 +08:00
|
|
|
process.on('message', async (task: WorkerTask) => {
|
2017-07-04 16:24:20 +08:00
|
|
|
try {
|
|
|
|
let result = null;
|
|
|
|
switch (task.type) {
|
|
|
|
case WorkerTaskTypes.diskManager:
|
|
|
|
result = await DiskMangerWorker.scanDirectory((<DiskManagerTask>task).relativeDirectoryName);
|
2017-07-24 04:37:29 +08:00
|
|
|
if (global.gc) {
|
|
|
|
global.gc();
|
|
|
|
}
|
2017-07-04 16:24:20 +08:00
|
|
|
break;
|
|
|
|
case WorkerTaskTypes.thumbnail:
|
2018-07-29 01:58:17 +08:00
|
|
|
result = await ThumbnailWorker.render((<ThumbnailTask>task).input, (<ThumbnailTask>task).renderer);
|
2017-07-04 16:24:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
2018-05-13 00:19:51 +08:00
|
|
|
throw new Error('Unknown worker task type');
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
process.send(<WorkerMessage>{
|
|
|
|
error: null,
|
|
|
|
result: result
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
process.send({error: err, result: null});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-24 04:37:29 +08:00
|
|
|
export enum WorkerTaskTypes {
|
2018-05-29 02:03:12 +08:00
|
|
|
thumbnail = 1, diskManager = 2
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface WorkerTask {
|
|
|
|
type: WorkerTaskTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DiskManagerTask extends WorkerTask {
|
|
|
|
relativeDirectoryName: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ThumbnailTask extends WorkerTask {
|
|
|
|
input: RendererInput;
|
|
|
|
renderer: ThumbnailProcessingLib;
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:17:33 +08:00
|
|
|
export module WorkerTask {
|
|
|
|
export const equals = (t1: WorkerTask, t2: WorkerTask): boolean => {
|
|
|
|
return Utils.equalsFilter(t1, t2);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
export interface WorkerMessage {
|
2018-12-09 01:17:33 +08:00
|
|
|
error: Error;
|
|
|
|
result: DirectoryDTO | void;
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|