Files: 47cdaf541c5fb70cd51572ef2e40fda377cfe086 / scheduler.js
3916 bytesRaw
1 | const binarySearchInsert = require('binary-search-insert') |
2 | const SortedMap = require('sortedmap') |
3 | const LockMap = require('lockmap') |
4 | |
5 | function comparator (a, b) { |
6 | return a.ticks - b.ticks |
7 | } |
8 | |
9 | module.exports = class Scheduler { |
10 | /** |
11 | * The Scheduler manages the run cycle of Actors and figures out which |
12 | * order they should run in |
13 | */ |
14 | constructor () { |
15 | this._waits = [] |
16 | this._running = new Set() |
17 | this._loadingInstances = new LockMap() |
18 | this._checkingWaits = false |
19 | this.instances = new SortedMap(comparator) |
20 | } |
21 | |
22 | /** |
23 | * locks the scheduler from clearing waits untill the lock is resolved |
24 | * @param {string} id |
25 | * @return {function} the resolve function to call once it to unlock |
26 | */ |
27 | lock (id) { |
28 | return this._loadingInstances.lock(id) |
29 | } |
30 | |
31 | /** |
32 | * updates an instance with a new tick count |
33 | * @param {Object} instance - an actor instance |
34 | */ |
35 | update (instance) { |
36 | this._waits = this._waits.filter(wait => wait.id !== instance.id) |
37 | this._update(instance) |
38 | this._running.add(instance.id) |
39 | this._checkWaits() |
40 | } |
41 | |
42 | _update (instance) { |
43 | this.instances.delete(instance.id) |
44 | this.instances.set(instance.id, instance) |
45 | } |
46 | |
47 | /** |
48 | * returns an Actor instance |
49 | * @param {String} id |
50 | * @return {Object} |
51 | */ |
52 | getInstance (id) { |
53 | return this.instances.get(id) || this._loadingInstances.get(id) |
54 | } |
55 | |
56 | /** |
57 | * deletes an instance from the scheduler |
58 | * @param {String} id - the containers id |
59 | */ |
60 | done (id) { |
61 | this._running.delete(id) |
62 | this.instances.delete(id) |
63 | this._checkWaits() |
64 | } |
65 | |
66 | /** |
67 | * returns a promise that resolves once all containers have reached the given |
68 | * number of ticks |
69 | * @param {interger} ticks - the number of ticks to wait |
70 | * @param {string} id - optional id of the container that is waiting |
71 | * @return {Promise} |
72 | */ |
73 | wait (ticks, id) { |
74 | this._running.delete(id) |
75 | return new Promise((resolve, reject) => { |
76 | binarySearchInsert(this._waits, comparator, { |
77 | ticks: ticks, |
78 | resolve: resolve, |
79 | id: id |
80 | }) |
81 | this._checkWaits() |
82 | }) |
83 | } |
84 | |
85 | /** |
86 | * returns the oldest container's ticks |
87 | * @return {integer} |
88 | */ |
89 | leastNumberOfTicks (exculde) { |
90 | let ticks = 0 |
91 | for (const instance of this.instances) { |
92 | ticks = instance[1].ticks |
93 | if (instance[1].id !== exculde) { |
94 | return ticks |
95 | } |
96 | } |
97 | return ticks |
98 | } |
99 | |
100 | // checks outstanding waits to see if they can be resolved |
101 | async _checkWaits () { |
102 | if (this._checkingWaits) { |
103 | 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 | } |
109 | // if there are no running containers |
110 | if (!this.instances.size) { |
111 | // clear any remanding waits |
112 | this._waits.forEach(wait => wait.resolve()) |
113 | this._waits = [] |
114 | 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 { |
125 | break |
126 | } |
127 | } |
128 | |
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 | } |
148 | } |
149 | } |
150 |
Built with git-ssb-web