about summary refs log tree commit diff
path: root/streaming
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-05-03 23:18:13 +0200
committerGitHub <noreply@github.com>2017-05-03 23:18:13 +0200
commitc997091166ba1801eea3a587c913b020b9b84ce4 (patch)
treeed109c440d11133421f3b2187c71b1c4e0270c6e /streaming
parent005f1fd360ada23c6e35c6cc1fd06b7a4fe28c41 (diff)
Clean up redis configuration. Allow using REDIS_URL to set advanced (#2732)
connection options instead of setting REDIS_HOST etc individually

Close #1986
Diffstat (limited to 'streaming')
-rw-r--r--streaming/index.js34
1 files changed, 27 insertions, 7 deletions
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 = {}