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
Class Method Summary collapse
- .configure(consul_host, templates, session_key, log_level, graphite_host = nil, graphite_paths = nil, diff_changes = false, session_ttl = nil) ⇒ Object
- .configure_signal_handlers ⇒ Object
- .run(cycle_sleep = nil, lock_sleep = nil) ⇒ Object
- .run_once ⇒ Object
Class Method Details
.configure(consul_host, templates, session_key, log_level, graphite_host = nil, graphite_paths = nil, diff_changes = false, session_ttl = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/consul/template/generator/cmd.rb', line 10 def configure(consul_host, templates, session_key, log_level, graphite_host = nil, graphite_paths = nil, diff_changes = false, session_ttl = nil) Consul::Template::Generator.configure do |config| config.log_level = log_level config.templates = templates config.session_key = session_key config.session_ttl = session_ttl || 30 config.consul_host = consul_host config.graphite_host = graphite_host config.graphite_paths = graphite_paths || {} config.diff_changes = diff_changes end @config = Consul::Template::Generator.config end |
.configure_signal_handlers ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/consul/template/generator/cmd.rb', line 24 def configure_signal_handlers Signal.trap("INT") do @config.logger.error "Received INT signal..." @terminated = true @interrupted = true end Signal.trap("TERM") do @config.logger.error "Received TERM signal..." @terminated = true @interrupted = true end end |
.run(cycle_sleep = nil, lock_sleep = nil) ⇒ Object
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/consul/template/generator/cmd.rb', line 37 def run(cycle_sleep = nil, lock_sleep = nil) configure_signal_handlers @terminated = false cycle_sleep ||= 0.5 lock_sleep ||= 1.0 uploaded_hashes = {} runner = CTRunner.new nil, false begin runner.create_session @interrupted = false runner.acquire_session_lock do @config.logger.info "Session lock acquired..." begin @config.templates.each do |template,template_key| new_hash = runner.run(template, template_key, uploaded_hashes[template], @config.diff_changes) unless new_hash.nil? uploaded_hashes[template] = new_hash if @config.graphite_paths.include? template runner.post_graphite_event @config.graphite_paths[template] end end sleep cycle_sleep end 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.}" @config.logger.debug "Sleeping before attempting to update again..." sleep lock_sleep break end until @interrupted end rescue Exception => e @config.logger.info "Unable to obtain session lock: #{e.}" @config.logger.debug "Sleeping before attempting lock session again..." sleep lock_sleep ensure runner.destroy_session end until @terminated 0 end |
.run_once ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/consul/template/generator/cmd.rb', line 80 def run_once begin @config.templates.each do |template,template_key| runner = CTRunner.new result = runner.run(template, template_key, nil, @config.diff_changes) unless result.nil? if @config.graphite_paths.include? template runner.post_graphite_event @config.graphite_paths[template] end end end rescue Exception => e @config.logger.error "An unexpected error occurred, unable to process template: #{e.}" 1 else 0 end end |