Class: Koinz::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/koinz/notification.rb

Class Method Summary collapse

Class Method Details

.email(payload) ⇒ Object



45
46
47
# File 'lib/koinz/notification.rb', line 45

def self.email(payload)
  publish('koinz.notification.email', payload)
end

.publish(event, payload) ⇒ Object



41
42
43
# File 'lib/koinz/notification.rb', line 41

def self.publish(event, payload)
  REDIS.publish(event, payload)
end

.subscribe(*events, &block) ⇒ Object

subscribe - its always pattern subscribe This is always spawned in a different thread. Eact SET of events is subscribed to in a different thread. You can call this multiple times. Please remember – if you subscribe to common patterns. eg. f* and foo, you will receive the callback twice!!



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/koinz/notification.rb', line 11

def self.subscribe(*events, &block)
  Thread.new do
    # Since we gather the events as an array an not variable arguments,
    # it is necessary to flatten the event array before passing it to
    # psubscribe

    # Process pending messages first!
    REDIS_SUB.backlog(events.flatten, &block)

    REDIS_SUB.psubscribe(*(events.flatten)) do |on|
      on.psubscribe do |event, total|
        log("Subscribed to ##{event} (#{total} subscriptions)")
      end
  
      on.pmessage do |pattern, event, message|
        begin
          block.call(event, MultiJson.decode(message))
        rescue Exception => e
          log("Exception in subscribe: #{e.message}")
        end
      end
  
      on.punsubscribe do |event, total|
        log("Unsubscribed for ##{event} (#{total} subscriptions)")
      end
    end
  end
  log("Exiting Koinz::Notification.subscribe")
end