Skip to content

Commit

Permalink
Merge pull request #7 from mtsmfm/wait
Browse files Browse the repository at this point in the history
Wait until yaichi container is appeared in docker ps
  • Loading branch information
mtsmfm authored Oct 18, 2017
2 parents eea95d8 + 721ecea commit f33722b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
steps:
- checkout
- setup_remote_docker:
version: 17.06.0-ce
version: 17.07.0-ce
- run: |
rm docker-compose.override.yml
bin/test
4 changes: 2 additions & 2 deletions conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ http {
mruby_server_context_handler_code '
s = Nginx::Server.new
Docker::Container.me.exposed_ports.map(&:last).each do |port|
Docker::Container.me!.exposed_ports.map(&:last).each do |port|
s.add_listener({address: port.to_s})
end
';
Expand All @@ -35,7 +35,7 @@ http {
mruby_server_context_handler_code '
s = Nginx::Server.new
Docker::Container.me.exposed_ports.map(&:last).each do |port|
Docker::Container.me!.exposed_ports.map(&:last).each do |port|
s.add_listener({address: port.to_s})
end
';
Expand Down
4 changes: 2 additions & 2 deletions hook/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
r.content_type = "text/html"

Docker::Container.expire_cache!
me = Docker::Container.me
me = Docker::Container.me!
containers = Docker::Container.all - [me]

not_connected_networks = (containers.flat_map(&:networks) - me.networks)
Expand All @@ -11,7 +11,7 @@
end
if not_connected_networks.any?
Docker::Container.expire_cache!
me = Docker::Container.me
me = Docker::Container.me!
containers = Docker::Container.all - [me]
end
containers = containers.sort_by(&:name)
Expand Down
4 changes: 2 additions & 2 deletions hook/proxy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
r = Nginx::Request.new
c = Nginx::Connection.new
me = Docker::Container.me
me = Docker::Container.me!

if container = Docker::Container.find_by_fqdn(r.hostname)
not_connected_networks = (container.networks - me.networks)
Expand All @@ -9,7 +9,7 @@
end
if not_connected_networks.any?
Docker::Container.expire_cache!
me = Docker::Container.me
me = Docker::Container.me!
container = Docker::Container.find_by_fqdn(r.hostname)
end

Expand Down
20 changes: 18 additions & 2 deletions mrbgem/mrblib/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def connect(container)
end

class Container
class NotFound < StandardError; end

class << self
def all
@all ||= begin
Expand All @@ -39,8 +41,16 @@ def my_id
@my_id ||= `hostname`.chomp
end

def me
all.find {|c| c.id.start_with?(my_id) }
def me!(timeout_sec = 3)
return me if me

timeout_sec.times do
sleep 1
expire_cache!
return me if me
end

raise NotFound
end

def find_by_fqdn(fqdn)
Expand All @@ -50,6 +60,12 @@ def find_by_fqdn(fqdn)
def expire_cache!
@all = nil
end

private

def me
all.find {|c| c.id.start_with?(my_id) }
end
end

include Identifiable
Expand Down

0 comments on commit f33722b

Please sign in to comment.