Sha256: fca541d7f7f818ba16121532f502a03b8113ba2225de867798f7fd45152274d2

Contents?: true

Size: 1.84 KB

Versions: 5

Compression:

Stored size: 1.84 KB

Contents

require 'consul/async/utilities'
require 'consul/async/consul_endpoint'
require 'consul/async/consul_template'
require 'consul/async/consul_template_render'
require 'em-http'
require 'thread'
require 'erb'
module Consul
  module Async
    class ConsulTemplateEngine
      attr_reader :template_manager, :hot_reload_failure
      attr_writer :hot_reload_failure
      def initialize
        @templates = []
        @template_callbacks = []
        @hot_reload_failure = 'die'
      end

      def add_template_callback(&block)
        @template_callbacks << block
      end

      def add_template(source, dest)
        @templates.push([source, dest])
      end

      def run(template_manager)
        @template_manager = template_manager
        EventMachine.run do
          template_renders = []
          @templates.each do |template_file, output_file|
            template_renders << Consul::Async::ConsulTemplateRender.new(template_manager, template_file, output_file,
                                                                        hot_reload_failure: hot_reload_failure)
          end
          EventMachine.add_periodic_timer(1) do
            begin
              results = template_renders.map(&:run)
              all_ready = results.reduce(true) { |a, e| a && e.ready? }
              begin
                @template_callbacks.each do |c|
                  c.call([all_ready, template_manager, results])
                end
              rescue StandardError => cbk_error
                STDERR.puts "Error in callback: #{cbk_error.inspect}"
                raise cbk_error
              end
            rescue StandardError => e
              STDERR.puts "[ERROR] Fatal error occured: #{e.inspect} - #{e.backtrace}"
              template_manager.terminate
              EventMachine.stop
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
consul-templaterb-1.0.7 lib/consul/async/consul_template_engine.rb
consul-templaterb-1.0.6 lib/consul/async/consul_template_engine.rb
consul-templaterb-1.0.5 lib/consul/async/consul_template_engine.rb
consul-templaterb-1.0.4 lib/consul/async/consul_template_engine.rb
consul-templaterb-1.0.3 lib/consul/async/consul_template_engine.rb