forked from exi/rsyncCopyManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverManager.js
78 lines (64 loc) · 1.88 KB
/
serverManager.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
var database = require('./database.js');
var Server = require('./server.js');
var Promise = require('node-promise').Promise;
var ServerManager = module.exports = function(dependencies) {
var servers = {};
var api = {};
api.addServer = function(server) {
if (server && server.id) {
servers[server.id] = {
model: server,
manager: new Server(dependencies, server.id)
};
}
};
api.delServer = function(serverId) {
var p = new Promise();
if (servers.hasOwnProperty(serverId)) {
var server = servers[serverId].manager;
server.closeAndDelete().then(function() {
delete servers[serverId];
dependencies.eventBus.emit('server-removed', serverId);
p.resolve();
});
} else {
p.resolve();
}
return p;
};
api.rescanServer = function(serverId) {
var p = new Promise();
if (servers.hasOwnProperty(serverId)) {
var server = servers[serverId].manager;
console.log('sm rescan ' + serverId);
server.rescan();
}
p.resolve();
return p;
};
api.getServerStatus = function(serverId) {
var p = new Promise();
if (servers.hasOwnProperty(serverId)) {
servers[serverId].manager.getStatus().then(function(status) {
p.resolve(status);
});
} else {
p.reject('Server not active.');
}
return p;
};
api.close = function() {
for (var i in servers) {
servers[i].manager.close();
}
};
database(function(err, models) {
if (err) {
throw err;
}
models.Server.all().success(function(s) {
s.forEach(api.addServer);
});
});
return api;
};