Class: Kontena::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/callback.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Callback

Returns a new instance of Callback.



5
6
7
# File 'lib/kontena/callback.rb', line 5

def initialize(command)
  @command = command
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/kontena/callback.rb', line 3

def command
  @command
end

Class Method Details

.callbacksObject



39
40
41
# File 'lib/kontena/callback.rb', line 39

def self.callbacks
  @@callbacks ||= {}
end

.matches_commands(*commands) ⇒ Object

Register callback for command types it is supposed to run with.



10
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
# File 'lib/kontena/callback.rb', line 10

def self.matches_commands(*commands)
  cmd_types = {}

  commands.each do |cmd|
    cmd_class, cmd_type = cmd.split(' ', 2)

    if cmd_class == '*'
      cmd_class = :all
    end

    if cmd_type.nil? || cmd_type == '*'
      cmd_type = :all
    else
      cmd_type = cmd_type.to_sym
    end
    cmd_types[cmd_class.to_sym] ||= []
    cmd_types[cmd_class.to_sym] << cmd_type
  end

  # Finally it should be normalized into a hash that looks like :cmd_class => :cmd_type, :app => :init, :grid => :all
  cmd_types.each do |cmd_class, cmd_types|
    cmd_types.each do |cmd_type|
      Kontena::Callback.callbacks[cmd_class] ||= {}
      Kontena::Callback.callbacks[cmd_class][cmd_type] ||= []
      Kontena::Callback.callbacks[cmd_class][cmd_type] << self
    end
  end
end

.run_callbacks(cmd_type, state, obj) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kontena/callback.rb', line 43

def self.run_callbacks(cmd_type, state, obj)
  [cmd_type.last, :all].compact.uniq.each do |cmdtype|
    [cmd_type.first, :all].compact.uniq.each do |cmdclass|
      callbacks.fetch(cmdclass, {}).fetch(cmdtype, []).each do |klass|
        if klass.instance_methods.include?(state)
          cb = klass.new(obj)
          if cb.send(state).kind_of?(FalseClass)
            Kontena.logger.debug { "Execution aborted by #{klass}" }
            exit 1
          end
        end
      end
    end
  end
end