Class: ConsulStockpile::RunStockpile

Inherits:
Base
  • Object
show all
Defined in:
lib/consul_stockpile/run_stockpile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

call

Constructor Details

#initialize(bucket:, name:, verbose: false) ⇒ RunStockpile

Returns a new instance of RunStockpile.



13
14
15
16
17
# File 'lib/consul_stockpile/run_stockpile.rb', line 13

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

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



11
12
13
# File 'lib/consul_stockpile/run_stockpile.rb', line 11

def bucket
  @bucket
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/consul_stockpile/run_stockpile.rb', line 11

def name
  @name
end

#verboseObject

Returns the value of attribute verbose.



11
12
13
# File 'lib/consul_stockpile/run_stockpile.rb', line 11

def verbose
  @verbose
end

Instance Method Details

#callObject



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
56
57
58
59
60
61
62
63
# File 'lib/consul_stockpile/run_stockpile.rb', line 19

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
    while !DetectConsul.call.running
      Logger.warn 'Local consul agent not detected, sleeping for 5 seconds'
      sleep 5
    end

    backup_actor = BackupConsulKVActor.spawn(
      :backup_consul_kv,
      bucket: self.bucket,
      name: self.name
    )

    BootstrapConsulKVActor.spawn(
      :bootstrap_consul_kv,
      backup_actor: backup_actor,
      bucket: self.bucket
    )
    WatchEventActor.spawn(:watch_event, backup_actor: backup_actor)


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

  rescue Interrupt
    Logger.info 'Exiting'
    # actors are cleaned up in at_exit handler
    exit 0
  end
end

#handle_signal(sig) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/consul_stockpile/run_stockpile.rb', line 65

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