Sha256: 62ea3b0e043379c88494178ec9813c363933e46b7cf3b78eed538e9337e65619

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module CottonTail
  # App is the main class for a CottonTail server
  class App
    def initialize(queue_strategy: Queue::Bunny, routing_strategy: Router)
      @dependencies = {
        queue_strategy: queue_strategy,
        routing_strategy: routing_strategy
      }
    end

    # Define message routing
    def define(&block)
      @definition = DSL::App.new(**@dependencies)
      @definition.instance_eval(&block)
      self
    end

    def queues
      @definition.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.1 while running?
    end

    private

    def supervisors
      @supervisors ||= queues.map do |_name, queue|
        Queue::Supervisor.new(queue, on_message: @definition.router)
      end
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cotton-tail-0.1.1 lib/cotton_tail/app.rb