Asynchronous class evaluation with queue-based system in Ruby

Ruby has several good-looking schedulers available. From its standard library, the Thread and Fiber classes are fine, but really raw ; but this is a pretty good base to create a queue-based system.

Here is the initial problem

Potential solution

dry-event is a Ruby library based on standard Thread one. It allows us to create several queues, subscribe to them and publish new messages on it. Since each queue is theoretically asynchronous from the other ones, we can use this mechanism to define priorities and implementation logic.

require "dry/events/publisher"

class Queues
  include Dry::Events::Publisher[:queue]

  def initialize
    @queues = %w(queue1 queue2 queue3)
    _create_queues
    _listen_queues
  end

  def _create_queues
    @queues.each do |queue|
      register_event(queue)
    end
  end

  def _listen_queues
     @queues.each do |queue|
        subscribe(queue) do |event|
          puts "%s | (%s) : %s" % [
            Time.now.to_s, event.id, event.payload.to_s]
        end
      end
    end
end