Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Eye配置
DEPLOY_PATH = {
:staging => 'app-name',
:production => 'app-name',
:uat => 'app-name-uat'
}
SERVICES = %w(app-name)
ENVIRONMENTS = [:staging, :uat] #这是测试环境的配置,对于产品环境可以改成[:production]
TIMEOUT = 30.seconds
LONG_TIMEOUT = 60.seconds
STOP_PAUSE = 10.seconds
CHAIN_PAUSE = 05.seconds
# 默认参数设置,默认使用dotenv
def default_options
{
dotenv: true,
}
end
def service_name(app)
app.gsub('-', '_')
end
# 集成.env的环境变量配置
def load_env(environment = :staging )
File.open("/opt/#{DEPLOY_PATH[environment]}/.env", 'rb').each_line do |line|
result = line.split('=')
#设置全局性的环境变量
env result[0] => result[1].chop if result.length > 1
end
# unicorn requires to be `ruby` in path (for soft restart)
env "PATH" => "/usr/local/bin/ruby:#{ENV['PATH']}"
end
def define_application(app, env, options = default_options)
app_name = "#{app}#{'-uat' if env==:uat}"
Eye.application app_name do
load_env(env) if options[:dotenv]
env('RAILS_ENV' => env.to_s) if options[:env]
group "#{app_name}-web" do
chain :grace => CHAIN_PAUSE
process "#{app_name}-web-1" do
working_dir "/opt/#{DEPLOY_PATH[env]}/#{service_name(app)}/current"
pid_file "/opt/#{DEPLOY_PATH[env]}/#{service_name(app)}/tmp/pids/unicorn.pid"
start_command "bundle exec unicorn -E #{env} -Dc ./config/unicorn.rb"
# 对于unicorn使用-D参数设置deamon
stop_signals [:QUIT, STOP_PAUSE, :TERM, STOP_PAUSE, :KILL]
# 没有downtime重启unicorn,产生新的master和worker后,自动杀掉老的master
restart_command "kill -USR2 {PID}"
start_timeout LONG_TIMEOUT
restart_grace LONG_TIMEOUT
stop_timeout TIMEOUT
monitor_children do
stop_command "kill -QUIT {PID}"
end
end
end
group "#{app_name}-worker" do
chain :grace => CHAIN_PAUSE
process "#{app_name}-worker-1" do
working_dir "/opt/#{DEPLOY_PATH[env]}/#{service_name(app)}/current"
pid_file "/opt/#{DEPLOY_PATH[env]}/#{service_name(app)}/tmp/pids/sidekiq.pid"
daemonize true
start_command "bundle exec sidekiq -e #{env}"
stop_signals [:QUIT, STOP_PAUSE, :TERM, STOP_PAUSE, :KILL]
start_timeout TIMEOUT
restart_timeout TIMEOUT
stop_timeout TIMEOUT
monitor_children do
stop_command "kill -QUIT {PID}"
end
end
end
end
end
Eye.config do
logger "/tmp/eye.log"
end
SERVICES.each do |service|
ENVIRONMENTS.each do |environment|
define_application(service, environment)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.