mirror of
https://github.com/nullnyat/NullcatChan.git
synced 2025-08-04 06:36:29 +09:00
✌️
This commit is contained in:
@ -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) {
|
||||
|
||||
|
81
src/modules/server/index.ts
Normal file
81
src/modules/server/index.ts
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user