Class: Resqued::ListenerPool

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/resqued/listener_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(master_state) ⇒ ListenerPool

Public: Initialize a new pool, and store state in the given master’s state.



9
10
11
12
13
14
15
16
# File 'lib/resqued/listener_pool.rb', line 9

def initialize(master_state)
  @master_state = master_state
  @listener_proxies = {}
  # If this master is replacing an old one, there will be listeners in the state already.
  @master_state.listener_states.each do |pid, ls|
    @listener_proxies[pid] = ListenerProxy.new(ls)
  end
end

Instance Method Details

#clear_current!Object

Public: Don’t consider the current listener to be current anymore.



66
67
68
# File 'lib/resqued/listener_pool.rb', line 66

def clear_current!
  @master_state.current_listener_pid = nil
end

#clear_last_good!Object

Public: Forget which listener was the last good one.



87
88
89
# File 'lib/resqued/listener_pool.rb', line 87

def clear_last_good!
  @master_state.last_good_listener_pid = nil
end

#currentObject

Public: The current ListenerProxy, if available.



56
57
58
# File 'lib/resqued/listener_pool.rb', line 56

def current
  @listener_proxies[current_pid]
end

#current_pidObject

Public: The pid of the current listener, if available.



61
62
63
# File 'lib/resqued/listener_pool.rb', line 61

def current_pid
  @master_state.current_listener_pid
end

#cycle_currentObject

Public: Change the current listener into the last good listener.



71
72
73
74
# File 'lib/resqued/listener_pool.rb', line 71

def cycle_current
  @master_state.last_good_listener_pid = @master_state.current_listener_pid
  @master_state.current_listener_pid = nil
end

#delete(pid) ⇒ Object

Public: Remove the given pid from the set of known listeners, and return its ListenerProxy.



50
51
52
53
# File 'lib/resqued/listener_pool.rb', line 50

def delete(pid)
  @master_state.listener_states.delete(pid)
  return @listener_proxies.delete(pid)
end

#each(&block) ⇒ Object

Public: Iterate through all active ListenerProxy instances.



19
20
21
# File 'lib/resqued/listener_pool.rb', line 19

def each(&block)
  @listener_proxies.values.each(&block)
end

#empty?Boolean

Public: Are the listeners all gone?

Returns:

  • (Boolean)


29
30
31
# File 'lib/resqued/listener_pool.rb', line 29

def empty?
  @listener_proxies.empty?
end

#last_goodObject

Public: The last good (previous current) ListenerProxy, if available.



77
78
79
# File 'lib/resqued/listener_pool.rb', line 77

def last_good
  @listener_proxies[last_good_pid]
end

#last_good_pidObject

Public: The pid of the last good listener, if available.



82
83
84
# File 'lib/resqued/listener_pool.rb', line 82

def last_good_pid
  @master_state.last_good_listener_pid
end

#sizeObject

Public: Number of active listeners.



24
25
26
# File 'lib/resqued/listener_pool.rb', line 24

def size
  @listener_proxies.size
end

#start!Object

Public: Initialize a new listener, run it, and record it as the current listener. Returns its ListenerProxy.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/resqued/listener_pool.rb', line 34

def start!
  listener_state = ListenerState.new
  listener_state.options = {
    config_paths: @master_state.config_paths,
    old_workers: map { |l| l.running_workers }.flatten,
    listener_id: next_listener_id,
  }
  listener = ListenerProxy.new(listener_state)
  listener.run
  @master_state.listener_states[listener.pid] = listener_state
  @listener_proxies[listener.pid] = listener
  @master_state.current_listener_pid = listener.pid
  return listener
end