Class: Flapjack::Pikelet::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/flapjack/pikelet.rb

Direct Known Subclasses

Generic, HTTP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pikelet_class, shutdown, opts = {}) ⇒ Base

Returns a new instance of Base.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

Instance Attribute Details

#pikeletObject

Returns the value of attribute pikelet.



38
39
40
# File 'lib/flapjack/pikelet.rb', line 38

def pikelet
  @pikelet
end

#redisObject

Returns the value of attribute redis.



38
39
40
# File 'lib/flapjack/pikelet.rb', line 38

def redis
  @redis
end

#siblingsObject

Returns the value of attribute siblings.



38
39
40
# File 'lib/flapjack/pikelet.rb', line 38

def siblings
  @siblings
end

Instance Method Details

#reload(cfg, &block) ⇒ Object



107
108
109
110
# File 'lib/flapjack/pikelet.rb', line 107

def reload(cfg, &block)
  Flapjack.logger.configure(cfg['logger'])
  yield
end

#start(&block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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(&block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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