Class: ConsulBridge::RunBridge

Inherits:
Base
  • Object
show all
Defined in:
lib/consul_bridge/run_bridge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

call

Constructor Details

#initialize(bucket:, container_name:, join_all: false, verbose: false) ⇒ RunBridge



10
11
12
13
14
15
# File 'lib/consul_bridge/run_bridge.rb', line 10

def initialize(bucket:, container_name:, join_all: false, verbose: false)
  self.bucket = bucket
  self.container_name = container_name
  self.join_all = join_all
  self.verbose = verbose
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



8
9
10
# File 'lib/consul_bridge/run_bridge.rb', line 8

def bucket
  @bucket
end

#container_nameObject

Returns the value of attribute container_name.



8
9
10
# File 'lib/consul_bridge/run_bridge.rb', line 8

def container_name
  @container_name
end

#join_allObject

Returns the value of attribute join_all.



8
9
10
# File 'lib/consul_bridge/run_bridge.rb', line 8

def join_all
  @join_all
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/consul_bridge/run_bridge.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/consul_bridge/run_bridge.rb', line 17

def call
  Concurrent.use_stdlib_logger(Logger::DEBUG) if self.verbose

  self_read, self_write = IO.pipe
  %w(INT TERM).each do |sig|
    begin
      trap sig do
        self_write.puts(sig)
      end
    rescue ArgumentError
      puts "Signal #{sig} not supported"
    end
  end

  begin
    bootstrap_actor = BootstrapConsulActor.spawn(
      :bootstrap_consul,
      bucket: self.bucket,
      join_all: self.join_all
    )
    bootstrap_actor << :bootstrap

    MonitorDockerEventsActor.spawn(
      :monitor_docker_events,
      bootstrap_actor: bootstrap_actor,
      container_name: container_name
    )

    while readable_io = IO.select([self_read])
      signal = readable_io.first[0].gets.strip
      handle_signal(signal)
    end

  rescue Interrupt
    puts 'Exiting'
    # actors are cleaned up in at_exit handler
    exit 0
  end
end

#handle_signal(sig) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/consul_bridge/run_bridge.rb', line 57

def handle_signal(sig)
  case sig
  when 'INT'
    raise Interrupt
  when 'TERM'
    raise Interrupt
  end
end