-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacesyncer.js
202 lines (170 loc) · 6.08 KB
/
spacesyncer.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var winston = require('winston');
var config = require('config');
// logger
var _loggerconfig;
if (config.env=="PRODUCTION") _loggerconfig = config.logger.production;
else _loggerconfig = config.logger.dev;
winston.loggers.add('space.syncer_log',_loggerconfig);
var logger = winston.loggers.get('space.syncer_log');
logger.info("[s p a c e syncer] - server initializes...");
var http = require('http');
var port = config.server.port;
var server = http.createServer(function(req,res){
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
res.write(JSON.stringify(config.sync));
res.end();
});
server.listen(config.server.port);
//var sockets=[];
var io = require('socket.io')(server);
io.on('connection', function (socket) {
logger.debug('[s p a c e syncer|server socket.io] says: new user connected!');
socket.emit("message","[s p a c e.syncer] says: hello :-)");
var connected = io.sockets.connected;
clientsCount = Object.keys(connected).length;
logger.debug("number of connections: "+clientsCount);
socket.on('forceSync',function(message){
logger.debug("[s p a c e syncer|server socket.io] says: someone sent a forceSync message - syncer: "+message.syncer);
var _syncer = message.syncer;
var _url = config.sync[_syncer].url;
var _type = "manual - forced"
switch(_syncer){
case "v1epics":
v1EpicSyncService.sync(_url,_type,io,function(err,result){
if (!err) logger.debug("successful: "+_syncer);
});
break;
case "v1data":
v1DataSyncService.sync(_url,_type,io,function(err,result){
if (!err) logger.debug("successful: "+_syncer);
});
break;
case "availability":
avSyncService.sync(_url,_type,io,function(err,result){
if (!err) logger.debug("successful: "+_syncer);
});
break;
case "incidents":
incidentSyncService.sync(_url,_type,io,function(err,result){
if (!err) logger.debug("successful: "+_syncer);
});
break;
case "problems":
problemSyncService.sync(_url,_type,io,function(err,result){
if (!err) logger.debug("successful: "+_syncer);
});
break;
case "socoutages":
socoutagesSyncService.sync(_url,_type,io,function(err,result){
if (!err) logger.debug("successful: "+_syncer);
});
break;
case "socservices":
socservicesSyncService.sync("socservices",_url,_type,io,function(err,result){
if (!err) logger.debug("successful: "+_syncer);
});
break;
}
})
socket.on('message',function(message){
logger.debug("[s p a c e syncer|server socket.io] says: someone sent a MESSAGE message : ");
socket.emit('message',message);
socket.broadcast.emit('message',message);
})
socket.on('heartbeat',function(message){
logger.debug("[s p a c e syncer|server socket.io] says: someone sent a HEARTBEAT: "+message);
var connected = io.sockets.connected;
clientsCount = Object.keys(connected).length;
logger.debug("number of connections: "+clientsCount);
socket.emit('heartbeat',"space.syncer ALIVE");
//socket.removeListener('heartbeat');
})
socket.on('disconnect',function(){
logger.debug("[s p a c e syncer|server socket.io] says: someone disconnected")
var connected = io.sockets.connected;
clientsCount = Object.keys(connected).length;
logger.debug("number of connections: "+clientsCount);
})
});
// services
var v1EpicSyncService = require('./services/V1EpicSyncService');
v1EpicSyncService.init(io,function(err,result){
if (err){
logger.error("error: "+err.message);
}
else
{
logger.info("init ok: "+result);
}
});
var v1DataSyncService = require('./services/V1DataSyncService');
v1DataSyncService.init(io,function(err,result){
if (err){
logger.error("error: "+err.message);
}
else
{
logger.info("init ok: "+result);
}
});
var mockupSyncService = require('./services/MockupSyncService');
mockupSyncService.init(io,function(err,result){
if (err){
logger.error("error: "+err.message);
}
else
{
logger.info("init ok: "+result);
}
});
var avSyncService = require('./services/AvailabilitySyncService');
avSyncService.init(io,function(err,av){
if (err) logger.error("[error]: "+err.message);
else logger.info("[ok]: "+JSON.stringify(av));
});
var incidentSyncService = require('./services/IncidentSyncService');
incidentSyncService.init(io,function(err,result){
if (err){
logger.error("[error]: "+err.message);
}
else{
logger.info("IncidentSyncService.init() says: "+result);
}
});
var socoutagesSyncService = require('./services/SOCOutagesSyncService');
socoutagesSyncService.init(io,function(err,data){
if (err){
logger.error("error: "+err.message);
}
else{
logger.debug("socoutagesSyncService.init() says: "+data.length+" items synced");
}
});
var socservicesSyncService = require('./services/GenericSyncService');
socservicesSyncService.init(io,"socservices",function(err, data){
if (err){
logger.error("error: "+err.message);
}
else{
logger.debug("socservicesSyncService.init() says: "+data.length+" items synced");
}
});
var problemSyncService = require('./services/ProblemSyncService');
problemSyncService.init(io,function(err,result){
if (err){
logger.error("error: "+err.message);
}
else
{
logger.info("init ok: "+result);
}
});
/*
var apmSyncService = require('./services/ApmSyncService');
apmSyncService.init(io);
*/
logger.info("[s p a c e snycserver] - initializes DONE..");