File size: 1,716 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { QueueParent } from "@/Queue/QueueParent";
import { IJobInfo } from "@/Queue/QueueTypes";
import { runWorker } from "@/Core/WebWorkers/RunWorker";

/**
 * A reduce (prepare protein) queue.
 */
export class ReduceQueue extends QueueParent {
    /**
     * Run a batch of jobs.
     *
     * @param {IJobInfo[]} inputBatch  The input batch.
     * @param {number}     procs       The number of processors to use.
     * @returns {Promise<IJobInfo[]>}  The output batch.
     */
    public async runJobBatch(
        inputBatch: IJobInfo[],
        procs: number
    ): Promise<IJobInfo[]> {
        const outputs: IJobInfo[] = [];
        for (const jobInfo of inputBatch) {
            // NOTE: Choosing to do this one protein at a time. TODO: Could
            // batch if you end up thinking it's necessary. Would require some
            // refactoring.
            outputs.push(await this._runJob(jobInfo));
        }

        return outputs;
    }

    /**
     * Run a single job.
     *
     * @param {IJobInfo} jobInfo  The job to run.
     * @returns {Promise<IJobInfo>}  The output job.
     */
    private async _runJob(jobInfo: IJobInfo): Promise<IJobInfo> {
        // treeNode not serializable, and not needed, so remove it.
        jobInfo.input.treeNode = undefined;

        return runWorker(
            new Worker(new URL("./Reduce.worker", import.meta.url)),
            jobInfo,
            true // auto terminate the worker.
        )
            .then((response) => {
                // Update .output value
                jobInfo.output = response.output;
                return jobInfo;
            })
            .catch((err) => {
                throw err;
            });
    }
}