import { ITask } from './ITask' import cp from 'child_process' import { LinkedList } from './LinkedList' export interface IExecutor { execute(task: ITask): Promise shutdown(): void } export type ExecutorFactory = () => IExecutor export class PromiseExecutor implements IExecutor { constructor(readonly execute: (task: ITask) => Promise) {} shutdown() { // do nothing } } class SubprocessExecutor implements IExecutor { process: cp.ChildProcess constructor( protected sourceFile: string, protected taskQueue: LinkedList>, ) { this.process = cp.fork(sourceFile) } async execute(task: ITask): Promise { return new Promise((resolve, reject) => { this.process.on('status_' + task.id, message => { if (message.error) { reject(message.error) } else { resolve(message.result) } }) this.process!.send(task) }) } shutdown() { this.process.kill('SIGKILL') } }