Module: Resque::Plugins::QueuePriority::Priority

Defined in:
lib/resque/plugins/queue_priority/priority.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



9
10
11
12
13
14
# File 'lib/resque/plugins/queue_priority/priority.rb', line 9

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

Instance Method Details

#queues_with_priorityObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/resque/plugins/queue_priority/priority.rb', line 16

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

    bucket_queues, remaining = [], []
    
    patterns = bucket_pattern.split(',')
    patterns.each do |pattern|
      pattern = pattern.strip
      
      if pattern =~ /^!/
        negated = true
        pattern = pattern[1..-1]
      end
      
      patstr = pattern.gsub(/\*/, ".*")
      pattern = /^#{patstr}$/
    
    
      if negated
        bucket_queues -= bucket_queues.grep(pattern)
      else
        bucket_queues.concat(all_queues.grep(pattern))
      end
    
    end
    
    bucket_queues.uniq!
    bucket_queues.shuffle! if fairly
    all_queues = all_queues - bucket_queues
    
    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