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



36
37
38
# File 'lib/kontena/callback.rb', line 36

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
# 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_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_type|
    Kontena::Callback.callbacks[cmd_class] ||= {}
    Kontena::Callback.callbacks[cmd_class][cmd_type] ||= []
    Kontena::Callback.callbacks[cmd_class][cmd_type] << self
  end
end

.run_callbacks(cmd_type, state, obj) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kontena/callback.rb', line 40

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)
            ENV["DEBUG"] && puts("Execution aborted by #{klass}")
            exit 1
          end
        end
      end
    end
  end
end