deploy_pool.js
1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
class DeployPool {
constructor() {
this.poolSize = 5;
this.pool = [];
this.running = new Map();
}
deploy(de) {
de.error = 0;
this.pool.push(de);
setTimeout(() => {
this.run();
}, 1);
}
async run() {
if (this.running.size < this.poolSize) {
let de = this.pool.shift();
if (de) {
let id = await de.deploy(function(err) {
if (err && this.running.has(de.id)) {
console.log(`deploy fail ${de.id} : ` + err);
de.error = (de.error + 1) || 1;
if (de.error < 3) {
this.pool.push(de);
}
}
this.running.delete(de.id);
setTimeout(function(){
this.run();
}.bind(this), 1000);
}.bind(this));
this.running.set(id, de);
}
}
}
stop() {
this.pool = [];
let iter = this.running.keys();
let next = iter.next();
while(!next.done) {
this.running.get(next.value).stop();
this.running.delete(next.value);
next = iter.next();
}
}
}
const pool = new DeployPool();
module.exports = pool;