Automatically publish backend address to the pool

This commit is contained in:
calzoneman 2016-01-20 23:11:55 -08:00
parent 8bef7924b2
commit dd73a8ee9a
3 changed files with 29 additions and 3 deletions

View File

@ -31,12 +31,14 @@
"nodemailer": "^1.4.0", "nodemailer": "^1.4.0",
"oauth": "^0.9.12", "oauth": "^0.9.12",
"q": "^1.4.1", "q": "^1.4.1",
"redis": "^2.4.2",
"sanitize-html": "git://github.com/calzoneman/sanitize-html", "sanitize-html": "git://github.com/calzoneman/sanitize-html",
"serve-static": "^1.10.0", "serve-static": "^1.10.0",
"socket.io": "^1.3.7", "socket.io": "^1.3.7",
"socket.io-redis": "^1.0.0", "socket.io-redis": "^1.0.0",
"source-map-support": "^0.3.2", "source-map-support": "^0.3.2",
"status-message-polyfill": "calzoneman/status-message-polyfill", "status-message-polyfill": "calzoneman/status-message-polyfill",
"uuid": "^2.0.1",
"yamljs": "^0.1.6" "yamljs": "^0.1.6"
}, },
"scripts": { "scripts": {

View File

@ -1,12 +1,20 @@
import Server from 'cytube-common/lib/proxy/server'; import Server from 'cytube-common/lib/proxy/server';
import FrontendManager from './frontendmanager'; import FrontendManager from './frontendmanager';
import uuid from 'uuid';
import PoolEntryUpdater from 'cytube-common/lib/redis/poolentryupdater';
import JSONProtocol from 'cytube-common/lib/proxy/protocol';
const BACKEND_POOL = 'backend-hosts';
export default class IOBackend { export default class IOBackend {
constructor(proxyListenerConfig, socketEmitter) { constructor(proxyListenerConfig, socketEmitter, poolRedisClient) {
this.proxyListenerConfig = proxyListenerConfig; this.proxyListenerConfig = proxyListenerConfig;
this.socketEmitter = socketEmitter; this.socketEmitter = socketEmitter;
this.poolRedisClient = poolRedisClient;
this.protocol = new JSONProtocol();
this.initFrontendManager(); this.initFrontendManager();
this.initProxyListener(); this.initProxyListener();
this.initBackendPoolUpdater();
} }
initFrontendManager() { initFrontendManager() {
@ -14,8 +22,21 @@ export default class IOBackend {
} }
initProxyListener() { initProxyListener() {
this.proxyListener = new Server(this.proxyListenerConfig); this.proxyListener = new Server(this.proxyListenerConfig, this.protocol);
this.proxyListener.on('connection', this.proxyListener.on('connection',
this.frontendManager.onConnection.bind(this.frontendManager)); this.frontendManager.onConnection.bind(this.frontendManager));
} }
initBackendPoolUpdater() {
const entry = {
address: this.proxyListenerConfig.getHost() + '/' + this.proxyListenerConfig.getPort()
}
this.poolEntryUpdater = new PoolEntryUpdater(
this.poolRedisClient,
BACKEND_POOL,
uuid.v4(),
entry
);
this.poolEntryUpdater.start();
}
} }

View File

@ -140,7 +140,10 @@ var Server = function () {
return '127.0.0.1'; return '127.0.0.1';
} }
}; };
const backend = new IOBackend(listenerConfig, sioEmitter); const redis = require('redis');
Promise.promisifyAll(redis.RedisClient.prototype);
Promise.promisifyAll(redis.Multi.prototype);
const backend = new IOBackend(listenerConfig, sioEmitter, redis.createClient());
// background tasks init ---------------------------------------------- // background tasks init ----------------------------------------------
require("./bgtask")(self); require("./bgtask")(self);