Sync announcements across partitions
This commit is contained in:
parent
312892e56b
commit
edb5fb6f4e
4 changed files with 91 additions and 4 deletions
61
src/partition/announcementrefresher.js
Normal file
61
src/partition/announcementrefresher.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import Logger from '../logger';
|
||||
import uuid from 'uuid';
|
||||
|
||||
var SERVER;
|
||||
const SERVER_ANNOUNCEMENTS = 'serverAnnouncements';
|
||||
|
||||
class AnnouncementRefresher {
|
||||
constructor(pubClient, subClient) {
|
||||
this.pubClient = pubClient;
|
||||
this.subClient = subClient;
|
||||
this.uuid = uuid.v4();
|
||||
process.nextTick(this.init.bind(this));
|
||||
}
|
||||
|
||||
init() {
|
||||
SERVER = require('../server').getServer();
|
||||
SERVER.on('announcement', this.sendAnnouncement.bind(this));
|
||||
|
||||
this.subClient.once('ready', () => {
|
||||
this.subClient.on('message', this.handleMessage.bind(this));
|
||||
this.subClient.subscribe(SERVER_ANNOUNCEMENTS);
|
||||
});
|
||||
}
|
||||
|
||||
handleMessage(channel, message) {
|
||||
if (channel !== SERVER_ANNOUNCEMENTS) {
|
||||
return;
|
||||
}
|
||||
|
||||
var data;
|
||||
try {
|
||||
data = JSON.parse(message);
|
||||
} catch (error) {
|
||||
Logger.errlog.log('Unable to unmarshal server announcement: ' + error.stack
|
||||
+ '\nMessage was: ' + message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.partitionID === this.uuid) {
|
||||
return;
|
||||
}
|
||||
|
||||
SERVER.setAnnouncement({
|
||||
title: data.title,
|
||||
text: data.text,
|
||||
from: data.from
|
||||
});
|
||||
}
|
||||
|
||||
sendAnnouncement(data) {
|
||||
const message = JSON.stringify({
|
||||
title: data.title,
|
||||
text: data.text,
|
||||
from: data.from,
|
||||
partitionID: this.uuid
|
||||
});
|
||||
this.pubClient.publish(SERVER_ANNOUNCEMENTS, message);
|
||||
}
|
||||
}
|
||||
|
||||
export { AnnouncementRefresher };
|
||||
|
|
@ -6,6 +6,7 @@ import RedisClientProvider from 'cytube-common/lib/redis/redisclientprovider';
|
|||
import logger from 'cytube-common/lib/logger';
|
||||
import LegacyConfig from '../config';
|
||||
import path from 'path';
|
||||
import { AnnouncementRefresher } from './announcementrefresher';
|
||||
|
||||
const PARTITION_CONFIG_PATH = path.resolve(__dirname, '..', '..', 'conf',
|
||||
'partitions.toml');
|
||||
|
|
@ -16,7 +17,7 @@ class PartitionModule {
|
|||
}
|
||||
|
||||
onReady() {
|
||||
|
||||
this.getAnnouncementRefresher();
|
||||
}
|
||||
|
||||
initConfig() {
|
||||
|
|
@ -69,6 +70,18 @@ class PartitionModule {
|
|||
|
||||
return this.redisClientProvider;
|
||||
}
|
||||
|
||||
getAnnouncementRefresher() {
|
||||
if (!this.announcementRefresher) {
|
||||
const provider = this.getRedisClientProvider();
|
||||
this.announcementRefresher = new AnnouncementRefresher(
|
||||
provider.get(),
|
||||
provider.get()
|
||||
);
|
||||
}
|
||||
|
||||
return this.announcementRefresher;
|
||||
}
|
||||
}
|
||||
|
||||
export { PartitionModule };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue