Files: af5a1e853077d12a3a4c73c48de51492cc08f615 / scheduler.js
3976 bytesRaw
1 | const binarySearchInsert = require('binary-search-insert') |
2 | const SortedMap = require('sortedmap') |
3 | const LockMap = require('lockmap') |
4 | |
5 | module.exports = class Scheduler { |
6 | /** |
7 | * The Scheduler manages the run cycle of the containers and figures out which |
8 | * order they should run in |
9 | */ |
10 | constructor () { |
11 | this._waits = [] |
12 | this._running = new Set() |
13 | this._loadingInstances = new LockMap() |
14 | this.instances = new SortedMap(comparator) |
15 | this.systemServices = new Map() |
16 | |
17 | function comparator (a, b) { |
18 | return a.ticks - b.ticks |
19 | } |
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 - a container 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 a container |
49 | * @param {String} id |
50 | * @return {Object} |
51 | */ |
52 | getInstance (id) { |
53 | return this.instances.get(id) || this._loadingInstances.get(id) || this.systemServices.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 | function comparator (a, b) { |
85 | return a.ticks - b.ticks |
86 | } |
87 | } |
88 | |
89 | /** |
90 | * returns the oldest container's ticks |
91 | * @return {integer} |
92 | */ |
93 | syncLeastNumberOfTicks (exculde) { |
94 | let ticks = 0 |
95 | for (const instance of this.instances) { |
96 | ticks = instance[1].ticks |
97 | if (instance[1].id !== exculde) { |
98 | return ticks |
99 | } |
100 | } |
101 | return ticks |
102 | } |
103 | |
104 | // checks outstanding waits to see if they can be resolved |
105 | async _checkWaits () { |
106 | if (this._checkingWaits) { |
107 | return |
108 | } else { |
109 | this._checkingWaits = true |
110 | await [...this._loadingInstances.values()] |
111 | } |
112 | // if there are no running containers |
113 | if (!this.instances.size) { |
114 | // clear any remanding waits |
115 | this._waits.forEach(wait => wait.resolve()) |
116 | this._waits = [] |
117 | this._checkingWaits = false |
118 | } else { |
119 | // find the old container and see if to can resolve any of the waits |
120 | while (this._waits[0]) { |
121 | const wait = this._waits[0] |
122 | const least = this.syncLeastNumberOfTicks(wait.id) |
123 | if (wait.ticks <= least) { |
124 | this._waits.shift() |
125 | wait.resolve() |
126 | this._running.add(wait.id) |
127 | } else { |
128 | break |
129 | } |
130 | } |
131 | |
132 | if (!this._running.size && this._waits.length) { |
133 | // if there are no containers running find the oldest wait and update |
134 | // the oldest containers to it ticks |
135 | const oldest = this._waits[0].ticks |
136 | for (let instance of this.instances) { |
137 | instance = instance[1] |
138 | if (instance.ticks > oldest) { |
139 | break |
140 | } else { |
141 | instance.ticks = oldest |
142 | this._update(instance) |
143 | } |
144 | } |
145 | this._checkingWaits = false |
146 | return this._checkWaits() |
147 | } else { |
148 | this._checkingWaits = false |
149 | } |
150 | } |
151 | } |
152 | } |
153 |
Built with git-ssb-web