Class: DockerBoss::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_boss/engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, config) ⇒ Engine

Returns a new instance of Engine.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/docker_boss/engine.rb', line 11

def initialize(options, config)
  @containers = []
  @options = options
  @config = config
  @mutex = Mutex.new
  @last_etcds
  @modules = []

  @config.each do |k,v|
    @modules << DockerBoss::ModuleManager[k].new(v)
  end
end

Instance Method Details

#event_loopObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/docker_boss/engine.rb', line 79

def event_loop
  @events = Queue.new
  threads = []
  threads << Thread.new do
    loop do
      event = @events.deq
      process_event(event)
    end
  end

  threads << Thread.new do
    loop do
      begin
        #Docker::Event.stream({}, Docker::Connection.new(Docker.url, {:nonblock => true})) do |event|
        Docker::Event.stream do |event|
          DockerBoss.logger.debug "New event on socket: #{event}"
          @events.enq({:id => event.id, :status => event.status})
        end
      rescue Docker::Error::TimeoutError
        next
      end
    end
  end

  @modules.each do |mod|
    begin
      threads << mod.run
    rescue NoMethodError
    end
  end

  ThreadsWait.new(*threads)
end

#process_event(event) ⇒ Object



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
# File 'lib/docker_boss/engine.rb', line 51

def process_event(event)
  DockerBoss.logger.info "Processing event: #{event}"
  case event[:status]
  when 'start' # 'create' also triggers 'start'
    @mutex.synchronize {
      if @options[:incr_refresh]
        new_container = Docker::Container.get(event[:id]).json
        @containers.delete_if { |c| c['Id'] == event[:id] }
        @containers << xform_container(new_container)
      else
        refresh_all
      end
      trigger(event[:id])
    }
  when 'die' # 'destroy', 'kill', 'stop' also trigger 'die'
    @mutex.synchronize {
      if @options[:incr_refresh]
        @containers.delete_if { |c| c['Id'] == event[:id] }
      else
        refresh_all
      end
      trigger(event[:id])
    }
  when 'pause'
  when 'unpause'
  end
end

#refresh_allObject



30
31
32
# File 'lib/docker_boss/engine.rb', line 30

def refresh_all
  @containers = Docker::Container.all.map { |c| xform_container(c.json) }
end

#refresh_and_triggerObject



34
35
36
37
38
39
# File 'lib/docker_boss/engine.rb', line 34

def refresh_and_trigger
  @mutex.synchronize {
    refresh_all
    trigger
  }
end

#trigger(id = nil) ⇒ Object



24
25
26
27
28
# File 'lib/docker_boss/engine.rb', line 24

def trigger(id = nil)
  @modules.each do |mod|
    mod.trigger(@containers, id)
  end
end

#xform_container(container) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/docker_boss/engine.rb', line 41

def xform_container(container)
  new_env = {}
  container['Config']['Env'].each do |env|
    (k,v) = env.split('=', 2)
    new_env[k] = v || true
  end
  container['Config']['Env'] = new_env
  container
end