From e618b9737fe1b4eebbfeec2eaabd5bb923179c83 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Wed, 5 Feb 2020 23:24:09 +0000 Subject: schedule.js prevents off-by-one error by popping minimum amount in step 3 --- tools/schedule.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/schedule.js b/tools/schedule.js index caad554..dfbe854 100644 --- a/tools/schedule.js +++ b/tools/schedule.js @@ -28,8 +28,8 @@ exports.Queue = function(delayms, maxExport, call){ this.size = 0; this.enqueue = function(job){ - if (! job[0]) return; if (! (job instanceof exports.Job)) job = new exports.Job(job); + if (! job.data[0]) return; let prio = job.prio; if (!jobs[prio]){ jobs[prio] = []; @@ -94,15 +94,15 @@ exports.Queue = function(delayms, maxExport, call){ // Step 3: Then, pop job.normweight*num//1 elems from remaining, without num decrease or normweight recalc. But keep job.wacc = job.normweight*num%1 let efflen = num - dequeued.length; for (job of jobq){ - job.wacc += job.wt*efflen/weightsum; + let topop = job.wt*efflen/weightsum; + job.wacc += topop%1; + topop = topop-topop%1; let data = job.data; - let topop = job.wacc-job.wacc%1; - job.wacc -= topop dequeued.push(...data.splice(data.length-topop)); } // Step 4: Shallow copy job array, and sort by job.wacc. - for (job of jobq.splice().sort((el, ne) => el.wacc-ne.wacc)){ + for (job of jobq.slice().sort((el, ne) => el.wacc-ne.wacc)){ // Step 5: Iterate through array (high->low), and subtract 1 until the length of output is num. if (dequeued.length == num) break; job.wacc--; -- cgit