后台:服务器维护
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
|
||||
import { scheduleJob, Job, } from 'node-schedule';
|
||||
import { scheduleJob, Job, scheduledJobs, } from 'node-schedule';
|
||||
import { PVPConfigModel, PVPConfigType } from '../db/SystemConfig';
|
||||
import { nowSeconds, getTimeFun, getSeconds } from '../pubUtils/timeUtil';
|
||||
import { getTodayGuildActivity, gameData } from '../pubUtils/data';
|
||||
import { pvpSeasonEnd } from './pvpService';
|
||||
import { getAllOnlineRoles, getAllServers, delGuildActivityRank } from './redisService';
|
||||
import { GUILD_ACTIVITY_TYPE, REFRESH_TIME, SEND_NAME, SERVER_OPEN_TIME, COUNTER, AUCTION_TIME, GM_MAIL_TYPE } from '../consts';
|
||||
import { GUILD_ACTIVITY_TYPE, REFRESH_TIME, SEND_NAME, SERVER_OPEN_TIME, COUNTER, AUCTION_TIME, GM_MAIL_TYPE, SERVER_STATUS } from '../consts';
|
||||
import { RoleModel } from '../db/Role';
|
||||
import { pinus } from 'pinus';
|
||||
import { indexOf } from 'underscore';
|
||||
@@ -19,13 +19,16 @@ import { dispatch } from '../pubUtils/dispatcher';
|
||||
import { Rank } from './rankService';
|
||||
import { checkTask } from './taskService';
|
||||
import { everydayRefresh } from './connectorService';
|
||||
import { initMarquee, initMaintenance } from './gmService';
|
||||
import { initMarquee } from './gmService';
|
||||
import moment = require('moment');
|
||||
import { CounterModel } from '../db/Counter';
|
||||
import { reportOneOnline } from './authenticateService';
|
||||
import { PVP } from '../pubUtils/dicParam';
|
||||
import { fetch37Words } from './sdkService';
|
||||
import { GMMailModel } from '../db/GMMail';
|
||||
import { Maintenance, ServerlistModel, ServerlistType, ServerlistUpdate } from '../db/Serverlist';
|
||||
import { getWorldChannelSid } from './chatService';
|
||||
import { createMarqueeMsg, pushMarqueeMsg } from './sysChatService';
|
||||
|
||||
const PER_SECOND = 1 * 1000;
|
||||
const PER_DAY = 24 * 60 * 60;
|
||||
@@ -396,4 +399,132 @@ async function sendCircleMail() {
|
||||
await f.sendToServer(serverIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*** 维护 */
|
||||
|
||||
let maintenInfos = new Map<string, { servers: ServerlistType[], maintenance: Maintenance }>(); // batchCode => {servers, maintenance}
|
||||
export async function initMaintenance(servers?: ServerlistType[]) {
|
||||
if(!servers) servers = await ServerlistModel.findByEnv(pinus.app.get('env'));
|
||||
|
||||
console.log('#### initMaintenance', servers)
|
||||
|
||||
for(let server of servers) {
|
||||
let { maintenance } = server;
|
||||
if(maintenance && maintenance.isOpen) {
|
||||
if(!maintenInfos.has(maintenance.batchCode)) {
|
||||
maintenInfos.set(maintenance.batchCode, { servers: [], maintenance });
|
||||
}
|
||||
maintenInfos.get(maintenance.batchCode).servers.push(server);
|
||||
}
|
||||
}
|
||||
for(let [batchCode] of maintenInfos) {
|
||||
await setMaintenance(batchCode);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置维护
|
||||
async function setMaintenance(batchCode: string) {
|
||||
let { maintenance } = maintenInfos.get(batchCode);
|
||||
let now = nowSeconds();
|
||||
// 发送消息
|
||||
if(maintenance.hasNotify && now < maintenance.startTime) {
|
||||
if(now > maintenance.startTime - 5 * 60) {
|
||||
maintenanceNotifySchedule(batchCode);
|
||||
} else {
|
||||
scheduleJob(`maintenNotify${batchCode}`, maintenance.startTime - 5 * 60, () => {
|
||||
maintenanceNotifySchedule(batchCode);
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if(scheduledJobs[`maintenNotify${batchCode}`]) scheduledJobs[`maintenNotify${batchCode}`].cancel();
|
||||
}
|
||||
// 开始维护
|
||||
console.log('******* setMaintenance', now, now < maintenance.endTime, now > maintenance.startTime)
|
||||
if(now < maintenance.endTime) {
|
||||
if(now > maintenance.startTime) {
|
||||
console.log('*******')
|
||||
await startMaintenanceSchedule(batchCode);
|
||||
} else {
|
||||
scheduleJob(`startMainten${batchCode}`, maintenance.startTime * 1000, async () => {
|
||||
await startMaintenanceSchedule(batchCode);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if(scheduledJobs[`startMainten${batchCode}`]) scheduledJobs[`startMainten${batchCode}`].cancel();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 维护前通知
|
||||
async function maintenanceNotifySchedule(batchCode: string) {
|
||||
let { servers, maintenance } = maintenInfos.get(batchCode);
|
||||
if(scheduledJobs[`maintenNotify${batchCode}`]) scheduledJobs[`maintenNotify${batchCode}`].cancel();
|
||||
if(scheduledJobs[`maintenSec${batchCode}`]) scheduledJobs[`maintenSec${batchCode}`].cancel();
|
||||
|
||||
scheduleJob(`maintenSec${batchCode}`, `0 */1 * * * *`, async () => {
|
||||
for(let { id: serverId } of servers) {
|
||||
let msgData = await createMarqueeMsg('', '系统', serverId, '服务器即将维护,请玩家提前退出游戏');
|
||||
await pushMarqueeMsg(msgData);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 开始维护
|
||||
async function startMaintenanceSchedule(batchCode: string) {
|
||||
let { servers, maintenance } = maintenInfos.get(batchCode);
|
||||
let serverIds = servers.map(cur => cur.id);
|
||||
if(scheduledJobs[`maintenNotify${batchCode}`]) scheduledJobs[`maintenNotify${batchCode}`].cancel();
|
||||
if(scheduledJobs[`maintenSec${batchCode}`]) scheduledJobs[`maintenSec${batchCode}`].cancel();
|
||||
if(scheduledJobs[`startMainten${batchCode}`]) scheduledJobs[`startMainten${batchCode}`].cancel();
|
||||
|
||||
// 向全服发送
|
||||
for(let { id: serverId } of servers) {
|
||||
let chatSid = await getWorldChannelSid(serverId);
|
||||
if(chatSid) {
|
||||
await pinus.app.rpc.chat.chatRemote.sendServerMaintenance.toServer(chatSid, serverId);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新serverlist上的status
|
||||
await ServerlistModel.updateByServerIds(serverIds, { serverStatus: SERVER_STATUS.MAINTENANCE });
|
||||
|
||||
// 更新connectorRemote里面的维护服务器
|
||||
console.log('******** startMaintenanceSchedule', batchCode, serverIds, maintenance.startTime, maintenance.endTime)
|
||||
pinus.app.rpc.connector.connectorRemote.setServerMainten.broadcast(serverIds, maintenance.startTime, maintenance.endTime);
|
||||
pinus.app.rpc.activity.activityRemote.setServerMainten.broadcast(serverIds, maintenance.startTime, maintenance.endTime);
|
||||
pinus.app.rpc.battle.battleRemote.setServerMainten.broadcast(serverIds, maintenance.startTime, maintenance.endTime);
|
||||
pinus.app.rpc.chat.chatRemote.setServerMainten.broadcast(serverIds, maintenance.startTime, maintenance.endTime);
|
||||
pinus.app.rpc.guild.guildRemote.setServerMainten.broadcast(serverIds, maintenance.startTime, maintenance.endTime);
|
||||
pinus.app.rpc.order.orderRemote.setServerMainten.broadcast(serverIds, maintenance.startTime, maintenance.endTime);
|
||||
pinus.app.rpc.role.roleRemote.setServerMainten.broadcast(serverIds, maintenance.startTime, maintenance.endTime);
|
||||
}
|
||||
|
||||
// 提前结束维护
|
||||
export async function stopMaintenance(batchCode: string, serverIds: number[]) {
|
||||
// console.log('***********', serverIds)
|
||||
let { servers = [], maintenance = {} as Maintenance } = maintenInfos.get(batchCode)||{};
|
||||
for(let id of serverIds) {
|
||||
let index = servers.findIndex(cur => cur.id == id);
|
||||
if(index != -1) servers.splice(index);
|
||||
}
|
||||
if(servers.length == 0) {
|
||||
if(scheduledJobs[`maintenNotify${batchCode}`]) scheduledJobs[`maintenNotify${batchCode}`].cancel();
|
||||
if(scheduledJobs[`maintenSec${batchCode}`]) scheduledJobs[`maintenSec${batchCode}`].cancel();
|
||||
if(scheduledJobs[`startMainten${batchCode}`]) scheduledJobs[`startMainten${batchCode}`].cancel();
|
||||
maintenInfos.delete(batchCode);
|
||||
}
|
||||
|
||||
// 更新serverlist上的status
|
||||
await ServerlistModel.updateByServerIds(serverIds, { serverStatus: SERVER_STATUS.HOT, 'maintenance.isOpen': false } as ServerlistUpdate);
|
||||
|
||||
// 更新connectorRemote里面的维护服务器
|
||||
pinus.app.rpc.connector.connectorRemote.stopServerMainten.broadcast(serverIds);
|
||||
pinus.app.rpc.activity.activityRemote.stopServerMainten.broadcast(serverIds);
|
||||
pinus.app.rpc.battle.battleRemote.stopServerMainten.broadcast(serverIds);
|
||||
pinus.app.rpc.chat.chatRemote.stopServerMainten.broadcast(serverIds);
|
||||
pinus.app.rpc.guild.guildRemote.stopServerMainten.broadcast(serverIds);
|
||||
pinus.app.rpc.order.orderRemote.stopServerMainten.broadcast(serverIds);
|
||||
pinus.app.rpc.role.roleRemote.stopServerMainten.broadcast(serverIds);
|
||||
}
|
||||
Reference in New Issue
Block a user