-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-store.js
81 lines (67 loc) · 1.59 KB
/
server-store.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var pm2 = require('pm2');
var uuid = require('node-uuid');
var serverStore = {
interval: null,
list: [],
default_servers: 5,
max_server: 10,
init() {
return new Promise((resolve, reject) => {
pm2.connect((err) => {
if(err) reject(err);
this.update().then((list) => {
console.log(list);
resolve();
});
});
});
},
cleanUp() {
pm2.disconnect();
},
create() {
console.log('Creating server...');
var id = uuid.v4();
pm2.start('../server/index.js',{
name: 'tis-server-' + id,
force: true
}, (err) => {
console.log('ERROR');
console.log(err);
});
},
poll(cb) {
if(this.interval) return;
this.timeout = setTimeout(() => {
this.update().then((server_list) => {
this.list = server_list;
console.log(this.list.length);
this.poll();
// query server for status
});
}, 1000);
},
update() {
return new Promise((resolve, reject) => {
pm2.list((err, list) => {
if (err) {
reject(err);
} else {
var data = list.filter((element) => {
return (element.name.indexOf('tis-server') !== -1 && element.pm2_env.status === 'online');
}).map((element) => {
return {
name: element.name,
id: element.pm_id,
pid: element.pid,
port: element.pm2_env.NODE_PORT || 8080
};
})
this.list = data;
resolve(data);
}
});
});
}
};
module.exports = serverStore;