Sha256: 1a7653b52c3607ce06726c81461aff0a824e1b73b465afcf0394936bcf97aee9

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

module Harmoniser
  class Launcher
    def initialize(configuration:, logger:)
      @configuration = configuration
      @logger = logger
    end

    def start
      boot_app
      start_subscribers
    end

    private

    def boot_app
      if File.directory?(@configuration.require)
        load_rails
      else
        load_file
      end
    end

    # TODO - Frameworks like Rails which have autoload for development/test will not start any subscriber unless the files where subscribers are located are required explicitly. Since we premier production and the eager load ensures that every file is loaded, this approach works
    def start_subscribers
      klasses = Subscriber.harmoniser_included
      klasses.each do |klass|
        klass.harmoniser_subscriber_start
      end
      @logger.info("Subscribers registered to consume messages from queues: klasses = `#{klasses}`")
    end

    private

    def load_rails
      filepath = File.expand_path("#{@configuration.require}/config/environment.rb")
      require filepath
    rescue LoadError
      @logger.warn("Error while requiring file within directory. No subscribers will run for this process: require = `#{@configuration.require}`, filepath = `#{filepath}`")
    end

    def load_file
      require @configuration.require
    rescue LoadError
      @logger.warn("Error while requiring file. No subscribers will run for this process: require = `#{@configuration.require}`")
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
harmoniser-0.8.1 lib/harmoniser/launcher.rb
harmoniser-0.8.0 lib/harmoniser/launcher.rb
harmoniser-0.7.0 lib/harmoniser/launcher.rb
harmoniser-0.6.0 lib/harmoniser/launcher.rb
harmoniser-0.5.0 lib/harmoniser/launcher.rb