Sha256: 0bc7431a5bf2e41bb85131df5c92d547d5cb003707e0b76c83179014d549633b

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

require 'resque/worker'

module Resque
  module Plugins
    module QueuePriority

      module Priority

        def self.included(klass)
          klass.instance_eval do
            alias_method :queues_without_priority, :queues
            alias_method :queues, :queues_with_priority
          end
        end

        def queues_with_priority
          all_queues = queues_without_priority
          result = []
          default_idx = -1, default_fairly = false;

          # Walk the priority patterns, extract each into its own bucket
          buckets = Resque.priority_buckets
          buckets.each do |bucket|
            bucket_pattern = bucket['pattern']
            fairly = bucket['fairly']

            # note the position of the default bucket for inserting the remaining queues at that location
            if bucket_pattern == 'default'
              default_idx = result.size
              default_fairly = fairly
              next
            end

            patstr = bucket_pattern.gsub(/\*/, ".*")
            pattern = /^#{patstr}$/
            bucket_queues, remaining = all_queues.partition {|q| q =~ pattern }

            bucket_queues.shuffle! if fairly
            all_queues = remaining

            result << bucket_queues
          end

          # insert the remaining queues at the position the default item was at (or last)
          all_queues.shuffle! if default_fairly
          result.insert(default_idx, all_queues)
          result.flatten!

          return result
        end

      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
resque-queue-priority-0.5.3 lib/resque/plugins/queue_priority/priority.rb
resque-queue-priority-0.5.2 lib/resque/plugins/queue_priority/priority.rb
resque-queue-priority-0.5.1 lib/resque/plugins/queue_priority/priority.rb
resque-queue-priority-0.5.0 lib/resque/plugins/queue_priority/priority.rb