Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis config sync #81

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions bin/bootstrapconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node

var
_ = require('underscore'),
mbc = require('../'),
backboneio = require('backbone.io'),
middleware = new mbc.iobackends().get_middleware(),
logger = mbc.logger().addLogger('config_bootstrap_bin')
;

var backends = {
app: {
use: [backboneio.middleware.configStore()],
redis: true,
},
};

var iobackends = new mbc.iobackends(undefined, backends);

var helper = new mbc.utils.bootstrapConfigHelper(backends.app);

helper.on('gotRedisAnswer', _.debounce(function() {
logger.info('config updating from peers done, exiting now.');
process.exit(0);
}, 1000, false));

helper.requestConfig();

_.delay(function() {
logger.info('nobody answered, exiting now.');
process.exit(1);
}, 10000);
13 changes: 13 additions & 0 deletions bin/touchconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node

var
mbc = require('../'),
logger = mbc.logger().addLogger('config_touch_bin'),
moment = require("moment"),
config = require("config")
;

config.timestamp = (new moment()).unix()
setTimeout(function() {
process.exit(0);
}, 1000);
2 changes: 1 addition & 1 deletion config/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var path = require('path'),
cwd = process.cwd();

module.exports = {
Hidden: ['Hidden', 'Search'],
Hidden: ['Hidden', 'Search', 'timestamp'],
Caspa: {
Branding: {
name: 'MBC Playout {mlt edition}',
Expand Down
2 changes: 1 addition & 1 deletion config/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var path = require('path'),
cwd = process.cwd();

module.exports = {
Hidden: ['Hidden', 'Search'],
Hidden: ['Hidden', 'Search', 'timestamp'],
Caspa: {
Branding: {
name: 'MBC Playout {mlt edition}',
Expand Down
3 changes: 2 additions & 1 deletion models/iobackends.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var iobackends = module.exports = exports = function (db, backends) {
var binded = [];
_(this.backends).each (function (backend, name) {
backend.io = backboneio.createBackend();
backend.io.name = name;
if (backend.use) {
_(backend.use).each (function (usefn) {
backend.io.use(usefn);
Expand All @@ -84,7 +85,7 @@ var iobackends = module.exports = exports = function (db, backends) {
* other servers and also broadcasts ours.
*/
if (backend.redis) {
backend.io.use (iocompat.redisMiddleware(backend, name, backend.redis.chain));
backend.io.use (iocompat.redisMiddleware(backend, name, backend.redis));
}

/*
Expand Down
11 changes: 8 additions & 3 deletions models/iocompat.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function inherits(Parent, Child, mixins) {
* listener, so we add a unike token to distinguish them.
*/
var _client_id = uuid.v4();
iocompat.client_id = _client_id;

/*
* redisMiddleware.
Expand All @@ -45,7 +46,7 @@ var _client_id = uuid.v4();
* 'redis' and 'redis:[create|update|delete]' events to update
* our models here in the server.
*/
iocompat.redisMiddleware = function (backend, name, chain) {
iocompat.redisMiddleware = function (backend, name, options) {
var _chan = '_RedisSync.' + name;
var io = backend.io;

Expand Down Expand Up @@ -98,16 +99,20 @@ iocompat.redisMiddleware = function (backend, name, chain) {
*/
function _middleware(req, res, next) {
if (req._redis_source) {
if (chain) {
if (options.chain) {
next();
} else {
res.end(req.model);
}
return;
}
if (req.method.match(/create|update|delete/)) {

if (req.method.match(/create|update|delete/) ||
(options.customMethods && options.customMethods.match('\b'+ req.method +'\b'))
) {
publisher.publishJSON(_chan, { model: req.model, method:req.method, _redis_source:_client_id});
}

next();
};

Expand Down
79 changes: 79 additions & 0 deletions utils/bootstrapconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var config = require("config"),
moment = require("moment"),
_ = require("underscore"),
Backbone = require("backbone"),
publisher = require('../pubsub')(),
logger = require("../logger")().addLogger('configBootstrap'),
iocompat = require("../models/iocompat")
;

exports = module.exports = bootstrapHelper;

function bootstrapHelper (configbackend) {
var self = this;
_.extend(this, Backbone.Events);
if (!configbackend) {
logger.error('bootstrapHelper: need to pass the config iobackend');
return;
}

if (config.timestamp == undefined) {
config.timestamp = 0;
}

this.backend = configbackend;
this.channel = '_RedisSync.' + configbackend.io.name;
this.client_id = iocompat.client_id;

_.bindAll(this, '_onRedisAnswer', '_onRedisRequest');

this.backend.io.on('redis:configBootstrapAnswer', this._onRedisAnswer);
this.backend.io.on('redis:configBootstrapRequest', this._onRedisRequest);

config.watch(config, null, this._onConfigChanged);
};

bootstrapHelper.prototype.requestConfig = function() {
var self = this;

setTimeout(function() {
self._onRedisRequest();
publisher.publishJSON(self.channel, { model: {}, method: 'configBootstrapRequest', _redis_source: self.client_id});
}, 500);
};

bootstrapHelper.prototype._onConfigChanged = _.debounce(function(object, propertyName, priorValue, newValue) {
if (propertyName == 'timestamp') {
return;
}
config.timestamp = (new moment()).unix()
}, 100);

bootstrapHelper.prototype._onRedisRequest = function(model) {
var pay = { model: config, method: 'configBootstrapAnswer', _redis_source: this.client_id};
publisher.publishJSON(this.channel, pay);
};

bootstrapHelper.prototype._onRedisAnswer = function(model) {
var self = this;
var timestamp = (config.timestamp == undefined) ? 0 : config.timestamp;

this.trigger('gotRedisAnswer');

function emitUpdate(mdl) {
var pay = []

pay.push( _.extend(mdl, {type: 'config'}) );
pay.push( _.extend(config.getOriginalConfig(), { type: 'defaults'}) );
pay.push( _.extend(config.getSchemaValidator(), { type: 'descriptions'}) );

self.backend.io.emit('updated', pay);
}

if ((model.timestamp != undefined) && (model.timestamp > timestamp)){
config._extendDeep(config, model);
config.timestamp = model.timestamp;
emitUpdate(config);
this.trigger('configUpdated');
}
};
4 changes: 2 additions & 2 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports = module.exports = {
heartbeats: require('./heartbeats'),
}

bootstrapConfigHelper: require('./bootstrapconfig.js'),
};