Class: CableRoom::ChannelTracker
- Inherits:
-
Object
- Object
- CableRoom::ChannelTracker
- Defined in:
- lib/cable_room/channel_tracker.rb
Defined Under Namespace
Classes: ThreadPool
Constant Summary collapse
- BEAT_INTERVAL =
5.seconds
Instance Attribute Summary collapse
-
#room_channels ⇒ Object
readonly
Returns the value of attribute room_channels.
-
#scheduler ⇒ Object
readonly
Returns the value of attribute scheduler.
Class Method Summary collapse
Instance Method Summary collapse
- #each_room_channel(&blk) ⇒ Object
-
#initialize ⇒ ChannelTracker
constructor
A new instance of ChannelTracker.
- #shutdown! ⇒ Object
- #shutdown? ⇒ Boolean
- #shutdown_rooms! ⇒ Object
- #track_room_channel(chan) ⇒ Object
- #untrack_room_channel(chan) ⇒ Object
- #worker_pool ⇒ Object
Constructor Details
#initialize ⇒ ChannelTracker
Returns a new instance of ChannelTracker.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/cable_room/channel_tracker.rb', line 13 def initialize @room_channels = Set.new @monitor = Monitor.new @scheduler = Rufus::Scheduler.new at_exit do logger.info "Shutting down CableRoom" shutdown! end scheduler.every(BEAT_INTERVAL) do each_room_channel do |chan| chan.beat end end end |
Instance Attribute Details
#room_channels ⇒ Object (readonly)
Returns the value of attribute room_channels.
11 12 13 |
# File 'lib/cable_room/channel_tracker.rb', line 11 def room_channels @room_channels end |
#scheduler ⇒ Object (readonly)
Returns the value of attribute scheduler.
11 12 13 |
# File 'lib/cable_room/channel_tracker.rb', line 11 def scheduler @scheduler end |
Class Method Details
.instance ⇒ Object
3 4 5 |
# File 'lib/cable_room/channel_tracker.rb', line 3 def self.instance @instance ||= new end |
Instance Method Details
#each_room_channel(&blk) ⇒ Object
78 79 80 |
# File 'lib/cable_room/channel_tracker.rb', line 78 def each_room_channel(&blk) @room_channels.dup.each(&blk) end |
#shutdown! ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cable_room/channel_tracker.rb', line 49 def shutdown! @monitor.synchronize do @shutdown = true scheduler.shutdown shutdown_rooms! worker_pool.executor.shutdown worker_pool.executor.wait_for_termination(5) worker_pool.executor.kill end end |
#shutdown? ⇒ Boolean
45 46 47 |
# File 'lib/cable_room/channel_tracker.rb', line 45 def shutdown? @shutdown end |
#shutdown_rooms! ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cable_room/channel_tracker.rb', line 63 def shutdown_rooms! @monitor.synchronize do each_room_channel do |chan| chan.initiate_shutdown("Server shutting down") end end count = 0 loop do break if @room_channels.empty? || count >= 15 sleep 1 count += 1 end end |
#track_room_channel(chan) ⇒ Object
36 37 38 39 |
# File 'lib/cable_room/channel_tracker.rb', line 36 def track_room_channel(chan) raise "Cannot add Room after shutdown" if @shutdown @room_channels << chan end |
#untrack_room_channel(chan) ⇒ Object
41 42 43 |
# File 'lib/cable_room/channel_tracker.rb', line 41 def untrack_room_channel(chan) @room_channels.delete(chan) end |
#worker_pool ⇒ Object
31 32 33 34 |
# File 'lib/cable_room/channel_tracker.rb', line 31 def worker_pool # TODO Pin Rooms to a specific thread so that they never have to worry about thread safety? @worker_pool || @monitor.synchronize { @worker_pool ||= ThreadPool.new(max_size: config.worker_pool_size) } end |