Skip to content

Instantly share code, notes, and snippets.

@samsongz
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsongz/996a1ef124891ba1f5f7 to your computer and use it in GitHub Desktop.
Save samsongz/996a1ef124891ba1f5f7 to your computer and use it in GitHub Desktop.
Deploy Promiscuous Subscriber
after "deploy:restart", "deploy:restart_subscriber"
def run_remote_rake(rake_cmd)
rake_args = ENV['RAKE_ARGS'].to_s.split(',')
cmd = "cd #{fetch(:latest_release)} && bundle exec #{fetch(:rake, "rake")} RAILS_ENV=#{fetch(:rails_env, "production")} #{rake_cmd}"
cmd += "['#{rake_args.join("','")}']" unless rake_args.empty?
run cmd
set :rakefile, nil if exists?(:rakefile)
end
namespace :deploy do
desc "Restart Promiscuous Subscriber"
task :restart_subscriber, :roles => :worker do
run_remote_rake "promiscuous:restart_subscriber"
end
end
namespace :promiscuous do
desc "Restart Promiscuous Subscriber"
task :restart_subscriber => :environment do
Rake::Task['promiscuous:stop_subscriber'].invoke
Rake::Task['promiscuous:start_subscriber'].invoke
end
desc "Quit running subscriber"
task :stop_subscriber => :environment do
stop_subscriber
end
desc "Start subscriber worker"
task :start_subscriber => :environment do
run_subscriber(1)
end
def store_pids(pids, mode)
pids_to_store = pids
pids_to_store += read_pids if mode == :append
# Make sure the pid file is writable.
File.open(File.expand_path('tmp/pids/promiscuous.pid', Rails.root), 'w') do |f|
f << pids_to_store.join(',')
end
end
def read_pids
pid_file_path = Rails.root + 'tmp/pids/promiscuous.pid'
return [] if ! File.exists?(pid_file_path)
File.open(pid_file_path, 'r') do |f|
f.read
end.split(',').collect {|p| p.to_i }
end
def stop_subscriber
pids = read_pids
if pids.empty?
puts "No workers to kill"
else
syscmd = "kill -s QUIT #{pids.join(' ')}"
puts "$ #{syscmd}"
`#{syscmd}`
store_pids([], :write)
end
end
# Start a subscriber with proper env vars and output redirection
def run_subscriber(count = 1)
puts "Starting Promiscuous subscriber worker"
## make sure log/resque_err, log/resque_stdout are writable.
ops = {:pgroup => true, :err => [(Rails.root + "log/promiscuous_err").to_s, "a"],
:out => [(Rails.root + "log/promiscuous_stdout").to_s, "a"]}
env_vars = { 'RAILS_ENV' => Rails.env.to_s }
pids = []
count.times do
## Using Kernel.spawn and Process.detach because regular system() call would
## cause the processes to quit when capistrano finishes
pid = spawn(env_vars, "bundle exec promiscuous subscribe", ops)
Process.detach(pid)
pids << pid
end
store_pids(pids, :append)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment