From e8ebf9ec7639a95ddf1a0a61a7a4996a570e261a Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Sat, 18 Jan 2020 08:44:01 -0500 Subject: queue-compat and bugfix in schedule.js---calls from jarvis still don't work --- tools/schedule.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'tools') diff --git a/tools/schedule.js b/tools/schedule.js index 83d7853..18278f0 100644 --- a/tools/schedule.js +++ b/tools/schedule.js @@ -1,7 +1,11 @@ // A mixed paradigm scheduler +// RECOMMENDED: Adjust maxr with each round, unless this is determined to be potentially circumventable (maybe maxr ranks remain constant as long as they are recalc on insertion---so function-style?) + +const bs = require('binary-search'); + // The core managed data type: a queue will be pushed a number of these and asked to manage it. -function Job(data, prio=0, wt=1){ +exports.Job = function(data, prio=0, wt=1){ // Data is an array; // Ideally, Both prio and wt are functions which take one argument: how many write cycles they've been waiting. This helps manage time-sensitive jobs. // But this is too computationally difficult, so they are integer constants. @@ -16,7 +20,7 @@ function Job(data, prio=0, wt=1){ this.wacc = 0; // mutable property: Queue will change this to keep track between dequeues of how much "left over" push real estate this should have. } -function Queue(maxExport, call, delay){ +exports.Queue = function(delayms, maxExport, call){ // Every delay ms, Queue executes `call(maxExport # of objs in queue)` let jobs = {}; // Links priorities to unordered arrays of jobs let prios = []; // Array of priorities (keys of jobs), sorted. @@ -24,6 +28,7 @@ function Queue(maxExport, call, delay){ let disab = true; // Is the queue disabled? this.enqueue = function(job){ + if (! (job instanceof exports.Job)) job = new exports.Job(job); let prio = job.prio; if (!jobs[prio]) jobs[prio] = []; jobs[prio].splice(0, Math.abs(bs(jobs[prio], job, (el, ne) => el.maxr - ne.maxr)), job); @@ -53,10 +58,9 @@ function Queue(maxExport, call, delay){ } open = false; let data = []; - while (data.length < maxExport){ - data.push(...getNumOrAll(prios[prios.length-1]), maxExport-data.length); + while (prios.length > 0 && data.length < maxExport){ + data.push(...getNumOrAll(prios[prios.length-1], maxExport-data.length)); } - call(data); setTimeout(dequeue, delayms); } @@ -67,8 +71,8 @@ function Queue(maxExport, call, delay){ let dequeued = []; // Step 2: Start at lowest, and pop all until job.data.length>job.normweight*num (decreasing num as popping and recalc job.normweight). Delete the job. - let weightsum = Math.sum(jobq.map(job => job.wt)).reduce((acc, cur)=>acc+cur); - while (job[0].data.length<(job[0].wacc+job[0].wt*num/weightsum)){ + let weightsum = jobq.map(job => job.wt).reduce((acc, cur)=>acc+cur); + while (jobq[0] && jobq[0].data.length<(jobq[0].wacc+jobq[0].wt*num/weightsum)){ weightsum -= job.wt; dequeued.push(...jobq.shift().data); } -- cgit