Skip to content

Commit

Permalink
отвязка god от релизных папок
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Apr 1, 2014
1 parent fb69813 commit 929e7dc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ redis:
resque:
interval: 5 # частота, с которой resque берет задачи из очереди в секундах (по умолчанию 5)
verbosity: 1 # "шумность" логера (0 - ничего не пишет, 1 - пишет о начале/конце задачи, 2 - пишет все)
log_file: "log/resque.log" # путь до log-файла от корня проекта
root: "/home/blizko/current" # (опционально) путь до корня проекта. нужно для корректных деплоев, так что настраивается только в production
root: "/home/pc/current" # (production) абсолютный путь до корня проекта
log_file: "/home/pc/static/pulscen/local/log/resque.log" # (production) абсолютный путь до лога
config_file: "/home/pc/static/pulscen/local/log/resque.god" # (production) абсолютный путь до кофига god
pids: "/home/pc/static/pulscen/local/pids" # (production) абсолютный путь до папки с пид файлами

# переменные окружения, которые надобно передавать в resque
env:
Expand All @@ -118,6 +120,7 @@ workers:
stop_timeout: 5 # максимальное время, отпущенное воркеру для остановки/рестарта
env: # переменные окружение, специфичные для данного воркера
RUBY_HEAP_SLOTS_GROWTH_FACTOR: 0.5
'companies,images: 2 # совмещённая очередь, приоритет будет у companies
# конфигурация failure-бэкэндов
failure:
Expand Down
14 changes: 13 additions & 1 deletion lib/resque/integration/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,19 @@ def verbosity

# Returns path to resque log file
def log_file
self['resque.log_file'] || 'log/resque.log'
self['resque.log_file'] || Rails.root.join('log/resque.log').to_s
end

def config_file
self['resque.config_file'] || Rails.root.join('config/resque.god').to_s
end

def pid_file
"#{pids}/resque-god.pid"
end

def pids
self['resque.pids'] || Rails.root.join('tmp/pids').to_s
end

def root
Expand Down
6 changes: 3 additions & 3 deletions lib/resque/integration/god.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# THIS FILE IS AUTO-GENERATED. PLEASE DO NOT CHANGE IT!

bundle = '<%= `which bundle`.strip %>'
rails_root = <%= root.inspect %>
God.pid_file_directory = File.join(rails_root, 'tmp/pids')
rails_root = '<%= root %>'
God.pid_file_directory = '<%= pids %>'
God.terminate_timeout = <%= terminate_timeout.to_i %>
<% workers.each do |worker| %>
Expand All @@ -14,7 +14,7 @@ God.terminate_timeout = <%= terminate_timeout.to_i %>
w.interval = 30.seconds
w.env = <%= Resque.config.env.merge(worker.env).merge(:RAILS_ENV => ::Rails.env, :BUNDLE_GEMFILE => "#{root}/Gemfile").stringify_keys! %>
w.start = "#{bundle} exec rake -f #{rails_root}/Rakefile resque:work"
w.log = "#{rails_root}/<%= log_file %>"
w.log = '<%= log_file %>'
w.stop_signal = 'QUIT'

<% if worker.stop_timeout %>
Expand Down
2 changes: 1 addition & 1 deletion lib/resque/integration/logs_rotator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class << self

# Перенаправить стандартный вывод в файл
def redirect_std
log_path = File.join(::Resque.config.root, ::Resque.config.log_file)
log_path = ::Resque.config.log_file
STDOUT.reopen File.open(log_path, 'a')
STDERR.reopen File.open(log_path, 'a')
STDOUT.sync = STDERR.sync = true
Expand Down
35 changes: 13 additions & 22 deletions lib/resque/integration/tasks/resque.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace :resque do
desc 'Generate God configuration file'
task :conf => :environment do
File.write(config_file, Resque.config.to_god)
File.write(Resque.config.config_file, Resque.config.to_god)

puts "God configuration file generated to #{config_file}"
puts "God configuration file generated to #{Resque.config.config_file}"
end

desc 'Start God server and watch for Resque workers'
task :start => :conf do
if god_running?
puts `#{god} start resque`
else
puts `#{god} -c #{config_file} -P #{pid_file} -l #{log_file}`
puts `#{god} -c #{Resque.config.config_file} -P #{Resque.config.pid_file} -l #{Resque.config.log_file}`
end
end

Expand All @@ -22,7 +22,7 @@ namespace :resque do
if god_stopped?
Rake::Task['resque:start'].invoke
else
puts `#{god} load #{config_file} stop && #{god} restart resque`
puts `#{god} load #{Resque.config.config_file} stop && #{god} restart resque`
end
end

Expand Down Expand Up @@ -54,21 +54,24 @@ namespace :resque do
namespace :logs do
desc 'Rotate resque logs'
task :rotate do
return unless god_running?

Process.kill('USR1', File.read(pid_file).to_i)
sleep 3
puts `#{god} signal resque HUP`
if god_running?
Process.kill('USR1', File.read(Resque.config.pid_file).to_i)
sleep 3
puts `#{god} signal resque HUP`
else
puts 'god is not running'
end
end
end

private

def god
`which god`.strip
end

def god_running?
File.exists?(pid_file) && Process.kill(0, File.read(pid_file).to_i)
File.exists?(Resque.config.pid_file) && Process.kill(0, File.read(Resque.config.pid_file).to_i)
rescue Errno::ESRCH
false
rescue Errno::EPERM
Expand All @@ -78,16 +81,4 @@ namespace :resque do
def god_stopped?
!god_running?
end

def pid_file
Rails.root.join('tmp/pids/resque-god.pid').to_s
end

def log_file
Rails.root.join(Resque.config.log_file).to_s
end

def config_file
Rails.root.join('config/resque.god').to_s
end
end

0 comments on commit 929e7dc

Please sign in to comment.