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:
parent
005f1fd360
commit
c997091166
8 changed files with 53 additions and 61 deletions
|
@ -1,7 +1,8 @@
|
|||
# Service dependencies
|
||||
# You may set REDIS_URL instead for more advanced options
|
||||
REDIS_HOST=redis
|
||||
REDIS_PORT=6379
|
||||
# REDIS_DB=0
|
||||
# You may set DB_URL instead for more advanced options
|
||||
DB_HOST=db
|
||||
DB_USER=postgres
|
||||
DB_NAME=postgres
|
||||
|
|
|
@ -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
|
|
@ -16,14 +16,6 @@ Rails.application.configure do
|
|||
if Rails.root.join('tmp/caching-dev.txt').exist?
|
||||
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 = {
|
||||
'Cache-Control' => 'public, max-age=172800',
|
||||
}
|
||||
|
|
|
@ -45,27 +45,6 @@ Rails.application.configure do
|
|||
# Use a different logger for distributed setups.
|
||||
# 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.
|
||||
# config.action_controller.asset_host = 'http://assets.example.com'
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@ Rails.application.configure do
|
|||
config.x.streaming_api_base_url = 'http://localhost:4000'
|
||||
|
||||
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}" }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,22 @@
|
|||
# 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(
|
||||
host: ENV.fetch('REDIS_HOST') { 'localhost' },
|
||||
port: ENV.fetch('REDIS_PORT') { 6379 },
|
||||
password: ENV.fetch('REDIS_PASSWORD') { false },
|
||||
url: ENV['REDIS_URL'],
|
||||
driver: :hiredis
|
||||
)
|
||||
|
||||
Rails.application.configure do
|
||||
config.cache_store = :redis_store, ENV['REDIS_URL'], {
|
||||
namespace: 'cache',
|
||||
expires_in: 10.minutes,
|
||||
}
|
||||
end
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
host = ENV.fetch('REDIS_HOST') { 'localhost' }
|
||||
port = ENV.fetch('REDIS_PORT') { 6379 }
|
||||
password = ENV.fetch('REDIS_PASSWORD') { false }
|
||||
db = ENV.fetch('REDIS_DB') { 0 }
|
||||
# frozen_string_literal: true
|
||||
|
||||
Sidekiq.configure_server do |config|
|
||||
config.redis = { host: host, port: port, db: db, password: password }
|
||||
config.redis = { url: ENV['REDIS_URL'] }
|
||||
end
|
||||
|
||||
Sidekiq.configure_client do |config|
|
||||
config.redis = { host: host, port: port, db: db, password: password }
|
||||
config.redis = { url: ENV['REDIS_URL'] }
|
||||
end
|
||||
|
|
|
@ -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`)
|
||||
|
||||
} 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 = {}
|
||||
|
|
Loading…
Reference in a new issue