Class: Webtube::Vital_Statistics
- Inherits:
-
Object
- Object
- Webtube::Vital_Statistics
- Defined in:
- lib/webtube/vital-statistics.rb
Overview
A tracker for live [[Webtube]] instances and their threads. This allows a shutdowning WEBrick to gently close the pending WebSockets.
Instance Attribute Summary collapse
-
#thread_group ⇒ Object
readonly
A [[ThreadGroup]] into which the Webtube threads can add themselves.
Instance Method Summary collapse
- #birth(webtube) ⇒ Object
-
#close_all(status_code = 1001, explanation = "") ⇒ Object
The default status code in a shutdown situation is 1001 ‘going away’.
- #death(webtube) ⇒ Object
-
#initialize(logger) ⇒ Vital_Statistics
constructor
A new instance of Vital_Statistics.
-
#to_a ⇒ Object
Construct a list of all the currently living [[Webtube]] instances.
Constructor Details
#initialize(logger) ⇒ Vital_Statistics
Returns a new instance of Vital_Statistics.
26 27 28 29 30 31 32 33 |
# File 'lib/webtube/vital-statistics.rb', line 26 def initialize logger super() @logger = logger @webtubes = Set.new @mutex = Mutex.new @thread_group = ThreadGroup.new return end |
Instance Attribute Details
#thread_group ⇒ Object (readonly)
A [[ThreadGroup]] into which the Webtube threads can add themselves. Note that [[Vital_Statistics]] does not forcefully move them (nor could it – a Webtube does not get a thread before [[Webtube#run]] is called, which is normally after [[Vital_Statistics#birth]] gets called).
When Webtube is being integrated with WEBrick by [[webtube/webrick.rb]], assigning Webtube-specific threads into this group will cause WEBrick’s standard shutdown procedure to not try to [[Thread#join]] them as it does to ordinary WEBrick threads. Instead, the integration code will call [[Vital_Statistics#close_all]], to request that each Webtube close itself, and then join the threads from [[Vital_Statistics#thread_group]].
24 25 26 |
# File 'lib/webtube/vital-statistics.rb', line 24 def thread_group @thread_group end |
Instance Method Details
#birth(webtube) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/webtube/vital-statistics.rb', line 35 def birth webtube @mutex.synchronize do @webtubes.add webtube end return end |
#close_all(status_code = 1001, explanation = "") ⇒ Object
The default status code in a shutdown situation is 1001 ‘going away’.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/webtube/vital-statistics.rb', line 60 def close_all status_code = 1001, explanation = "" # Note that we're only mutexing off extracting the content # of [[@webtubes]] (see [[to_a]]). We can't mutex the # whole block, for as the webtubes will be closing, # they'll want to notify us about it, and that is also # mutexed. # # This is not as bad as it may sound, for the webserver # shouldn't be accepting new connections anymore anyway by # the time it'll start closing the old ones. self.to_a.each do |webtube| begin webtube.close status_code, explanation rescue Exception => e # log and continue logger.error e end end return end |
#death(webtube) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/webtube/vital-statistics.rb', line 42 def death webtube @mutex.synchronize do @webtubes.delete webtube end return end |
#to_a ⇒ Object
Construct a list of all the currently living [[Webtube]] instances. Note that while the operation is atomic, the Webtube infrastructure is inherently multithreaded, so the list can get slightly stale immediately and should, in most contexts, be considered informative.
54 55 56 |
# File 'lib/webtube/vital-statistics.rb', line 54 def to_a return @mutex.synchronize{@webtubes.to_a} end |