Sha256: e273f5eee013b9f3b1806c44a02f9894735bbe0ede5b6f0f75733ca5e178def3

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module CottonTail
  # App is the main class for a CottonTail server
  class App
    attr_reader :env

    def initialize(queue_strategy: Queue::Bunny, env: {}, connection: Bunny.new)
      @dependencies = { queue_strategy: queue_strategy, connection: connection }
      @env = env
      @connection = connection.start
    end

    def config
      @config ||= Configuration.new(middleware: Middleware.default_stack(self))
    end

    def queues
      routes.queues
    end

    # Get a single message queue
    def queue(name)
      queues[name]
    end

    # Start the app, process all pending messages, and then shutdown
    def run
      supervisors.map(&:run).each(&:join)
    end

    def start
      supervisors.map(&:start)
      puts 'Waiting for messages ...'

      sleep 0.01 while running?
    end

    def routes
      @routes ||= DSL::Routes.new(**@dependencies)
    end

    private

    def supervisors
      @supervisors ||= queues.map do |_name, queue|
        Queue::Supervisor.new(queue, app: self)
      end
    end

    def running?
      supervisors.any?(&:running?)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cotton-tail-0.6.1 lib/cotton_tail/app.rb
cotton-tail-0.6.0 lib/cotton_tail/app.rb
cotton-tail-0.5.0 lib/cotton_tail/app.rb
cotton-tail-0.4.1 lib/cotton_tail/app.rb
cotton-tail-0.4.0 lib/cotton_tail/app.rb