From 19f7b248d88805e6595b671a7447b0ba5e1451fa Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Mon, 14 May 2018 17:51:15 +0200 Subject: [PATCH] adding redis unix connection --- config/default.yaml | 2 ++ config/production.yaml.example | 4 +++- server/initializers/checker.ts | 14 +++++++++++++- server/initializers/constants.ts | 9 +++++---- server/lib/job-queue/job-queue.ts | 8 ++------ server/lib/redis.ts | 16 +++++++++++----- 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/config/default.yaml b/config/default.yaml index 88a2f2aab..9a9b5833f 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -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 diff --git a/config/production.yaml.example b/config/production.yaml.example index ac5e2a739..a4c80b1f1 100644 --- a/config/production.yaml.example +++ b/config/production.yaml.example @@ -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 diff --git a/server/initializers/checker.ts b/server/initializers/checker.ts index d5402098f..52a1aeb50 100644 --- a/server/initializers/checker.ts +++ b/server/initializers/checker.ts @@ -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 } diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 6173e1298..c5bc886d8 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -116,10 +116,11 @@ const CONFIG = { PASSWORD: config.get('database.password') }, REDIS: { - HOSTNAME: config.get('redis.hostname'), - PORT: config.get('redis.port'), - AUTH: config.get('redis.auth'), - DB: config.get('redis.db') + HOSTNAME: config.has('redis.hostname') ? config.get('redis.hostname') : null, + PORT: config.has('redis.port') ? config.get('redis.port') : null, + SOCKET: config.has('redis.socket') ? config.get('redis.socket') : null, + AUTH: config.has('redis.auth') ? config.get('redis.auth') : null, + DB: config.has('redis.db') ? config.get('redis.db') : null }, SMTP: { HOSTNAME: config.get('smtp.hostname'), diff --git a/server/lib/job-queue/job-queue.ts b/server/lib/job-queue/job-queue.ts index 77aaa7fa8..1b46180e8 100644 --- a/server/lib/job-queue/job-queue.ts +++ b/server/lib/job-queue/job-queue.ts @@ -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)) { diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 78b28986a..06a340060 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -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) -- 2.25.1