2017-10-12 10:00:28 +08:00
|
|
|
/* global PowerQueue */
|
|
|
|
import Redis from 'redis';
|
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { EventEmitter2 } from 'eventemitter2';
|
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import Logger from './logger';
|
|
|
|
|
|
|
|
// Fake meetingId used for messages that have no meetingId
|
|
|
|
const NO_MEETING_ID = '_';
|
|
|
|
|
2019-04-06 06:32:21 +08:00
|
|
|
const makeEnvelope = (channel, eventName, header, body, routing) => {
|
2017-10-12 10:00:28 +08:00
|
|
|
const envelope = {
|
|
|
|
envelope: {
|
|
|
|
name: eventName,
|
2019-04-06 06:32:21 +08:00
|
|
|
routing: routing || {
|
2017-10-12 10:00:28 +08:00
|
|
|
sender: 'bbb-apps-akka',
|
|
|
|
// sender: 'html5-server', // TODO
|
|
|
|
},
|
2019-10-19 00:50:38 +08:00
|
|
|
timestamp: Date.now(),
|
2017-10-12 10:00:28 +08:00
|
|
|
},
|
|
|
|
core: {
|
|
|
|
header,
|
|
|
|
body,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return JSON.stringify(envelope);
|
|
|
|
};
|
|
|
|
|
|
|
|
const makeDebugger = enabled => (message) => {
|
|
|
|
if (!enabled) return;
|
2019-01-22 01:00:24 +08:00
|
|
|
Logger.debug(`REDIS: ${message}`);
|
2017-10-12 10:00:28 +08:00
|
|
|
};
|
|
|
|
|
2019-02-16 03:45:42 +08:00
|
|
|
class MeetingMessageQueue {
|
2019-04-13 03:55:25 +08:00
|
|
|
constructor(eventEmitter, asyncMessages = [], debug = () => { }) {
|
2017-10-12 10:00:28 +08:00
|
|
|
this.asyncMessages = asyncMessages;
|
|
|
|
this.emitter = eventEmitter;
|
|
|
|
this.queue = new PowerQueue();
|
|
|
|
this.debug = debug;
|
|
|
|
|
|
|
|
this.handleTask = this.handleTask.bind(this);
|
|
|
|
this.queue.taskHandler = this.handleTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleTask(data, next) {
|
|
|
|
const { channel } = data;
|
|
|
|
const { envelope } = data.parsedMessage;
|
|
|
|
const { header } = data.parsedMessage.core;
|
|
|
|
const { body } = data.parsedMessage.core;
|
2018-01-08 08:25:56 +08:00
|
|
|
const { meetingId } = header;
|
2017-10-12 10:00:28 +08:00
|
|
|
const eventName = header.name;
|
|
|
|
const isAsync = this.asyncMessages.includes(channel)
|
|
|
|
|| this.asyncMessages.includes(eventName);
|
|
|
|
|
|
|
|
let called = false;
|
|
|
|
|
|
|
|
check(eventName, String);
|
|
|
|
check(body, Object);
|
|
|
|
|
|
|
|
const callNext = () => {
|
|
|
|
if (called) return;
|
|
|
|
this.debug(`${eventName} completed ${isAsync ? 'async' : 'sync'}`);
|
|
|
|
called = true;
|
2018-04-03 04:16:46 +08:00
|
|
|
const queueLength = this.queue.length();
|
2018-06-15 03:01:49 +08:00
|
|
|
if (queueLength > 100) {
|
2018-04-03 04:16:46 +08:00
|
|
|
Logger.error(`prev queue size=${queueLength} `);
|
|
|
|
}
|
2017-10-12 10:00:28 +08:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onError = (reason) => {
|
2019-06-29 02:51:26 +08:00
|
|
|
Logger.error(`${eventName}: ${reason.stack ? reason.stack : reason}`);
|
2017-10-12 10:00:28 +08:00
|
|
|
callNext();
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2018-03-17 02:57:27 +08:00
|
|
|
this.debug(`${JSON.stringify(data.parsedMessage.core)} emitted`);
|
2017-10-12 10:00:28 +08:00
|
|
|
|
|
|
|
if (isAsync) {
|
|
|
|
callNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emitter
|
|
|
|
.emitAsync(eventName, { envelope, header, body }, meetingId)
|
|
|
|
.then(callNext)
|
|
|
|
.catch(onError);
|
|
|
|
} catch (reason) {
|
|
|
|
onError(reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
add(...args) {
|
|
|
|
return this.queue.add(...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 03:07:02 +08:00
|
|
|
class RedisPubSub {
|
2017-10-12 10:00:28 +08:00
|
|
|
static handlePublishError(err) {
|
|
|
|
if (err) {
|
|
|
|
Logger.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(config = {}) {
|
|
|
|
this.config = config;
|
|
|
|
|
|
|
|
this.didSendRequestEvent = false;
|
2019-04-13 03:55:25 +08:00
|
|
|
const host = process.env.REDIS_HOST || Meteor.settings.private.redis.host;
|
|
|
|
const redisConf = Meteor.settings.private.redis;
|
|
|
|
const { password, port } = redisConf;
|
|
|
|
|
2019-06-29 02:51:26 +08:00
|
|
|
if (password) {
|
2019-04-13 03:55:25 +08:00
|
|
|
this.pub = Redis.createClient({ host, port, password });
|
|
|
|
this.sub = Redis.createClient({ host, port, password });
|
|
|
|
this.pub.auth(password);
|
|
|
|
this.sub.auth(password);
|
|
|
|
} else {
|
|
|
|
this.pub = Redis.createClient({ host, port });
|
|
|
|
this.sub = Redis.createClient({ host, port });
|
2019-04-10 01:58:56 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 10:00:28 +08:00
|
|
|
this.emitter = new EventEmitter2();
|
|
|
|
this.mettingsQueues = {};
|
|
|
|
|
|
|
|
this.handleSubscribe = this.handleSubscribe.bind(this);
|
|
|
|
this.handleMessage = this.handleMessage.bind(this);
|
|
|
|
this.debug = makeDebugger(this.config.debug);
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.sub.on('psubscribe', Meteor.bindEnvironment(this.handleSubscribe));
|
|
|
|
this.sub.on('pmessage', Meteor.bindEnvironment(this.handleMessage));
|
|
|
|
|
|
|
|
const channelsToSubscribe = this.config.subscribeTo;
|
|
|
|
|
|
|
|
channelsToSubscribe.forEach((channel) => {
|
|
|
|
this.sub.psubscribe(channel);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.debug(`Subscribed to '${channelsToSubscribe}'`);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateConfig(config) {
|
|
|
|
this.config = Object.assign({}, this.config, config);
|
|
|
|
this.debug = makeDebugger(this.config.debug);
|
|
|
|
}
|
|
|
|
|
2018-04-24 21:59:13 +08:00
|
|
|
|
2017-10-12 10:00:28 +08:00
|
|
|
// TODO: Move this out of this class, maybe pass as a callback to init?
|
|
|
|
handleSubscribe() {
|
|
|
|
if (this.didSendRequestEvent) return;
|
|
|
|
|
|
|
|
// populate collections with pre-existing data
|
2018-01-08 08:24:05 +08:00
|
|
|
const REDIS_CONFIG = Meteor.settings.private.redis;
|
2017-10-12 10:00:28 +08:00
|
|
|
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
|
|
|
|
const EVENT_NAME = 'GetAllMeetingsReqMsg';
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
requesterId: 'nodeJSapp',
|
|
|
|
};
|
|
|
|
|
|
|
|
this.publishSystemMessage(CHANNEL, EVENT_NAME, body);
|
|
|
|
this.didSendRequestEvent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMessage(pattern, channel, message) {
|
|
|
|
const parsedMessage = JSON.parse(message);
|
|
|
|
const { name: eventName, meetingId } = parsedMessage.core.header;
|
|
|
|
const { ignored: ignoredMessages, async } = this.config;
|
|
|
|
|
|
|
|
if (ignoredMessages.includes(channel)
|
|
|
|
|| ignoredMessages.includes(eventName)) {
|
2018-10-17 01:48:27 +08:00
|
|
|
if (eventName === 'CheckAlivePongSysMsg') {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-12 10:00:28 +08:00
|
|
|
this.debug(`${eventName} skipped`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const queueId = meetingId || NO_MEETING_ID;
|
|
|
|
|
|
|
|
if (!(queueId in this.mettingsQueues)) {
|
2019-02-16 03:45:42 +08:00
|
|
|
this.mettingsQueues[meetingId] = new MeetingMessageQueue(this.emitter, async, this.debug);
|
2017-10-12 10:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.mettingsQueues[meetingId].add({
|
|
|
|
pattern,
|
|
|
|
channel,
|
|
|
|
eventName,
|
|
|
|
parsedMessage,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
destroyMeetingQueue(id) {
|
|
|
|
delete this.mettingsQueues[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
on(...args) {
|
|
|
|
return this.emitter.on(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
publishVoiceMessage(channel, eventName, voiceConf, payload) {
|
|
|
|
const header = {
|
|
|
|
name: eventName,
|
|
|
|
voiceConf,
|
|
|
|
};
|
|
|
|
|
|
|
|
const envelope = makeEnvelope(channel, eventName, header, payload);
|
|
|
|
|
2017-10-13 03:07:02 +08:00
|
|
|
return this.pub.publish(channel, envelope, RedisPubSub.handlePublishError);
|
2017-10-12 10:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
publishSystemMessage(channel, eventName, payload) {
|
|
|
|
const header = {
|
|
|
|
name: eventName,
|
|
|
|
};
|
|
|
|
|
|
|
|
const envelope = makeEnvelope(channel, eventName, header, payload);
|
|
|
|
|
2017-10-13 03:07:02 +08:00
|
|
|
return this.pub.publish(channel, envelope, RedisPubSub.handlePublishError);
|
2017-10-12 10:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
publishMeetingMessage(channel, eventName, meetingId, payload) {
|
|
|
|
const header = {
|
|
|
|
name: eventName,
|
|
|
|
meetingId,
|
|
|
|
};
|
|
|
|
|
|
|
|
const envelope = makeEnvelope(channel, eventName, header, payload);
|
|
|
|
|
2017-10-13 03:07:02 +08:00
|
|
|
return this.pub.publish(channel, envelope, RedisPubSub.handlePublishError);
|
2017-10-12 10:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
publishUserMessage(channel, eventName, meetingId, userId, payload) {
|
|
|
|
const header = {
|
|
|
|
name: eventName,
|
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
};
|
|
|
|
|
2019-04-06 06:32:21 +08:00
|
|
|
const envelope = makeEnvelope(channel, eventName, header, payload, { meetingId, userId });
|
2017-10-12 10:00:28 +08:00
|
|
|
|
2017-10-13 03:07:02 +08:00
|
|
|
return this.pub.publish(channel, envelope, RedisPubSub.handlePublishError);
|
2017-10-12 10:00:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 03:07:02 +08:00
|
|
|
const RedisPubSubSingleton = new RedisPubSub();
|
2017-10-12 10:00:28 +08:00
|
|
|
|
|
|
|
Meteor.startup(() => {
|
2018-01-08 08:24:05 +08:00
|
|
|
const REDIS_CONFIG = Meteor.settings.private.redis;
|
2017-10-12 10:00:28 +08:00
|
|
|
|
|
|
|
RedisPubSubSingleton.updateConfig(REDIS_CONFIG);
|
|
|
|
RedisPubSubSingleton.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
export default RedisPubSubSingleton;
|