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 rebootTimer: NodeJS.Timer;
private rebootTimerSub: NodeJS.Timer;
private recentStat: any[] = [];
/**
* 11
*/
private statsLogs: any;
public install = (ai: ) => {
this.ai = ai;
@ -34,6 +40,31 @@ export default class ServerModule implements IModule {
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) => {
@ -50,22 +81,16 @@ export default class ServerModule implements IModule {
}
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();
}
this.recentStat = stats;
}
private scheduleReboot = () => {
private scheduleReboot = (reason: string) => {
if (this.preventScheduleReboot) return;
this.preventScheduleReboot = true;
this.ai.post({
text: serifs.REBOOT_SCHEDULED
text: reason == 'cpu' ? serifs.REBOOT_SCHEDULED_CPU : serifs.REBOOT_SCHEDULED_MEM
});
this.rebootTimer = setTimeout(() => {

View file

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