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,13 +1,14 @@
// AI CORE
import * as WebSocket from 'ws';
import * as request from 'request-promise-native';
const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
import serifs from './serifs';
import config from './config';
import IModule from './module';
import MessageLike from './message-like';
import ReversiModule from './modules/reversi';
import ServerModule from './modules/server';
const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
/**
*
@ -45,6 +46,7 @@ export default class 藍 {
}
public install = (module: IModule) => {
module.install(this);
this.modules.push(module);
}
@ -120,6 +122,9 @@ export default class 藍 {
const ai = new ();
const serverModule = new ServerModule();
ai.install(serverModule);
if (config.reversiEnabled) {
const reversiModule = new ReversiModule();
ai.install(reversiModule);

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);
}
}

View file

@ -7,5 +7,16 @@ export default {
/**
*
*/
REVERSI_DECLINE: 'ごめんなさい、今リバーシはするなと言われてます...'
REVERSI_DECLINE: 'ごめんなさい、今リバーシはするなと言われてます...',
/**
* ()
*/
REBOOT_SCHEDULED: 'サーバーの空きメモリが少なくなってきたので、1分後にサーバー再起動しますね',
/**
*
*/
REBOOT: 'では、まもなくサーバーを再起動します!',
REBOOT_DETAIL: '(私も再起動に巻き込まれちゃうので、サーバーの再起動が完了したことのお知らせはできません...)'
};