class Sidekiq::Cluster::Worker

Attributes

cli[RW]
config[RW]
index[RW]
pid[RW]
state[RW]

Public Class Methods

new(index, cli) click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 31
def initialize(index, cli)
  self.config = cli.config
  self.index  = index
  self.cli    = cli
  self.state  = :idle
end

Public Instance Methods

check_worker() click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 70
def check_worker
  self.pid = Process.pid if pid.nil?
  respawn! if memory_used_pct == -1 && master?
end
handle_signal(sig) click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 90
def handle_signal(sig)
  Process.kill(sig, pid) if pid
rescue Errno::ESRCH
  nil
end
log_worker() click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 104
def log_worker
  cli.info(status) if memory_used_pct >= 0.0
end
master?() click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 100
def master?
  Process.pid == cli.master_pid
end
memory_used_pct() click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 59
def memory_used_pct
  self.pid = Process.pid if pid.nil?
  result = `ps -o %mem= -p #{pid}`
  result.nil? || result == '' ? -1 : result.to_f
end
memory_used_percent() click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 65
def memory_used_percent
  mem = memory_used_pct
  mem < 0 ? 'DEAD' : sprintf('%.2f%', mem)
end
pid_file() click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 96
def pid_file
  "#{config.pid_prefix}.#{index + 1}"
end
respawn!() click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 75
def respawn!
  if master? && pid
    cli.info "NOTE: re-spawning child #{index} (pid #{pid}), memory is at #{memory_used_percent}."
    begin
      Process.kill('USR1', pid)
      sleep 5
      Process.kill('TERM', pid)
    rescue Errno::ESRCH
      nil
    end
    self.pid = nil
    spawn
  end
end
spawn() click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 38
def spawn
  if master?
    cli.info "booting worker #{'%2d' % index} with ARGV '#{config.worker_argv.join(' ')}'"
    self.pid = ::Process.fork do
      # cli.close_logger
      cli.init_logger

      cli.info "child #{index}, running spawn block..."
      begin
        config.spawn_block[self]
      rescue => e
        raise e if $DEBUG
        error e.message
        error e.backtrace.join("\n")
        stop!
      end
    end
  end
  pid
end
status() click to toggle source
# File lib/sidekiq/cluster/worker.rb, line 108
def status
  "worker.#{config.name}[index=#{index}, pid=#{pid ? pid : 'nil'}] —— memory: #{memory_used_percent}"
end