Class: Subserver::Manager

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/subserver/manager.rb

Overview

The Manager is the central coordination point in Subserver, controlling the lifecycle of the Google Cloud Listeners.

Tasks:

  1. start: Load subscibers and start listeners.

  2. listener_died: restart listener

  3. quiet: tell listeners to stop listening and finish processing messages then shutdown.

  4. stop: hard stop the listeners by deadline.

Note that only the last task requires its own Thread since it has to monitor the shutdown process. The other tasks are performed by other threads.

Constant Summary collapse

PAUSE_TIME =

hack for quicker development / testing environment

STDOUT.tty? ? 0.1 : 0.5

Constants included from Util

Util::EXPIRY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#fire_event, #hostname, #identity, #logger, #process_nonce, #safe_thread, #watchdog

Methods included from ExceptionHandler

#handle_exception

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/subserver/manager.rb', line 29

def initialize(options={})
  logger.debug { options.inspect }
  @options = options
  
  @done = false
  @listeners = Set.new

  subscribers.each do |subscriber|
    @listeners << Listener.new(self, subscriber)
  end

  @plock = Mutex.new
end

Instance Attribute Details

#listenersObject (readonly)

Returns the value of attribute listeners.



26
27
28
# File 'lib/subserver/manager.rb', line 26

def listeners
  @listeners
end

#optionsObject (readonly)

Returns the value of attribute options.



27
28
29
# File 'lib/subserver/manager.rb', line 27

def options
  @options
end

Instance Method Details

#listener_died(listener, subscriber, reason) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/subserver/manager.rb', line 94

def listener_died(listener, subscriber, reason)
  @plock.synchronize do
    @listeners.delete(listener)
    unless @done
      l = Listener.new(self, subscriber)
      @listeners << l
      l.start
    end
  end
end

#listener_stopped(listener) ⇒ Object



88
89
90
91
92
# File 'lib/subserver/manager.rb', line 88

def listener_stopped(listener)
  @plock.synchronize do
    @listeners.delete(listener)
  end
end

#quietObject



54
55
56
57
58
59
60
61
# File 'lib/subserver/manager.rb', line 54

def quiet
  return if @done
  @done = true

  logger.info { "Stopping listeners" }
  @listeners.each { |x| x.stop }
  fire_event(:quiet, reverse: true)
end

#startObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/subserver/manager.rb', line 43

def start
  if @listeners.count > 0
    logger.info("Starting Listeners For: #{@listeners.map(&:name).join(', ')}")
    @listeners.each do |x|
      x.start
    end
  else
    logger.warn("No Listeners starting: Couldn't find any subscribers.")
  end
end

#stop(deadline) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/subserver/manager.rb', line 66

def stop(deadline)
  quiet
  fire_event(:shutdown, reverse: true)

  # some of the shutdown events can be async,
  # we don't have any way to know when they're done but
  # give them a little time to take effect
  sleep PAUSE_TIME
  return if @listeners.empty?

  logger.info { "Pausing to allow listeners to finish..." }
  remaining = deadline - Time.now
  while remaining > PAUSE_TIME
    return if @listeners.empty?
    sleep PAUSE_TIME
    remaining = deadline - Time.now
  end
  return if @listeners.empty?

  hard_shutdown
end

#stopped?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/subserver/manager.rb', line 105

def stopped?
  @done
end

#subscribersObject



109
110
111
# File 'lib/subserver/manager.rb', line 109

def subscribers
  @subscribers ||= load_subscribers
end