Class: Check::Notifications
- Inherits:
-
Object
- Object
- Check::Notifications
- Defined in:
- lib/check/notifications.rb
Instance Method Summary collapse
-
#initialize(redis_notifications = REDIS_NOTIFICATIONS) ⇒ Notifications
constructor
A new instance of Notifications.
- #on_message ⇒ Object
- #on_subscribe ⇒ Object
- #on_unsubscribe ⇒ Object
- #shutdown ⇒ Object
- #shutdown_gracefully ⇒ Object
- #unpack(message) ⇒ Object
Constructor Details
#initialize(redis_notifications = REDIS_NOTIFICATIONS) ⇒ Notifications
Returns a new instance of Notifications.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/check/notifications.rb', line 7 def initialize(redis_notifications=REDIS_NOTIFICATIONS) shutdown_gracefully Redis.current.subscribe(redis_notifications) do |on| if block_given? yield(on) else on_subscribe.(on) .(on) on_unsubscribe.(on) end end end |
Instance Method Details
#on_message ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/check/notifications.rb', line 29 def Proc.new do |on| on. do |channel, | puts "#{channel}: #{unpack()}" end end end |
#on_subscribe ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/check/notifications.rb', line 21 def on_subscribe Proc.new do |on| on.subscribe do |channel, subscriptions| puts "Subscribed to #{channel} (#{subscriptions} subscriptions)" end end end |
#on_unsubscribe ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/check/notifications.rb', line 37 def on_unsubscribe Proc.new do |on| on.unsubscribe do |channel, subscriptions| puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)" end end end |
#shutdown ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/check/notifications.rb', line 57 def shutdown Proc.new do Redis.current.disconnect puts "\n#{self.class} shutting down..." exit 0 end end |
#shutdown_gracefully ⇒ Object
51 52 53 54 55 |
# File 'lib/check/notifications.rb', line 51 def shutdown_gracefully Signal.trap "SIGTERM", shutdown Signal.trap "SIGINT", shutdown Signal.trap "SIGQUIT", shutdown end |
#unpack(message) ⇒ Object
45 46 47 48 49 |
# File 'lib/check/notifications.rb', line 45 def unpack() MessagePack.unpack() rescue MessagePack::UnpackError puts "#{.inspect} could not be unpacked by MessagePack" end |