adding redis unix connection
authorRigel Kent <sendmemail@rigelk.eu>
Mon, 14 May 2018 15:51:15 +0000 (17:51 +0200)
committerRigel Kent <par@rigelk.eu>
Sat, 14 Jul 2018 13:00:56 +0000 (15:00 +0200)
config/default.yaml
config/production.yaml.example
server/initializers/checker.ts
server/initializers/constants.ts
server/lib/job-queue/job-queue.ts
server/lib/redis.ts

index 88a2f2aab38493ff83d98bffcef420a22ce4553a..9a9b5833f7f4d8d8f66afc16567173bacfa50da1 100644 (file)
@@ -23,6 +23,8 @@ database:
   username: 'peertube'
   password: 'peertube'
 
+# You can also specify a 'socket' path to a unix socket but first need to
+# comment out hostname and port
 redis:
   hostname: 'localhost'
   port: 6379
index ac5e2a7391c531ac6c21c830ce43df8b3cfcb6ff..a4c80b1f1b6b32052708248d3a59da9c4f8f3ae8 100644 (file)
@@ -23,6 +23,8 @@ database:
   password: 'peertube'
 
 # Redis server for short time storage
+# You can also specify a 'socket' path to a unix socket but first need to
+# comment out hostname and port
 redis:
   hostname: 'localhost'
   port: 6379
@@ -124,4 +126,4 @@ services:
     # If true, a video player will be embedded in the Twitter feed on PeerTube video share
     # If false, we use an image link card that will redirect on your PeerTube instance
     # Test on https://cards-dev.twitter.com/validator to see if you are whitelisted
-    whitelisted: false
\ No newline at end of file
+    whitelisted: false
index d5402098f309f92762bf59c42242c6e243baed15..52a1aeb50ba842e45fdcc0083fc452d3d6a3283a 100644 (file)
@@ -44,7 +44,6 @@ function checkMissedConfig () {
     'webserver.https', 'webserver.hostname', 'webserver.port',
     'trust_proxy',
     'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password',
-    'redis.hostname', 'redis.port', 'redis.auth', 'redis.db',
     'smtp.hostname', 'smtp.port', 'smtp.username', 'smtp.password', 'smtp.tls', 'smtp.from_address',
     'storage.avatars', 'storage.videos', 'storage.logs', 'storage.previews', 'storage.thumbnails', 'storage.torrents', 'storage.cache',
     'log.level',
@@ -56,6 +55,12 @@ function checkMissedConfig () {
     'instance.default_nsfw_policy', 'instance.robots',
     'services.twitter.username', 'services.twitter.whitelisted'
   ]
+  const requiredAlternatives = [
+    [ // set
+      ['redis.hostname', 'redis.port'], // alternative
+      ['redis.socket']
+    ]
+  ]
   const miss: string[] = []
 
   for (const key of required) {
@@ -64,6 +69,13 @@ function checkMissedConfig () {
     }
   }
 
+  const missingAlternatives = requiredAlternatives.filter(
+    set => !set.find(alternative => !alternative.find(key => !config.has(key)))
+  )
+
+  missingAlternatives
+    .forEach(set => set[0].forEach(key => miss.push(key)))
+
   return miss
 }
 
index 6173e1298d6afd17b7b5690dfc10c07a1dace0fc..c5bc886d8d498fb553540d15cb51884ea5117221 100644 (file)
@@ -116,10 +116,11 @@ const CONFIG = {
     PASSWORD: config.get<string>('database.password')
   },
   REDIS: {
-    HOSTNAME: config.get<string>('redis.hostname'),
-    PORT: config.get<number>('redis.port'),
-    AUTH: config.get<string>('redis.auth'),
-    DB: config.get<number>('redis.db')
+    HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
+    PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
+    SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
+    AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
+    DB: config.has('redis.db') ? config.get<number>('redis.db') : null
   },
   SMTP: {
     HOSTNAME: config.get<string>('smtp.hostname'),
index 77aaa7fa8a55d6fcb4398aaee80cba6a51463a5f..1b46180e8eea3cea5972e663b97b5e22897bb014 100644 (file)
@@ -1,6 +1,7 @@
 import * as Bull from 'bull'
 import { JobState, JobType } from '../../../shared/models'
 import { logger } from '../../helpers/logger'
+import { Redis } from '../redis'
 import { CONFIG, JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_REQUEST_TTL } from '../../initializers'
 import { ActivitypubHttpBroadcastPayload, processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast'
 import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher'
@@ -63,12 +64,7 @@ class JobQueue {
     this.jobRedisPrefix = 'bull-' + CONFIG.WEBSERVER.HOST
     const queueOptions = {
       prefix: this.jobRedisPrefix,
-      redis: {
-        host: CONFIG.REDIS.HOSTNAME,
-        port: CONFIG.REDIS.PORT,
-        auth: CONFIG.REDIS.AUTH,
-        db: CONFIG.REDIS.DB
-      }
+      redis: Redis.getRedisClient()
     }
 
     for (const handlerName of Object.keys(handlers)) {
index 78b28986a0b4955f65286f90231fdf1b2156132f..06a34006012f5e0e6ae96a2e56ff23e16d1af5f9 100644 (file)
@@ -24,11 +24,7 @@ class Redis {
     if (this.initialized === true) return
     this.initialized = true
 
-    this.client = createClient({
-      host: CONFIG.REDIS.HOSTNAME,
-      port: CONFIG.REDIS.PORT,
-      db: CONFIG.REDIS.DB
-    })
+    this.client = createClient(Redis.getRedisClient())
 
     this.client.on('error', err => {
       logger.error('Error in Redis client.', { err })
@@ -42,6 +38,16 @@ class Redis {
     this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-'
   }
 
+  static getRedisClient () {
+    return Object.assign({},
+      (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
+      (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
+      (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) ?
+      { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } :
+      { path: CONFIG.REDIS.SOCKET }
+    )
+  }
+
   async setResetPasswordVerificationString (userId: number) {
     const generatedString = await generateRandomString(32)