0
0

Clean up redis configuration. Allow using REDIS_URL to set advanced (#2732)

connection options instead of setting REDIS_HOST etc individually

Close #1986
This commit is contained in:
Eugen Rochko 2017-05-03 23:18:13 +02:00 committed by GitHub
parent 005f1fd360
commit c997091166
8 changed files with 53 additions and 61 deletions

View File

@ -1,7 +1,8 @@
# Service dependencies # Service dependencies
# You may set REDIS_URL instead for more advanced options
REDIS_HOST=redis REDIS_HOST=redis
REDIS_PORT=6379 REDIS_PORT=6379
# REDIS_DB=0 # You may set DB_URL instead for more advanced options
DB_HOST=db DB_HOST=db
DB_USER=postgres DB_USER=postgres
DB_NAME=postgres DB_NAME=postgres

View File

@ -1,10 +0,0 @@
development:
adapter: redis
url: redis://localhost:6379/1
test:
adapter: async
production:
adapter: redis
url: redis://<%= ENV['REDIS_PASSWORD'] ? ':' + ENV['REDIS_PASSWORD'] + '@' : '' %><%= ENV['REDIS_HOST'] || 'localhost' %>:<%= ENV['REDIS_PORT'] || 6379 %>/1

View File

@ -16,14 +16,6 @@ Rails.application.configure do
if Rails.root.join('tmp/caching-dev.txt').exist? if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true config.action_controller.perform_caching = true
config.cache_store = :redis_store, {
host: ENV['REDIS_HOST'] || 'localhost',
port: ENV['REDIS_PORT'] || 6379,
db: 0,
namespace: 'cache',
expires_in: 1.minute,
}
config.public_file_server.headers = { config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800', 'Cache-Control' => 'public, max-age=172800',
} }

View File

@ -45,27 +45,6 @@ Rails.application.configure do
# Use a different logger for distributed setups. # Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Parse and split the REDIS_URL if passed (used with hosting platforms such as Heroku).
# Set ENV variables because they are used elsewhere.
if ENV['REDIS_URL']
redis_url = URI.parse(ENV['REDIS_URL'])
ENV['REDIS_HOST'] = redis_url.host
ENV['REDIS_PORT'] = redis_url.port.to_s
ENV['REDIS_PASSWORD'] = redis_url.password
db_num = redis_url.path[1..-1]
ENV['REDIS_DB'] = db_num if db_num.present?
end
# Use a different cache store in production.
config.cache_store = :redis_store, {
host: ENV.fetch('REDIS_HOST') { 'localhost' },
port: ENV.fetch('REDIS_PORT') { 6379 },
password: ENV.fetch('REDIS_PASSWORD') { false },
db: ENV.fetch('REDIS_DB') { 0 },
namespace: 'cache',
expires_in: 10.minutes,
}
# Enable serving of images, stylesheets, and JavaScripts from an asset server. # Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = 'http://assets.example.com' # config.action_controller.asset_host = 'http://assets.example.com'

View File

@ -1,9 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
port = ENV.fetch('PORT') { 3000 } port = ENV.fetch('PORT') { 3000 }
host = ENV.fetch('LOCAL_DOMAIN') { "localhost:#{port}" } host = ENV.fetch('LOCAL_DOMAIN') { "localhost:#{port}" }
web_host = ENV.fetch('WEB_DOMAIN') { host } web_host = ENV.fetch('WEB_DOMAIN') { host }
https = ENV['LOCAL_HTTPS'] == 'true' https = ENV['LOCAL_HTTPS'] == 'true'
Rails.application.configure do Rails.application.configure do
config.x.local_domain = host config.x.local_domain = host
@ -15,7 +15,6 @@ Rails.application.configure do
config.x.streaming_api_base_url = 'http://localhost:4000' config.x.streaming_api_base_url = 'http://localhost:4000'
if Rails.env.production? if Rails.env.production?
config.action_cable.allowed_request_origins = ["http#{https ? 's' : ''}://#{web_host}"] config.x.streaming_api_base_url = ENV.fetch('STREAMING_API_BASE_URL') { "http#{https ? 's' : ''}://#{web_host}" }
config.x.streaming_api_base_url = ENV.fetch('STREAMING_API_BASE_URL') { "http#{https ? 's' : ''}://#{web_host}" }
end end
end end

