This commit is contained in:
syuilo
2018-08-11 12:19:34 +09:00
parent 986fe39762
commit dbae24bcc8
4 changed files with 102 additions and 7 deletions

View File

@ -1,12 +1,11 @@
import * as childProcess from 'child_process';
const ReconnectingWebSocket = require('../../../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
import from '../..';
import IModule from '../../module';
import serifs from '../../serifs';
import config from '../../config';
import MessageLike from '../../message-like';
import * as WebSocket from 'ws';
export default class ReversiModule implements IModule {
private ai: ;
@ -56,7 +55,6 @@ export default class ReversiModule implements IModule {
}
}
private onReversiConnectionMessage = (msg: any) => {
switch (msg.type) {

View File

@ -0,0 +1,81 @@
import * as childProcess from 'child_process';
import * as WebSocket from 'ws';
import from '../..';
import IModule from '../../module';
import serifs from '../../serifs';
import config from '../../config';
const ReconnectingWebSocket = require('../../../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
export default class ServerModule implements IModule {
private ai: ;
private connection?: any;
private rebootScheduled = false;
private rebootTimer: any;
private rebootTimerSub: any;
public install = (ai: ) => {
this.ai = ai;
this.connection = new ReconnectingWebSocket(`${config.wsUrl}/server-stats`, [], {
WebSocket: WebSocket
});
this.connection.addEventListener('open', () => {
console.log('server-stats stream opened');
});
this.connection.addEventListener('close', () => {
console.log('server-stats stream closed');
});
this.connection.addEventListener('message', message => {
const msg = JSON.parse(message.data);
this.onConnectionMessage(msg);
});
}
private onConnectionMessage = (msg: any) => {
switch (msg.type) {
case 'stats': {
this.onStats(msg.body);
break;
}
default:
break;
}
}
private onStats = async (stats: any) => {
const memUsage = Math.round((stats.mem.used / stats.mem.total) * 100);
console.log(`[SERVER] MEM: ${memUsage}%`);
if (memUsage >= 90) {
this.scheduleReboot();
}
}
private scheduleReboot = () => {
if (this.rebootScheduled) return;
this.rebootScheduled = true;
this.ai.post({
text: serifs.REBOOT_SCHEDULED
});
this.rebootTimer = setTimeout(() => {
childProcess.exec('forever restartall');
}, 1000 * 60);
this.rebootTimerSub = setTimeout(() => {
this.ai.post({
cw: serifs.REBOOT,
text: serifs.REBOOT_DETAIL
});
}, 1000 * 50);
}
}