Commit 1a02908906ad592224398890dbe31da370639340
Cleanup scheduler documentation & _checkWaits
sdtsui committed on 11/22/2017, 12:43:11 AMParent: 47cdaf541c5fb70cd51572ef2e40fda377cfe086
Files changed
package-lock.json | changed |
scheduler.js | changed |
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 325800 bytes New file size: 342929 bytes |
scheduler.js | |||
---|---|---|---|
@@ -7,10 +7,9 @@ | |||
7 | 7 … | } | |
8 | 8 … | ||
9 | 9 … | module.exports = class Scheduler { | |
10 | 10 … | /** | |
11 | - * The Scheduler manages the run cycle of Actors and figures out which | ||
12 | - * order they should run in | ||
11 … | + * the Scheduler keeps track of which containers are instantiated, running, and waiting for a message | ||
13 | 12 … | */ | |
14 | 13 … | constructor () { | |
15 | 14 … | this._waits = [] | |
16 | 15 … | this._running = new Set() | |
@@ -71,8 +70,9 @@ | |||
71 | 70 … | * @return {Promise} | |
72 | 71 … | */ | |
73 | 72 … | wait (ticks, id) { | |
74 | 73 … | this._running.delete(id) | |
74 … | + | ||
75 | 75 … | return new Promise((resolve, reject) => { | |
76 | 76 … | binarySearchInsert(this._waits, comparator, { | |
77 | 77 … | ticks: ticks, | |
78 | 78 … | resolve: resolve, | |
@@ -85,65 +85,68 @@ | |||
85 | 85 … | /** | |
86 | 86 … | * returns the oldest container's ticks | |
87 | 87 … | * @return {integer} | |
88 | 88 … | */ | |
89 | - leastNumberOfTicks (exculde) { | ||
89 … | + leastNumberOfTicks (exclude) { | ||
90 | 90 … | let ticks = 0 | |
91 | 91 … | for (const instance of this.instances) { | |
92 | 92 … | ticks = instance[1].ticks | |
93 | - if (instance[1].id !== exculde) { | ||
93 … | + if (instance[1].id !== exclude) { | ||
94 | 94 … | return ticks | |
95 | 95 … | } | |
96 | 96 … | } | |
97 … | + | ||
97 | 98 … | return ticks | |
98 | 99 … | } | |
99 | 100 … | ||
100 | 101 … | // checks outstanding waits to see if they can be resolved | |
101 | 102 … | async _checkWaits () { | |
102 | 103 … | if (this._checkingWaits) { | |
103 | 104 … | return | |
104 | - } else { | ||
105 | - this._checkingWaits = true | ||
106 | - // wait to check waits untill all the instances are done loading | ||
107 | - await [...this._loadingInstances.values()] | ||
108 | 105 … | } | |
109 | - // if there are no running containers | ||
106 … | + this._checkingWaits = true | ||
107 … | + | ||
108 … | + // wait to check waits until all the instances are done loading | ||
109 … | + await [...this._loadingInstances.values()] | ||
110 … | + | ||
111 … | + // if there are no instances, clear any remaining waits | ||
110 | 112 … | if (!this.instances.size) { | |
111 | - // clear any remanding waits | ||
112 | 113 … | this._waits.forEach(wait => wait.resolve()) | |
113 | 114 … | this._waits = [] | |
114 | 115 … | this._checkingWaits = false | |
115 | - } else { | ||
116 | - // find the old container and see if to can resolve any of the waits | ||
117 | - while (this._waits[0]) { | ||
118 | - const wait = this._waits[0] | ||
119 | - const least = this.leastNumberOfTicks(wait.id) | ||
120 | - if (wait.ticks <= least) { | ||
121 | - this._waits.shift() | ||
122 | - wait.resolve() | ||
123 | - this._running.add(wait.id) | ||
124 | - } else { | ||
116 … | + | ||
117 … | + return | ||
118 … | + } | ||
119 … | + | ||
120 … | + // find the old container, see if any of the waits can be resolved | ||
121 … | + while (this._waits[0]) { | ||
122 … | + const wait = this._waits[0] | ||
123 … | + const least = this.leastNumberOfTicks(wait.id) | ||
124 … | + if (wait.ticks <= least) { | ||
125 … | + this._waits.shift() | ||
126 … | + wait.resolve() | ||
127 … | + this._running.add(wait.id) | ||
128 … | + } else { | ||
129 … | + break | ||
130 … | + } | ||
131 … | + } | ||
132 … | + | ||
133 … | + // if there are no containers running find the oldest wait | ||
134 … | + // and update the oldest containers to its ticks | ||
135 … | + if (!this._running.size && this._waits.length) { | ||
136 … | + const oldest = this._waits[0].ticks | ||
137 … | + for (let instance of this.instances) { | ||
138 … | + instance = instance[1] | ||
139 … | + if (instance.ticks > oldest) { | ||
125 | 140 … | break | |
126 | 141 … | } | |
142 … | + instance.ticks = oldest | ||
143 … | + this._update(instance) | ||
127 | 144 … | } | |
145 … | + this._checkingWaits = false | ||
128 | 146 … | ||
129 | - if (!this._running.size && this._waits.length) { | ||
130 | - // if there are no containers running find the oldest wait and update | ||
131 | - // the oldest containers to it ticks | ||
132 | - const oldest = this._waits[0].ticks | ||
133 | - for (let instance of this.instances) { | ||
134 | - instance = instance[1] | ||
135 | - if (instance.ticks > oldest) { | ||
136 | - break | ||
137 | - } else { | ||
138 | - instance.ticks = oldest | ||
139 | - this._update(instance) | ||
140 | - } | ||
141 | - } | ||
142 | - this._checkingWaits = false | ||
143 | - return this._checkWaits() | ||
144 | - } else { | ||
145 | - this._checkingWaits = false | ||
146 | - } | ||
147 … | + return this._checkWaits() | ||
147 | 148 … | } | |
149 … | + | ||
150 … | + this._checkingWaits = false | ||
148 | 151 … | } | |
149 | 152 … | } |
Built with git-ssb-web