Module: Consul::Template::Generator::CMD

Includes:
Consul::Template::Generator
Defined in:
lib/consul/template/generator/cmd.rb

Constant Summary

Constants included from Consul::Template::Generator

VERSION

Class Method Summary collapse

Class Method Details

.configure(consul_host, template, template_key, log_level, proxy = nil) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/consul/template/generator/cmd.rb', line 9

def self.configure(consul_host, template, template_key, log_level, proxy = nil)
  Consul::Template::Generator.configure do |config|
    config.log_level = log_level
    config.template = template
    config.template_key = template_key
    config.consul_host = consul_host
  end
end

.run(cycle_sleep = nil, lock_sleep = nil) ⇒ Object



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
56
57
58
# File 'lib/consul/template/generator/cmd.rb', line 18

def self.run(cycle_sleep = nil, lock_sleep = nil)
  cycle_sleep ||= 0.5
  lock_sleep ||= 1.0
  config = Consul::Template::Generator.config
  uploaded_hash = nil
  begin
    runner = CTRunner.new
    runner.acquire_session_lock do
      config.logger.info "Session lock acquired..."
      begin
        uploaded_hash = runner.run(uploaded_hash) || uploaded_hash
        sleep cycle_sleep
      rescue Interrupt
        raise # Re-raise to break this rescue block
      rescue ConsulSessionExpired
        config.logger.error "The current consul session has expired."
        break
      rescue Exception => e
        config.logger.error "An error occurred while updating template: #{e.message}"
        config.logger.debug "Sleeping before attempting to update again..."
        sleep lock_sleep
        break
      end until false
    end
  rescue Interrupt
    config.logger.error "Received interrupt signal, exiting..."
    break
  rescue Exception => e
    config.logger.info "Unable to obtain session lock: #{e.message}"
    config.logger.debug "Sleeping before attempting lock session again..."
    begin
      sleep lock_sleep
    rescue Interrupt
      config.logger.error "Received interrupt signal, exiting..."
      break
    end
  ensure
    runner.destroy_session
  end until false
  0
end

.run_onceObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/consul/template/generator/cmd.rb', line 60

def self.run_once
  config = Consul::Template::Generator.config
  begin
    runner = CTRunner.new
    result = runner.run
  rescue Exception => e
    config.logger.error "An unexpected error occurred, unable to process template: #{e.message}"
    1
  else
    0
  end 
end