module Octopusci::WorkerLauncher

Public Class Methods

cleanup_existing_workers() click to toggle source
# File lib/octopusci/worker_launcher.rb, line 6
def self.cleanup_existing_workers
  exist_worker_pids = get_worker_pids()
  puts "Cleaning up any existing workers... (#{exist_worker_pids.join(',')})"
  exist_worker_pids.each do |pid|
    wait_to_finish_and_exit(pid)
  end
  del_worker_pids
  puts "Finished cleaning up workers"
end
del_worker_pids() click to toggle source
# File lib/octopusci/worker_launcher.rb, line 114
def self.del_worker_pids
  Resque.redis.del(pids_list_key())
end
do_not_process_new_jobs(pid) click to toggle source
# File lib/octopusci/worker_launcher.rb, line 93
def self.do_not_process_new_jobs(pid)
  kill("USR2", pid)
end
get_worker_pids() click to toggle source
# File lib/octopusci/worker_launcher.rb, line 105
def self.get_worker_pids
  len = Resque.redis.llen(pids_list_key())
  if len > 0
    return Resque.redis.lrange(pids_list_key(), 0, len-1)
  else
    return []
  end
end
immediately_kill_child(pid) click to toggle source
# File lib/octopusci/worker_launcher.rb, line 89
def self.immediately_kill_child(pid)
  kill("USR1", pid)
end
immediately_kill_child_and_exit(pid) click to toggle source
# File lib/octopusci/worker_launcher.rb, line 85
def self.immediately_kill_child_and_exit(pid)
  kill("TERM", pid)
end
kill(signal_str, pid) click to toggle source
# File lib/octopusci/worker_launcher.rb, line 72
def self.kill(signal_str, pid)
  begin
    Process.kill(signal_str, pid.to_i)
    puts "Sent '#{signal_str}' signal to worker with pid (#{pid})"
  rescue Errno::ESRCH => e
    puts "Failed to send '#{signal_str}' to worker with pid (#{pid}) - #{e.to_s}"
  end
end
launch() click to toggle source
# File lib/octopusci/worker_launcher.rb, line 16
def self.launch
  puts "Launching new workers..."

  # We need to disconnect the redis connection because when we fork and
  # each fork gets a copy of the same connection it is a problem and the
  # redis protocol will detect it and throw an exception. This is
  # necessary because each forked instance needs its own redis connection.
  # This only works because Resque.redis manages a singleton an instance
  # variable and if that instance variable isn't set then it reconnects
  # and returns the new connection.
  Resque.redis.quit

  if Octopusci::Config['general']['tentacles_user'] != nil
    require 'etc'
    chosen_user = Etc.getpwnam(Octopusci::Config['general']['tentacles_user'])
    if chosen_user
      # Switch the effective and real uid to the specified user
      Process.uid = chosen_user.uid
      Process.euid = chosen_user.uid

      # Set the USER, HOME, and SHELL environment variables to those found in /etc/passwd for the specified user.
      # This is important for some applications/scripts like RVM so that they can find things relative to the users home directory.
      ENV['USER'] = chosen_user.name
      ENV['SHELL'] = chosen_user.shell
      ENV['HOME'] = chosen_user.dir

      # Set the OCTOPUSCI environment variable applications to true so that programs instantiated by ocotpusci jobs could determine if
      # they are being run by octopusci or not.
      ENV['OCTOPUSCI'] = 'true'
    end
  end

  worker_pids = []
  
  Octopusci::Config['stages'].size.times do
    cur_pid = Process.fork do
      queues = ['octopusci:commit']
      worker = Resque::Worker.new(*queues)
      worker.log "Starting worker #{worker}"
      worker.work(5)
    end
    
    worker_pids << cur_pid
    
    Process.detach(cur_pid)
    
    puts "Launched worker with pid (#{cur_pid})"
  end
  
  worker_pids.each do |pid|
    push_worker_pid(pid)
  end
  
  puts "Finished launching workers"
end
pids_list_key() click to toggle source
# File lib/octopusci/worker_launcher.rb, line 118
def self.pids_list_key
  "octopusci:workerpids"
end
push_worker_pid(pid) click to toggle source
# File lib/octopusci/worker_launcher.rb, line 101
def self.push_worker_pid(pid)
  Resque.redis.rpush(pids_list_key, pid)
end
start_to_process_new_jobs_again(pid) click to toggle source
# File lib/octopusci/worker_launcher.rb, line 97
def self.start_to_process_new_jobs_again(pid)
  kill("CONT", pid)
end
wait_to_finish_and_exit(pid) click to toggle source
# File lib/octopusci/worker_launcher.rb, line 81
def self.wait_to_finish_and_exit(pid)
  kill("QUIT", pid)
end