fix:
修复定时器重复执行导致邮件重复发送问题
This commit is contained in:
parent
dacd312bfe
commit
23e1941812
@ -2,13 +2,14 @@ import {RedisClientType, createClient} from "redis";
|
||||
import {redisClient} from "./setRedis";
|
||||
import {ConnectionStatus, WsServer} from "tsrpc";
|
||||
import cluster from 'cluster';
|
||||
import { clusterFun } from "./clusterFunction";
|
||||
import {clusterFun} from "./clusterFunction";
|
||||
|
||||
//维护当前uid和pid的对应关系
|
||||
let uid2processId = {};
|
||||
//订阅redis
|
||||
let subscribeRedis: RedisClientType
|
||||
let firstPid = null;
|
||||
|
||||
//发布和可写redis复用原 setRedis 里的redisClient
|
||||
|
||||
|
||||
@ -20,10 +21,10 @@ let firstPid = null;
|
||||
*/
|
||||
export function clusterSubscribe(key: string, callback: Function) {
|
||||
subscribeRedis.subscribe(G.redis.fromatKey(key), function (msg) {
|
||||
try{
|
||||
try {
|
||||
callback(msg);
|
||||
}catch(e){
|
||||
console.error("clusterSubscribe error",e);
|
||||
} catch (e) {
|
||||
console.error("clusterSubscribe error", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -43,14 +44,19 @@ export function clusterPublish(key: string, data: any) {
|
||||
* 在集群的N个进程中,只运行一次,在业务逻辑中也可使用
|
||||
*/
|
||||
export function clusterRunOnce(fun) {
|
||||
console.log(`${process.pid}环境变量pm_id===>${process.env.pm_id}`);
|
||||
if (process.env.pm_id == null || process.env.pm_id === '0') {
|
||||
//非pm2启动的,或是pm2下启动的第一个进程
|
||||
console.log("run clusterRunOnce1 ===>", process.pid)
|
||||
fun();
|
||||
return;
|
||||
}
|
||||
|
||||
if(firstPid == process.pid){
|
||||
if(G.config.isG123)return;
|
||||
|
||||
if (firstPid == process.pid) {
|
||||
//pm2的其中一个进程
|
||||
console.log("run clusterRunOnce2 ===>", process.pid)
|
||||
fun();
|
||||
return;
|
||||
}
|
||||
@ -60,16 +66,16 @@ export function clusterRunOnce(fun) {
|
||||
* 对外方法:
|
||||
* 在uid所在的进程执行fun方法,如果uid不在任何进程的话,则在当前进程执行
|
||||
*/
|
||||
export function clusterFunctionRunAtUidProcess(uid:string, fun:string, ...arg:any[]) {
|
||||
if(!uid2processId[uid] || uid2processId[uid] == process.pid){
|
||||
clusterFun[ fun ].call(this, ...arg);
|
||||
export function clusterFunctionRunAtUidProcess(uid: string, fun: string, ...arg: any[]) {
|
||||
if (!uid2processId[uid] || uid2processId[uid] == process.pid) {
|
||||
clusterFun[fun].call(this, ...arg);
|
||||
return;
|
||||
}
|
||||
|
||||
clusterPublish("RunclusterFunction", JSON.stringify({
|
||||
"uid": uid,
|
||||
"fun": fun,
|
||||
"arg" : arg
|
||||
"arg": arg
|
||||
}))
|
||||
}
|
||||
|
||||
@ -159,17 +165,17 @@ async function initSubscribeRedis() {
|
||||
clusterSubscribe('broadcastClusterMsg', function (msg) {
|
||||
let data = JSON.parse(msg);
|
||||
|
||||
if(!data.filter){
|
||||
if (!data.filter) {
|
||||
//如果不是有条件发送的话
|
||||
G.server.broadcastMsg(data.msgName, data.msg);
|
||||
}else {
|
||||
} else {
|
||||
//但是如果是有条件发送的话
|
||||
//条件里可能需要进程内的信息,则需要每个进程都分别筛选后执行
|
||||
if (data.filter.ghId != null) {
|
||||
//指定公会
|
||||
let conns = G.server.connections;
|
||||
conns = conns.filter(c => c?.gud?.ghId == data.filter.ghId);
|
||||
conns.length>0 && G.server.broadcastMsg(data.msgName, data.msg, conns);
|
||||
conns.length > 0 && G.server.broadcastMsg(data.msgName, data.msg, conns);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -178,7 +184,7 @@ async function initSubscribeRedis() {
|
||||
clusterSubscribe('RunclusterFunction', function (msg) {
|
||||
let data = JSON.parse(msg);
|
||||
if (uid2processId[data.uid] == process.pid) {
|
||||
clusterFun[ data.fun ].call(this, ...data.arg);
|
||||
clusterFun[data.fun].call(this, ...data.arg);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -193,8 +199,8 @@ export async function clusterMain() {
|
||||
});
|
||||
//初始化订阅redis
|
||||
await initSubscribeRedis();
|
||||
|
||||
if(process.env.pm_id != null){
|
||||
|
||||
if (process.env.pm_id != null) {
|
||||
//pm2启动的,设置key为我的pid
|
||||
firstPid = await redisClient.get(G.redis.fromatKey("firstPid"));
|
||||
if (!firstPid) {
|
||||
@ -207,7 +213,7 @@ export async function clusterMain() {
|
||||
redisClient.del(G.redis.fromatKey("uid2processId"));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
//初始化玩家pid信息
|
||||
await initUid2processId();
|
||||
|
Loading…
Reference in New Issue
Block a user