Merge pull request #212 from thecodingmachine/mitigateworlderror

Adding finally clause to make sure Prometheus indicators are ok
This commit is contained in:
David Négrier 2020-06-29 23:05:39 +02:00 committed by GitHub
commit 2eaba04f35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -48,12 +48,12 @@ export class IoSocketController {
this.nbClientsGauge = new Gauge({ this.nbClientsGauge = new Gauge({
name: 'workadventure_nb_sockets', name: 'workadventure_nb_sockets',
help: 'Number of connected sockets', help: 'Number of connected sockets',
labelNames: [ 'host' ] labelNames: [ ]
}); });
this.nbClientsPerRoomGauge = new Gauge({ this.nbClientsPerRoomGauge = new Gauge({
name: 'workadventure_nb_clients_per_room', name: 'workadventure_nb_clients_per_room',
help: 'Number of clients per room', help: 'Number of clients per room',
labelNames: [ 'host', 'room' ] labelNames: [ 'room' ]
}); });
// Authentication with token. it will be decoded and stored in the socket. // Authentication with token. it will be decoded and stored in the socket.
@ -139,7 +139,7 @@ export class IoSocketController {
// Let's log server load when a user joins // Let's log server load when a user joins
const srvSockets = this.Io.sockets.sockets; const srvSockets = this.Io.sockets.sockets;
this.nbClientsGauge.inc({ host: os.hostname() }); this.nbClientsGauge.inc();
console.log(new Date().toISOString() + ' A user joined (', Object.keys(srvSockets).length, ' connected users)'); console.log(new Date().toISOString() + ' A user joined (', Object.keys(srvSockets).length, ' connected users)');
si.currentLoad().then(data => console.log(' Current load: ', data.avgload)); si.currentLoad().then(data => console.log(' Current load: ', data.avgload));
si.currentLoad().then(data => console.log(' CPU: ', data.currentload, '%')); si.currentLoad().then(data => console.log(' CPU: ', data.currentload, '%'));
@ -262,7 +262,7 @@ export class IoSocketController {
// Let's log server load when a user leaves // Let's log server load when a user leaves
const srvSockets = this.Io.sockets.sockets; const srvSockets = this.Io.sockets.sockets;
this.nbClientsGauge.dec({ host: os.hostname() }); this.nbClientsGauge.dec();
console.log('A user left (', Object.keys(srvSockets).length, ' connected users)'); console.log('A user left (', Object.keys(srvSockets).length, ' connected users)');
si.currentLoad().then(data => console.log('Current load: ', data.avgload)); si.currentLoad().then(data => console.log('Current load: ', data.avgload));
si.currentLoad().then(data => console.log('CPU: ', data.currentload, '%')); si.currentLoad().then(data => console.log('CPU: ', data.currentload, '%'));
@ -295,11 +295,12 @@ export class IoSocketController {
leaveRoom(Client : ExSocketInterface){ leaveRoom(Client : ExSocketInterface){
// leave previous room and world // leave previous room and world
if(Client.roomId){ if(Client.roomId){
try {
Client.to(Client.roomId).emit(SockerIoEvent.USER_LEFT, Client.userId); Client.to(Client.roomId).emit(SockerIoEvent.USER_LEFT, Client.userId);
//user leave previous world //user leave previous world
const world : World|undefined = this.Worlds.get(Client.roomId); const world: World | undefined = this.Worlds.get(Client.roomId);
if(world){ if (world) {
world.leave(Client); world.leave(Client);
if (world.isEmpty()) { if (world.isEmpty()) {
this.Worlds.delete(Client.roomId); this.Worlds.delete(Client.roomId);
@ -307,15 +308,17 @@ export class IoSocketController {
} }
//user leave previous room //user leave previous room
Client.leave(Client.roomId); Client.leave(Client.roomId);
this.nbClientsPerRoomGauge.dec({ host: os.hostname(), room: Client.roomId }); } finally {
this.nbClientsPerRoomGauge.dec({ room: Client.roomId });
delete Client.roomId; delete Client.roomId;
} }
} }
}
private joinRoom(Client : ExSocketInterface, roomId: string, position: PointInterface): World { private joinRoom(Client : ExSocketInterface, roomId: string, position: PointInterface): World {
//join user in room //join user in room
Client.join(roomId); Client.join(roomId);
this.nbClientsPerRoomGauge.inc({ host: os.hostname(), room: roomId }); this.nbClientsPerRoomGauge.inc({ room: roomId });
Client.roomId = roomId; Client.roomId = roomId;
Client.position = position; Client.position = position;