Sha256: 974130abf6258cbb5eb131ea4c817b896141986945ef1f8aa9febb79aece64dd

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require "resque"
require "resqueue-metadata"

module Resque
  class PickyWorker < Worker
    Error = Class.new(StandardError)

    class << self
      attr_accessor :default_max_workers
      # Something that responds to #call (eg. Proc) and takes a single argument - array of queue names
      # Should return a string which is a valid queue name
      attr_accessor :chooser
    end
    self.default_max_workers ||= 5

    def self.chooser
      @chooser || raise(Error, "chooser must be specified or I cannot choose a queue to listen to")
    end

    # Returns a string (name of a queue)
    def self.pick_queue
      cue = chooser.call(Resque.queues)
      raise(Error, "chooser returned an invalid queue name") unless Resque.queues.include?(cue)
      cue
    end

    def self.queue_max name
      (Resque::Queue::Metadata.new(name)["max_workers"] || default_max_workers).to_i
    end
    
    def self.queue_current name
      all.select {|worker| worker.queues.include?(name) }.size
    end

    # Invoke the worker class with a randomly picked queue
    def initialize
      super(PickyWorker.pick_queue)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
resque-picky_worker-0.0.3 lib/resque/picky_worker.rb
resque-picky_worker-0.0.2 lib/resque/picky_worker.rb
resque-picky_worker-0.0.1 lib/resque/picky_worker.rb