class Flapjack::Pikelet::Base

Attributes

pikelet[RW]
redis[RW]
siblings[RW]

Public Class Methods

new(pikelet_class, shutdown, opts = {}) click to toggle source
# File lib/flapjack/pikelet.rb, line 40
def initialize(pikelet_class, shutdown, opts = {})
  @pikelet_class = pikelet_class

  @config        = opts[:config]
  @boot_time     = opts[:boot_time]
  @logger_name   = opts[:logger_name]
  @shutdown      = shutdown

  @siblings      = []

  @lock = Monitor.new
  @stop_condition = @lock.new_cond

  @pikelet = @pikelet_class.new(:lock => @lock,
    :stop_condition => @stop_condition, :config => @config,
    :boot_time => @boot_time)

  @finished_condition = @lock.new_cond
end

Public Instance Methods

reload(cfg) { || ... } click to toggle source
# File lib/flapjack/pikelet.rb, line 107
def reload(cfg, &block)
  Flapjack.logger.configure(cfg['logger'])
  yield
end
start() { || ... } click to toggle source
# File lib/flapjack/pikelet.rb, line 60
def start(&block)
  @pikelet.siblings = @siblings.map(&:pikelet) if @pikelet.respond_to?(:siblings=)

  @thread = Thread.new do

    Flapjack.configure_log(@logger_name, @config['logger'])

    # TODO rename this, it's only relevant in the error case
    max_runs = @config['max_runs'] || 1
    runs = 0

    keep_running = false
    shutdown_all = false

    loop do
      begin
        Flapjack.logger.debug "pikelet start for #{@pikelet_class.name}"
        yield
      rescue Flapjack::PikeletStop
        Flapjack.logger.debug "pikelet exception stop for #{@pikelet_class.name}"
      rescue Flapjack::GlobalStop
        Flapjack.logger.debug "global exception stop for #{@pikelet_class.name}"
        @shutdown_thread = @thread
        shutdown_all = true
      rescue Exception => e
        Flapjack.logger.warn "#{e.class.name} #{e.message}"
        trace = e.backtrace
        Flapjack.logger.warn trace.join("\n") if trace
        runs += 1
        keep_running = (max_runs > 0) && (runs < max_runs)
        shutdown_all = !keep_running
      end

      break unless keep_running
    end

    @lock.synchronize do
      @finished = true
      @finished_condition.signal
    end

    if shutdown_all
      @shutdown.call
    end
  end
end
stop() { || ... } click to toggle source
# File lib/flapjack/pikelet.rb, line 112
def stop(&block)
  fin = nil
  @lock.synchronize do
    fin = @finished
  end
  return if fin
  if block_given?
    yield
  else
    case @pikelet.stop_type
    when :exception
      @lock.synchronize do
        Flapjack.logger.debug "triggering pikelet exception stop for #{@pikelet_class.name}"
        @thread.raise Flapjack::PikeletStop
        @finished_condition.wait_until { @finished }
      end
    when :signal
      @lock.synchronize do
        Flapjack.logger.debug "triggering pikelet signal stop for #{@pikelet_class.name}"
        @pikelet.instance_variable_set('@should_quit', true)
        @stop_condition.signal
        @finished_condition.wait_until { @finished }
      end
    end
  end

  @thread.join
  @thread = nil
end