CPU使用率が高くても再起動するように

This commit is contained in:
syuilo 2018-08-12 12:34:46 +09:00
parent 40543b42e2
commit 9ebd6cf62c
2 changed files with 41 additions and 11 deletions

View file

@ -13,6 +13,12 @@ export default class ServerModule implements IModule {
private preventScheduleReboot = false; private preventScheduleReboot = false;
private rebootTimer: NodeJS.Timer; private rebootTimer: NodeJS.Timer;
private rebootTimerSub: NodeJS.Timer; private rebootTimerSub: NodeJS.Timer;
private recentStat: any[] = [];
/**
* 11
*/
private statsLogs: any;
public install = (ai: ) => { public install = (ai: ) => {
this.ai = ai; this.ai = ai;
@ -34,6 +40,31 @@ export default class ServerModule implements IModule {
this.onConnectionMessage(msg); this.onConnectionMessage(msg);
}); });
setInterval(() => {
this.statsLogs.push(this.recentStat);
if (this.statsLogs.length > 60) this.statsLogs.unshift();
}, 1000);
setInterval(() => {
this.check();
}, 1000);
}
private check = () => {
const average = (arr) => arr.reduce((a, b) => a + b) / arr.length;
const memPercentages = this.statsLogs.map(s => (s.mem.total - s.mem.used) * 100);
const memPercentage = average(memPercentages);
if (memPercentage >= 90) {
this.scheduleReboot('mem');
}
const cpuPercentages = this.statsLogs.map(s => s.cpu_usage * 100);
const cpuPercentage = average(cpuPercentages);
if (cpuPercentage >= 90) {
this.scheduleReboot('cpu');
}
} }
private onConnectionMessage = (msg: any) => { private onConnectionMessage = (msg: any) => {
@ -50,22 +81,16 @@ export default class ServerModule implements IModule {
} }
private onStats = async (stats: any) => { private onStats = async (stats: any) => {
const memUsage = Math.round((stats.mem.used / stats.mem.total) * 100); this.recentStat = stats;
console.log(`[SERVER] MEM: ${memUsage}%`);
if (memUsage >= 90) {
this.scheduleReboot();
}
} }
private scheduleReboot = () => { private scheduleReboot = (reason: string) => {
if (this.preventScheduleReboot) return; if (this.preventScheduleReboot) return;
this.preventScheduleReboot = true; this.preventScheduleReboot = true;
this.ai.post({ this.ai.post({
text: serifs.REBOOT_SCHEDULED text: reason == 'cpu' ? serifs.REBOOT_SCHEDULED_CPU : serifs.REBOOT_SCHEDULED_MEM
}); });
this.rebootTimer = setTimeout(() => { this.rebootTimer = setTimeout(() => {

View file

@ -10,9 +10,14 @@ export default {
REVERSI_DECLINE: 'ごめんなさい、今リバーシはするなと言われてます...', REVERSI_DECLINE: 'ごめんなさい、今リバーシはするなと言われてます...',
/** /**
* () * ()
*/ */
REBOOT_SCHEDULED: 'サーバーの空きメモリが少なくなってきたので、1分後にサーバー再起動しますね', REBOOT_SCHEDULED_MEM: 'サーバーの空きメモリが少なくなってきたので、1分後にサーバー再起動しますね',
/**
* (CPU使用率が高いので)
*/
REBOOT_SCHEDULED_CPU: 'サーバーの負荷が高いので、1分後にサーバー再起動しますね',
/** /**
* *