Class: Vetinari::CallbackContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/vetinari/callback_container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot) ⇒ CallbackContainer

Returns a new instance of CallbackContainer.



5
6
7
8
9
# File 'lib/vetinari/callback_container.rb', line 5

def initialize(bot)
  @bot = bot
  @callbacks = Hash.new { |hash, key| hash[key] = {} }
  @mutex = Mutex.new
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



3
4
5
# File 'lib/vetinari/callback_container.rb', line 3

def bot
  @bot
end

Instance Method Details

#add(event, options, proc) ⇒ Object



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
# File 'lib/vetinari/callback_container.rb', line 11

def add(event, options, proc)
  worker = Integer(options.delete(:worker) || 0)
  uuid = Celluloid.uuid
  args = [event, options, proc, self, uuid]

  case
  when worker == 1
    callback = Callback.new(*args)
    synchronicity = :async
  when worker > 1
    callback = Callback.pool(:size => worker, :args => args)
    synchronicity = :async
  else
    callback = Callback.new(*args)
    synchronicity = :sync
  end

  @mutex.synchronize do
    @callbacks[callback.event][uuid] = {
      :callback      => callback,
      :synchronicity => synchronicity
    }
  end

  callback
end

#call(env) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vetinari/callback_container.rb', line 64

def call(env)
  callbacks = nil

  @mutex.synchronize do
    if @callbacks.key?(env[:type])
      callbacks = @callbacks[env[:type]].values.dup
    end
  end

  if callbacks
    callbacks.each do |hash|
      case hash[:synchronicity]
      when :sync
        hash[:callback].call(env)
      when :async
        hash[:callback].async.call(env)
      end
    end
  end
end

#inspectObject



85
86
87
# File 'lib/vetinari/callback_container.rb', line 85

def inspect
  "#<CallbackContainer bot=#{@bot.inspect}>"
end

#remove(event, uuid, terminate = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vetinari/callback_container.rb', line 38

def remove(event, uuid, terminate = false)
  @mutex.synchronize do
    if @callbacks.key?(event)
      hash = @callbacks[event].delete(uuid)

      if hash
        if terminate && hash[:callback].alive?
          hash[:callback].async.terminate
        end

        # https://github.com/celluloid/celluloid/issues/197
        # callback.soft_terminate

        # #terminate is broken for Pools:
        # https://github.com/celluloid/celluloid/pull/207
        #
        # So, don't terminate for now.
        # hash[:callback].terminate
        return true
      end
    end
  end

  false
end

#terminate_callbacksObject



89
90
91
92
93
94
95
96
# File 'lib/vetinari/callback_container.rb', line 89

def terminate_callbacks
  @callbacks.each do |event, hash|
    hash.each do |uuid, _hash|
      actor = _hash[:callback]
      actor.terminate if actor.alive?
    end
  end
end