class Sponges::Runner

This class concern is to create a Supervisor, set some signals handlers and watch over the supervisor.

Attributes

store[R]

Public Class Methods

new(name, options = {}, block) click to toggle source
# File lib/sponges/runner.rb, line 9
def initialize(name, options = {}, block)
  @name, @block = name, block
  @options = default_options.merge options
  @store = Sponges::Store.new(@name)
  if store.running?
    Sponges.logger.error "Runner #{@name} already started."
    exit
  end
end

Public Instance Methods

start() click to toggle source
# File lib/sponges/runner.rb, line 19
def start
  if daemonize?
    Sponges.logger.info "Supervisor daemonized."
    Process.daemon
  end
  Sponges.logger.info "Runner #{@name} start message received."
  @supervisor = fork_supervisor
  trap_signals
  Sponges.logger.info "Supervisor started with #{@supervisor} pid."
  Process.waitpid(@supervisor) unless daemonize?
end

Private Instance Methods

daemonize?() click to toggle source
# File lib/sponges/runner.rb, line 57
def daemonize?
  !!@options[:daemonize]
end
default_options() click to toggle source
# File lib/sponges/runner.rb, line 45
def default_options
  {
    size: Machine::Info::Cpu.cores_size
  }
end
fork_supervisor() click to toggle source
# File lib/sponges/runner.rb, line 51
def fork_supervisor
  fork do
    Supervisor.new(@name, @options, store, @block).start
  end
end
kill_supervisor() click to toggle source
# File lib/sponges/runner.rb, line 39
def kill_supervisor
  Process.kill :QUIT, @supervisor
rescue Errno::ESRCH => e
  # Don't panic
end
trap_signals() click to toggle source
# File lib/sponges/runner.rb, line 33
def trap_signals
  Sponges::SIGNALS.each do |signal|
    trap(signal) {|signal| kill_supervisor }
  end
end