MCSManager/route/websocket/schedule.js

86 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-06-02 14:05:21 +08:00
const {
WebSocketObserver
} = require('../../model/WebSocketModel');
const permssion = require('../../helper/Permission');
2018-06-02 15:04:20 +08:00
const response = require('../../helper/Response');
2018-06-02 14:05:21 +08:00
const schedulejob = require('../../helper/Schedule');
2018-06-02 14:16:18 +08:00
const tools = require('../../core/tools');
2018-06-02 14:05:21 +08:00
const serverModel = require('../../model/ServerModel');
2018-06-02 22:00:53 +08:00
const UUID = require('uuid');
//每个服务器最大数量计划任务
2018-06-02 21:42:50 +08:00
const MAX_MASK = MCSERVER.localProperty.schedule_max || 10;
2018-06-02 22:00:53 +08:00
//创建计划任务函数
2018-06-02 14:05:21 +08:00
function CreateScheduleJob(obj) {
let id = tools.randomString(6) + "_" + new Date().getTime();
let thisServer = serverModel.ServerManager().getServer(obj.servername);
2018-06-02 14:05:21 +08:00
schedulejob.createScheduleJobCount(id, obj.time, obj.count, obj.commande, obj.servername);
2018-06-02 14:05:21 +08:00
}
2018-06-02 22:00:53 +08:00
//过滤计划任务列表
2018-06-02 21:38:25 +08:00
function getMineScheduleList(servername) {
2018-06-02 21:00:21 +08:00
let list = MCSERVER.Schedule.dataModel.list;
sendlist = [];
for (const iterator of list) {
if (iterator && iterator.servername == servername) {
sendlist.push(iterator);
}
}
2018-06-02 21:38:25 +08:00
return sendlist;
}
//列出计划任务
WebSocketObserver().listener('schedule/list', (data) => {
let username = data.WsSession.username;
let servername = data.body;
let list = MCSERVER.Schedule.dataModel.list;
let sendlist = getMineScheduleList(servername);
2018-06-02 14:05:21 +08:00
if (permssion.isCanServer(username, servername)) {
let thisServer = serverModel.ServerManager().getServer(servername);
2018-06-02 15:04:20 +08:00
response.wsSend(data.ws, 'schedule/list', {
username: data.WsSession.username,
servername: servername,
2018-06-02 21:00:21 +08:00
schedules: sendlist
2018-06-02 15:04:20 +08:00
});
2018-06-02 14:05:21 +08:00
}
});
//创建计划任务
WebSocketObserver().listener('schedule/create', (data) => {
let username = data.WsSession.username;
2018-06-02 20:35:43 +08:00
let obj = JSON.parse(data.body) || {};
2018-06-02 14:05:21 +08:00
if (permssion.isCanServer(username, obj.servername || "")) {
2018-06-02 20:35:43 +08:00
try {
2018-06-02 21:38:25 +08:00
list = getMineScheduleList(obj.servername);
2018-06-02 21:42:50 +08:00
if (list.length > MAX_MASK) {
2018-06-02 21:38:25 +08:00
response.wsMsgWindow(data.ws, "到达创建数量上限!");
return;
}
2018-06-02 20:35:43 +08:00
CreateScheduleJob(obj);
response.wsMsgWindow(data.ws, "创建计划任务成功 √");
} catch (err) {
response.wsMsgWindow(data.ws, "错误!创建失败:" + err);
}
2018-06-02 14:05:21 +08:00
}
});
//删除计划任务
WebSocketObserver().listener('schedule/delete', (data) => {
let username = data.WsSession.username;
2018-06-02 20:35:43 +08:00
let obj = JSON.parse(data.body) || {};
2018-06-02 14:05:21 +08:00
if (permssion.isCanServer(username, obj.servername || "")) {
2018-06-02 20:35:43 +08:00
try {
schedulejob.deleteScheduleJob(obj.id || "");
response.wsMsgWindow(data.ws, "删除序号:" + obj.id + "计划任务");
} catch (err) {
response.wsMsgWindow(data.ws, "删除失败!" + err);
}
2018-06-02 14:05:21 +08:00
}
});