Class: Check::Notifications

Inherits:
Object
  • Object
show all
Defined in:
lib/check/notifications.rb

Instance Method Summary collapse

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_message.(on)
      on_unsubscribe.(on)
    end
  end
end

Instance Method Details

#on_messageObject



29
30
31
32
33
34
35
# File 'lib/check/notifications.rb', line 29

def on_message
  Proc.new do |on|
    on.message do |channel, message|
      puts "#{channel}: #{unpack(message)}"
    end
  end
end

#on_subscribeObject



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_unsubscribeObject



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

#shutdownObject



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_gracefullyObject



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(message)
  MessagePack.unpack(message)
rescue MessagePack::UnpackError
  puts "#{message.inspect} could not be unpacked by MessagePack"
end