mirror of https://github.com/calzoneman/sync.git
Sync announcements across partitions
This commit is contained in:
parent
312892e56b
commit
edb5fb6f4e
|
@ -2,7 +2,7 @@
|
||||||
"author": "Calvin Montgomery",
|
"author": "Calvin Montgomery",
|
||||||
"name": "CyTube",
|
"name": "CyTube",
|
||||||
"description": "Online media synchronizer and chat",
|
"description": "Online media synchronizer and chat",
|
||||||
"version": "3.17.4",
|
"version": "3.17.5",
|
||||||
"repository": {
|
"repository": {
|
||||||
"url": "http://github.com/calzoneman/sync"
|
"url": "http://github.com/calzoneman/sync"
|
||||||
},
|
},
|
||||||
|
|
|
@ -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 logger from 'cytube-common/lib/logger';
|
||||||
import LegacyConfig from '../config';
|
import LegacyConfig from '../config';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import { AnnouncementRefresher } from './announcementrefresher';
|
||||||
|
|
||||||
const PARTITION_CONFIG_PATH = path.resolve(__dirname, '..', '..', 'conf',
|
const PARTITION_CONFIG_PATH = path.resolve(__dirname, '..', '..', 'conf',
|
||||||
'partitions.toml');
|
'partitions.toml');
|
||||||
|
@ -16,7 +17,7 @@ class PartitionModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
onReady() {
|
onReady() {
|
||||||
|
this.getAnnouncementRefresher();
|
||||||
}
|
}
|
||||||
|
|
||||||
initConfig() {
|
initConfig() {
|
||||||
|
@ -69,6 +70,18 @@ class PartitionModule {
|
||||||
|
|
||||||
return this.redisClientProvider;
|
return this.redisClientProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getAnnouncementRefresher() {
|
||||||
|
if (!this.announcementRefresher) {
|
||||||
|
const provider = this.getRedisClientProvider();
|
||||||
|
this.announcementRefresher = new AnnouncementRefresher(
|
||||||
|
provider.get(),
|
||||||
|
provider.get()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.announcementRefresher;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { PartitionModule };
|
export { PartitionModule };
|
||||||
|
|
|
@ -3,6 +3,7 @@ var singleton = null;
|
||||||
var Config = require("./config");
|
var Config = require("./config");
|
||||||
var Promise = require("bluebird");
|
var Promise = require("bluebird");
|
||||||
import * as ChannelStore from './channel-storage/channelstore';
|
import * as ChannelStore from './channel-storage/channelstore';
|
||||||
|
import { EventEmitter } from 'events';
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init: function () {
|
init: function () {
|
||||||
|
@ -163,6 +164,8 @@ var Server = function () {
|
||||||
initModule.onReady();
|
initModule.onReady();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Server.prototype = Object.create(EventEmitter.prototype);
|
||||||
|
|
||||||
Server.prototype.getHTTPIP = function (req) {
|
Server.prototype.getHTTPIP = function (req) {
|
||||||
var ip = req.ip;
|
var ip = req.ip;
|
||||||
if (ip === "127.0.0.1" || ip === "::1") {
|
if (ip === "127.0.0.1" || ip === "::1") {
|
||||||
|
@ -285,12 +288,22 @@ Server.prototype.packChannelList = function (publicOnly, isAdmin) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Server.prototype.announce = function (data) {
|
Server.prototype.announce = function (data) {
|
||||||
|
this.setAnnouncement(data);
|
||||||
|
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
this.announcement = null;
|
|
||||||
db.clearAnnouncement();
|
db.clearAnnouncement();
|
||||||
} else {
|
} else {
|
||||||
this.announcement = data;
|
|
||||||
db.setAnnouncement(data);
|
db.setAnnouncement(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit("announcement", data);
|
||||||
|
};
|
||||||
|
|
||||||
|
Server.prototype.setAnnouncement = function (data) {
|
||||||
|
if (data == null) {
|
||||||
|
this.announcement = null;
|
||||||
|
} else {
|
||||||
|
this.announcement = data;
|
||||||
sio.instance.emit("announcement", data);
|
sio.instance.emit("announcement", data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue