From 0293ad3c93d0b03a7ae352dc86c7db722e95b52e Mon Sep 17 00:00:00 2001 From: suwings Date: Sat, 2 Jun 2018 21:00:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20-=20=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 2 ++ helper/Schedule.js | 33 ++++++++++++++++++++++++++++----- route/websocket/schedule.js | 9 ++++++++- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index b130801..6b2f402 100644 --- a/app.js +++ b/app.js @@ -186,6 +186,7 @@ process.on("uncaughtException", function (err) { const USERS_PATH = './users/'; const SERVER_PATH = './server/'; const SERVER_PATH_CORE = './server/server_core/'; + const SERVER_PATH_SCH = './server/schedule/'; const CENTEN_LOG_JSON_PATH = './core/info.json'; const PUBLIC_URL_PATH = './public/common/URL.js'; @@ -193,6 +194,7 @@ process.on("uncaughtException", function (err) { if (!fs.existsSync(USERS_PATH)) fs.mkdirSync(USERS_PATH); if (!fs.existsSync(SERVER_PATH)) { fs.mkdir(SERVER_PATH, () => fs.mkdirSync(SERVER_PATH_CORE)); + fs.mkdirSync(SERVER_PATH_SCH); } // 生成不 git 同步的文件 diff --git a/helper/Schedule.js b/helper/Schedule.js index 7293eac..c61fdb0 100644 --- a/helper/Schedule.js +++ b/helper/Schedule.js @@ -1,18 +1,41 @@ const schedule = require("node-schedule"); -const DataModel = require('../core/DataModel'); const fs = require('fs'); -const PATH = 'server/_ schedule _.js'; +const DataModel = require('../core/DataModel'); +const serverModel = require('../model/ServerModel'); + +const PATH = 'server/schedule/_ schedule _.js'; MCSERVER.Schedule = {}; MCSERVER.Schedule.container = {}; MCSERVER.Schedule.dataModel = new DataModel(PATH) MCSERVER.Schedule.dataModel.list = []; -function serverExe(commande) { - console.log("执行:" + commande) +function serverExe(servername, commande) { + MCSERVER.log("[ Schedule ] [", servername, "] 服务器计划执行: ", commande); + try { + if (commande == "__start__") { + serverModel.startServer(servername); + } + if (commande == "__stop__") { + serverModel.stopServer(servername); + } + if (commande == "__restart__") { + serverModel.stopServer(servername); + setTimeout(() => { + serverModel.startServer(servername); + }, 15000) + } + //默认执行命令 + serverModel.sendCommand(servername, commande); + } catch (err) { + //默认忽略定时计划任务错误 + MCSERVER.log("[ Schedule ] [", servername, "] 服务器计划执行时保存 | 已忽略"); + } + } +//计划任务模块初始化 module.exports.init = () => { try { MCSERVER.Schedule.dataModel.load(); @@ -36,7 +59,7 @@ function createScheduleJobCount(id, time, count, commande, servername, callback, return; } lco++; - serverExe(commande); + serverExe(servername, commande); callback && callback(commande); }); diff --git a/route/websocket/schedule.js b/route/websocket/schedule.js index f85a348..fd5eaf0 100644 --- a/route/websocket/schedule.js +++ b/route/websocket/schedule.js @@ -20,13 +20,20 @@ function CreateScheduleJob(obj) { WebSocketObserver().listener('schedule/list', (data) => { let username = data.WsSession.username; let servername = data.body; + let list = MCSERVER.Schedule.dataModel.list; + sendlist = []; + for (const iterator of list) { + if (iterator && iterator.servername == servername) { + sendlist.push(iterator); + } + } if (permssion.isCanServer(username, servername)) { let thisServer = serverModel.ServerManager().getServer(servername); response.wsSend(data.ws, 'schedule/list', { username: data.WsSession.username, servername: servername, - schedules: MCSERVER.Schedule.dataModel.list + schedules: sendlist }); } });