Listening to redis event for all core models
Change-Id: I183b688cf73b7f54aede5e2b9519f26a54d2790c
diff --git a/package.json b/package.json
index d4045bf..2249138 100644
--- a/package.json
+++ b/package.json
@@ -5,7 +5,7 @@
"main": "src/server.js",
"scripts": {
"start": "node src/server.js",
- "test": "mocha spec/**/*.spec.js",
+ "test": "LOG_LEVEL=error mocha spec/**/*.spec.js",
"test:dev": "mocha -w spec/**/*.spec.js",
"lint": "eslint ."
},
diff --git a/spec/redis.spec.js b/spec/redis.spec.js
index e607e5c..823a047 100644
--- a/spec/redis.spec.js
+++ b/spec/redis.spec.js
@@ -20,6 +20,19 @@
}
}
};
+
+ const mockRequest = {
+ get: () => {
+ return {
+ end: (fn) => {
+ fn(null, {body: [
+ {name: 'Slice'},
+ {name: 'Site'}
+ ]});
+ }
+ }
+ }
+ }
const channelName = 'Site';
describe('The event system', () => {
@@ -38,6 +51,9 @@
// Override the redis module with our fakeredis instance
mockery.registerMock('redis', fakeredis);
+ // Override the superagent module with our mockRequest instance
+ mockery.registerMock('superagent', mockRequest);
+
// mock the socketIo client to have a spy
mockery.registerMock('./websocket.js', mockSocket);
diff --git a/src/controllers/redis.js b/src/controllers/redis.js
index 0bcebe3..1ed8a20 100644
--- a/src/controllers/redis.js
+++ b/src/controllers/redis.js
@@ -2,14 +2,16 @@
'use strict';
const socketIo = require('./websocket.js');
- const config = require('../config/config.js').redis;
+ const config = require('../config/config.js');
const logger = require('../config/logger.js');
+ const request = require('superagent');
const socket = socketIo.get();
+ const _ = require('lodash');
var redis = require('redis');
var client = redis.createClient({
- host: config.host,
- port: config.port
+ host: config.redis.host,
+ port: config.redis.port
});
client.on('error', function (err) {
@@ -38,20 +40,21 @@
msg = message;
socket.emit('event', {model: channel, msg: msg});
}
-
});
- const watchedCollections = [
- 'Instance',
- 'Node',
- 'Service',
- 'Slice',
- 'Site',
- 'Subscriber',
- 'Tenant'
- ];
-
- watchedCollections.forEach(c => {
- client.subscribe(c);
+ // dynamically load Model names to listen on channels
+ // NOTE how to listen for models defined by services?
+ request.get(`${config.xos.host}:${config.xos.port}/api/utility/modeldefs`)
+ .end((err, res) => {
+ if (err) {
+ logger.log('error', err);
+ }
+ if (res) {
+ const models = _.map(res.body, i => i.name);
+ _.forEach(models, c => {
+ client.subscribe(c);
+ });
+ }
});
+
})();
\ No newline at end of file