From c997091166ba1801eea3a587c913b020b9b84ce4 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 3 May 2017 23:18:13 +0200 Subject: Clean up redis configuration. Allow using REDIS_URL to set advanced (#2732) connection options instead of setting REDIS_HOST etc individually Close #1986 --- streaming/index.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'streaming') diff --git a/streaming/index.js b/streaming/index.js index 3be844037..18da58e49 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -16,23 +16,42 @@ dotenv.config({ path: env === 'production' ? '.env.production' : '.env' }) -if (cluster.isMaster) { - // cluster master +const dbUrlToConfig = (dbUrl) => { + if (!dbUrl) { + return {} + } + const params = url.parse(dbUrl) + const auth = params.auth ? params.auth.split(':') : [] + + return { + user: auth[0], + password: auth[1], + host: params.hostname, + port: params.port, + database: params.pathname ? params.pathname.split('/')[1] : null, + ssl: true + } +} + +if (cluster.isMaster) { + // Cluster master const core = +process.env.STREAMING_CLUSTER_NUM || (env === 'development' ? 1 : Math.max(os.cpus().length - 1, 1)) + const fork = () => { const worker = cluster.fork(); + worker.on('exit', (code, signal) => { log.error(`Worker died with exit code ${code}, signal ${signal} received.`); setTimeout(() => fork(), 0); }); }; + for (let i = 0; i < core; i++) fork(); - log.info(`Starting streaming API server master with ${core} workers`) + log.info(`Starting streaming API server master with ${core} workers`) } else { - // cluster worker - + // Cluster worker const pgConfigs = { development: { database: 'mastodon_development', @@ -51,14 +70,15 @@ if (cluster.isMaster) { } const app = express() - const pgPool = new pg.Pool(pgConfigs[env]) + const pgPool = new pg.Pool(Object.assign(dbUrlToConfig(process.env.DB_URL), pgConfigs[env])) const server = http.createServer(app) const wss = new WebSocket.Server({ server }) const redisClient = redis.createClient({ host: process.env.REDIS_HOST || '127.0.0.1', port: process.env.REDIS_PORT || 6379, - password: process.env.REDIS_PASSWORD + password: process.env.REDIS_PASSWORD, + url: process.env.REDIS_URL || null }) const subs = {} -- cgit