Sha256: c3b4b86393288e4f56a8e255aebddbdd41f278d9f29e4798ea9667081330f518

Contents?: true

Size: 1.08 KB

Versions: 2

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.01 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

2 entries across 2 versions & 1 rubygems

Version Path
cotton-tail-0.2.1 lib/cotton_tail/app.rb
cotton-tail-0.2.0 lib/cotton_tail/app.rb