sidekiq-dynamic-queues 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ 0.5.6 (06/13/2013)
2
+ ------------------
3
+
4
+ do setup in config_server block to avoid loading of Fetch class when not running server <ed484a0> [Matt Conway]
5
+
1
6
  0.5.5 (06/13/2013)
2
7
  ------------------
3
8
 
data/README.md CHANGED
@@ -10,8 +10,12 @@ If creating a gem of your own that uses sidekiq-dynamic-queues, you may have to
10
10
 
11
11
  require 'sidekiq-dynamic-queues'
12
12
 
13
- Configure by setting Sidekiq.options[:fetch] = Sidekiq::DynamicQueues::Fetch
14
-
13
+ Configure by:
14
+
15
+ Sidekiq.configure_server do |config|
16
+ config.options[:fetch] = Sidekiq::DynamicQueues::Fetch
17
+ end
18
+
15
19
  Start your workers with a queue that can contain '\*' (.star.) for zero-or more of any character, '!' (.not.) to exclude the following pattern, or @key (.at.key) to look up the patterns from redis. The version in parens is required to get around the sidekiq cli's restriction on queue names. Some examples help:
16
20
 
17
21
  sidekiq -q foo
@@ -58,6 +58,59 @@ module Sidekiq
58
58
  result[FALLBACK_KEY] ||= ['*']
59
59
  return result
60
60
  end
61
+
62
+ # Returns a list of queues to use when searching for a job.
63
+ #
64
+ # A splat ("*") means you want every queue (in alpha order) - this
65
+ # can be useful for dynamically adding new queues.
66
+ #
67
+ # The splat can also be used as a wildcard within a queue name,
68
+ # e.g. "*high*", and negation can be indicated with a prefix of "!"
69
+ #
70
+ # An @key can be used to dynamically look up the queue list for key from redis.
71
+ # If no key is supplied, it defaults to the worker's hostname, and wildcards
72
+ # and negations can be used inside this dynamic queue list. Set the queue
73
+ # list for a key with
74
+ # Sidekiq::DynamicQueues::Attributes.set_dynamic_queue(key, ["q1", "q2"]
75
+ #
76
+ def expand_queues(queues)
77
+ queue_names = queues.dup
78
+
79
+ real_queues = Sidekiq::Client.registered_queues
80
+ matched_queues = []
81
+
82
+ while q = queue_names.shift
83
+ q = q.to_s
84
+
85
+ if q =~ /^(!)?@(.*)/
86
+ key = $2.strip
87
+ key = hostname if key.size == 0
88
+
89
+ add_queues = get_dynamic_queue(key)
90
+ add_queues.map! { |q| q.gsub!(/^!/, '') || q.gsub!(/^/, '!') } if $1
91
+
92
+ queue_names.concat(add_queues)
93
+ next
94
+ end
95
+
96
+ if q =~ /^!/
97
+ negated = true
98
+ q = q[1..-1]
99
+ end
100
+
101
+ patstr = q.gsub(/\*/, ".*")
102
+ pattern = /^#{patstr}$/
103
+ if negated
104
+ matched_queues -= matched_queues.grep(pattern)
105
+ else
106
+ matches = real_queues.grep(/^#{pattern}$/)
107
+ matches = [q] if matches.size == 0 && q == patstr
108
+ matched_queues.concat(matches)
109
+ end
110
+ end
111
+
112
+ return matched_queues.collect { |q| "queue:#{q}" }.uniq.sort
113
+ end
61
114
 
62
115
  end
63
116
 
@@ -1,9 +1,13 @@
1
- require 'forwardable'
1
+ require 'sidekiq/fetch'
2
2
 
3
3
  module Sidekiq
4
4
  module DynamicQueues
5
5
 
6
- # enable with Sidekiq.options[:fetch] = Sidekiq::DynamicQueues::Fetch
6
+ # enable with:
7
+ # Sidekiq.configure_server do |config|
8
+ # config.options[:fetch] = Sidekiq::DynamicQueues::Fetch
9
+ # end
10
+ #
7
11
  class Fetch < Sidekiq::BasicFetch
8
12
 
9
13
  include Sidekiq::Util
@@ -19,67 +23,13 @@ module Sidekiq
19
23
  if @dynamic_queues.grep(/(^!)|(^@)|(\*)/).size == 0
20
24
  super
21
25
  else
22
- queues = expanded_queues
26
+ queues = expand_queues(@dynamic_queues)
23
27
  queues = @strictly_ordered_queues ? queues : queues.shuffle
24
28
  queues << "queue:default" if queues.size == 0
25
29
  queues << Sidekiq::Fetcher::TIMEOUT
26
30
  end
27
31
  end
28
32
 
29
- # Returns a list of queues to use when searching for a job.
30
- #
31
- # A splat ("*") means you want every queue (in alpha order) - this
32
- # can be useful for dynamically adding new queues.
33
- #
34
- # The splat can also be used as a wildcard within a queue name,
35
- # e.g. "*high*", and negation can be indicated with a prefix of "!"
36
- #
37
- # An @key can be used to dynamically look up the queue list for key from redis.
38
- # If no key is supplied, it defaults to the worker's hostname, and wildcards
39
- # and negations can be used inside this dynamic queue list. Set the queue
40
- # list for a key with
41
- # Sidekiq::DynamicQueues::Attributes.set_dynamic_queue(key, ["q1", "q2"]
42
- #
43
- def expanded_queues
44
- queue_names = @dynamic_queues.dup
45
-
46
- real_queues = Sidekiq::Client.registered_queues
47
- matched_queues = []
48
-
49
- while q = queue_names.shift
50
- q = q.to_s
51
-
52
- if q =~ /^(!)?@(.*)/
53
- key = $2.strip
54
- key = hostname if key.size == 0
55
-
56
- add_queues = get_dynamic_queue(key)
57
- add_queues.map! { |q| q.gsub!(/^!/, '') || q.gsub!(/^/, '!') } if $1
58
-
59
- queue_names.concat(add_queues)
60
- next
61
- end
62
-
63
- if q =~ /^!/
64
- negated = true
65
- q = q[1..-1]
66
- end
67
-
68
- patstr = q.gsub(/\*/, ".*")
69
- pattern = /^#{patstr}$/
70
- if negated
71
- matched_queues -= matched_queues.grep(pattern)
72
- else
73
- matches = real_queues.grep(/^#{pattern}$/)
74
- matches = [q] if matches.size == 0 && q == patstr
75
- matched_queues.concat(matches)
76
- end
77
- end
78
-
79
- return matched_queues.collect { |q| "queue:#{q}" }.uniq.sort
80
- end
81
-
82
-
83
33
  def self.translate_from_cli(*queues)
84
34
  queues.collect do |queue|
85
35
  queue.gsub('.star.', '*').gsub('.at.', '@').gsub('.not.', '!')
@@ -22,9 +22,7 @@ module Sidekiq
22
22
  @queues = []
23
23
  dqueues = Attr.get_dynamic_queues
24
24
  dqueues.each do |k, v|
25
- fetch = Fetch.new(:queues => ["@#{k}"], :strict => true)
26
- expanded = fetch.queues_cmd
27
- expanded.pop
25
+ expanded = Attr.expand_queues(["@#{k}"])
28
26
  expanded = expanded.collect {|q| q.split(":").last }
29
27
  view_data = {
30
28
  'name' => k,
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module DynamicQueues
3
- VERSION = "0.5.5"
3
+ VERSION = "0.5.6"
4
4
  end
5
5
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: sidekiq-dynamic-queues
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.5
5
+ version: 0.5.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Matt Conway