View File

@ -1,8 +1,22 @@
# frozen_string_literal: true # frozen_string_literal: true
if ENV['REDIS_URL'].blank?
password = ENV.fetch('REDIS_PASSWORD') { '' }
host = ENV.fetch('REDIS_HOST') { 'localhost' }
port = ENV.fetch('REDIS_PORT') { 6379 }
db = ENV.fetch('REDIS_DB') { 0 }
ENV['REDIS_URL'] = "redis://#{password.blank? ? '' : ":#{password}@"}#{host}:#{port}/#{db}"
end
Redis.current = Redis.new( Redis.current = Redis.new(
host: ENV.fetch('REDIS_HOST') { 'localhost' }, url: ENV['REDIS_URL'],
port: ENV.fetch('REDIS_PORT') { 6379 },
password: ENV.fetch('REDIS_PASSWORD') { false },
driver: :hiredis driver: :hiredis
) )
Rails.application.configure do
config.cache_store = :redis_store, ENV['REDIS_URL'], {
namespace: 'cache',
expires_in: 10.minutes,
}
end

View File

@ -1,12 +1,9 @@
host = ENV.fetch('REDIS_HOST') { 'localhost' } # frozen_string_literal: true
port = ENV.fetch('REDIS_PORT') { 6379 }
password = ENV.fetch('REDIS_PASSWORD') { false }
db = ENV.fetch('REDIS_DB') { 0 }
Sidekiq.configure_server do |config| Sidekiq.configure_server do |config|
config.redis = { host: host, port: port, db: db, password: password } config.redis = { url: ENV['REDIS_URL'] }
end end
Sidekiq.configure_client do |config| Sidekiq.configure_client do |config|
config.redis = { host: host, port: port, db: db, password: password } config.redis = { url: ENV['REDIS_URL'] }
end end

View File

@ -16,23 +16,42 @@ dotenv.config({
path: env === 'production' ? '.env.production' : '.env' path: env === 'production' ? '.env.production' : '.env'
}) })
if (cluster.isMaster) { const dbUrlToConfig = (dbUrl) => {
// cluster master 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 core = +process.env.STREAMING_CLUSTER_NUM || (env === 'development' ? 1 : Math.max(os.cpus().length - 1, 1))
const fork = () => { const fork = () => {
const worker = cluster.fork(); const worker = cluster.fork();
worker.on('exit', (code, signal) => { worker.on('exit', (code, signal) => {
log.error(`Worker died with exit code ${code}, signal ${signal} received.`); log.error(`Worker died with exit code ${code}, signal ${signal} received.`);
setTimeout(() => fork(), 0); setTimeout(() => fork(), 0);
}); });
}; };
for (let i = 0; i < core; i++) fork(); 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 { } else {
// cluster worker // Cluster worker
const pgConfigs = { const pgConfigs = {
development: { development: {
database: 'mastodon_development', database: 'mastodon_development',
@ -51,14 +70,15 @@ if (cluster.isMaster) {
} }
const app = express() 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 server = http.createServer(app)
const wss = new WebSocket.Server({ server }) const wss = new WebSocket.Server({ server })
const redisClient = redis.createClient({ const redisClient = redis.createClient({
host: process.env.REDIS_HOST || '127.0.0.1', host: process.env.REDIS_HOST || '127.0.0.1',
port: process.env.REDIS_PORT || 6379, port: process.env.REDIS_PORT || 6379,
password: process.env.REDIS_PASSWORD password: process.env.REDIS_PASSWORD,
url: process.env.REDIS_URL || null
}) })
const subs = {} const subs = {}