Class: Wakame::EventDispatcher

Inherits:
Object
  • Object
show all
Includes:
ThreadImmutable
Defined in:
lib/wakame/event_dispatcher.rb

Constant Summary collapse

FIRE_EVENT_LOG_IGNORE_EVENTS =
[Wakame::Event::AgentPong]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ThreadImmutable

#bind_thread, included, #target_thread, #target_thread?, #thread_check

Constructor Details

#initializeEventDispatcher

Returns a new instance of EventDispatcher.



43
44
45
46
47
48
# File 'lib/wakame/event_dispatcher.rb', line 43

def initialize
  @event_handlers = {}
  @tickets = {}

  @unsubscribe_queue = []
end

Class Method Details

.fire_event(event_obj) ⇒ Object



31
32
33
# File 'lib/wakame/event_dispatcher.rb', line 31

def fire_event(event_obj)
  self.instance.fire_event(event_obj)
end

.instanceObject



6
7
8
9
10
11
# File 'lib/wakame/event_dispatcher.rb', line 6

def instance
  if @instance.nil?
    @instance = self.new
  end
  @instance
end

.resetObject



35
36
37
# File 'lib/wakame/event_dispatcher.rb', line 35

def reset
  @instance = nil
end

.subscribe(event_class, *args, &blk) ⇒ Object



13
14
15
# File 'lib/wakame/event_dispatcher.rb', line 13

def subscribe(event_class, *args, &blk)
  self.instance.subscribe(event_class, *args, &blk)
end

.subscribe_once(event_class, *args, &blk) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/wakame/event_dispatcher.rb', line 21

def subscribe_once(event_class, *args, &blk)
  tkt = self.subscribe(event_class, *args) { |event|
    begin
      blk.call(event) if blk
    ensure
      self.unsubscribe(tkt)
    end
  }
end

.unsubscribe(ticket) ⇒ Object



17
18
19
# File 'lib/wakame/event_dispatcher.rb', line 17

def unsubscribe(ticket)
  self.instance.unsubscribe(ticket)
end

Instance Method Details

#fire_event(event_obj) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wakame/event_dispatcher.rb', line 100

def fire_event(event_obj)
  raise ArgumentError unless event_obj.is_a?(Event::Base)

  if Wakame.log.level == 'DEBUG' && !FIRE_EVENT_LOG_IGNORE_EVENTS.member?(event_obj.class)
    Wakame.log.debug("Event #{event_obj.class} has been fired:" + (event_obj.log_message.nil? ? "" : event_obj.log_message) )
  end
  tlist = @event_handlers[event_obj.class]
  return if tlist.nil?

  run_callbacks = proc {
    @unsubscribe_queue.each { |t|
      @tickets.delete(t)
      tlist.delete(t)
    }

    tlist.each { |t|
      ary = @tickets[t]
      c = ary[1]
      if c.nil?
        next
      end
      
      begin 
        c.call(event_obj)
      rescue => e
        Wakame.log.error(e)
        #raise e
      end
    }
    
    @unsubscribe_queue.each { |t|
      @tickets.delete(t)
      tlist.delete(t)
    }
  }

  #@handler_run_queue.push(run_handlers)
  
  StatusDB.barrier {
    begin 
      run_callbacks.call
    rescue => e
      Wakame.log.error(e)
    end
  }
end

#reset(event_class = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/wakame/event_dispatcher.rb', line 147

def reset(event_class=nil)
  if event_class.nil?
    @event_handlers.clear
    @tickets.clear
  else
    unless @event_handlers[event_class.to_s].nil?
      @event_handlers[event_class.to_s].each_key { |k|
        @tickets.delete(k)
      }
      @event_handlers[event_class.to_s].clear
    end
  end
end

#subscribe(event_class, *args, &blk) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wakame/event_dispatcher.rb', line 50

def subscribe(event_class, *args, &blk)
  event_class = case event_class
                when Class
                  event_class
                when String
                  Util.str2const(event_class)
                else
                  raise ArgumentError, "event_class has to be a form of String or Class type"
                end

  StatusDB.barrier {
    tlist = @event_handlers[event_class]
    if tlist.nil?
      tlist = @event_handlers[event_class] = []
    end
    
    tickets = []
    args.each { |o|
      tickets << Util.gen_id
      @tickets.store(tickets.last, [event_class, o])
      tlist << tickets.last
    }
    
    if blk
      tickets << Util.gen_id
      @tickets.store(tickets.last, [event_class, blk])
      tlist << tickets.last
    end
    
    # Return in array if num of ticket to be returned is more than or equal 2.
    tickets.size > 1 ? tickets : tickets.first
  }
end

#unsubscribe(ticket) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/wakame/event_dispatcher.rb', line 84

def unsubscribe(ticket)
  unless @tickets.has_key?(ticket)
    #Wakame.log.warn("EventHander.unsubscribe(#{ticket}) has been tried but the ticket was not registered.")
    return nil
  end
  
  StatusDB.barrier {
    Wakame.log.debug("#{self.class}.unsubscribe(#{ticket})")

    @unsubscribe_queue << ticket
    ticket
  }
end