resque-unique_by_arity 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f9ce19b6bc1fb732d1e65041619ab23e3ec260e
4
- data.tar.gz: 8378dafafc81c1fbc4f8cb8accc4ae21b9833604
3
+ metadata.gz: ac085f3b0b4e1bffe91a7d102a682d1b24c3a1aa
4
+ data.tar.gz: eacd68195490aee206b3d6c45af092bd00624ac1
5
5
  SHA512:
6
- metadata.gz: 354198da378d793f142af7ce1d4938af08544f8208cff62bca8af90460005a773f5205b212afdb7454aac20ebedf840588005ea3679f1ba8ebed05dc106de2b3
7
- data.tar.gz: b61e3cb714f9b19d8a52d8bd3285d0d47de32439858cf44e256758ef633378a2dfb06d153fe9b21469da0858a07b389f8d49501dcb9aa2fbd36e576442b56e97
6
+ metadata.gz: c9ff0d231d3cc0c06f9bc597783d3948035d8e66f5163c36437809a39dade1326412502b39d2443c437cc66ae33c1f223a751be7a326379ce4d4d88e36a6752d
7
+ data.tar.gz: fe7a66deceb8ed1fe40dc8b36e94d6e567cd46c896688e44ccb9e3ae4db070430643a35618867f2505d198b4f782090054505e77562211bda1de32f0fdfba6ae
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Resque::UniqueByArity
2
2
 
3
+ NOTE:
4
+
5
+ Requires the pboling fork of resque-lonely_job at https://github.com/pboling/resque-lonely_job as the standard one is just very incompatible with resque-solo (or vice versa, either direction is true; they step on each other).
6
+
3
7
  Usage:
4
8
 
5
9
  ```ruby
@@ -3,6 +3,22 @@ module Resque
3
3
  module CopModulizer
4
4
  def self.to_mod(configuration)
5
5
  Module.new do
6
+ if configuration.unique_in_queue || configuration.unique_at_runtime
7
+ # @return [Array<String, arguments>] the key base hash used to enforce uniqueness, and the arguments from the payload used to calculate it
8
+ define_method(:redis_unique_hash) do |payload|
9
+ payload = Resque.decode(Resque.encode(payload))
10
+ job = payload["class"]
11
+ args = payload["args"]
12
+ args.map! do |arg|
13
+ arg.is_a?(Hash) ? arg.sort : arg
14
+ end
15
+ # what is the configured arity for uniqueness?
16
+ # minus one because zero indexed
17
+ uniqueness_args = args[0..(configuration.arity_for_uniqueness - 1)]
18
+ args = { class: job, args: uniqueness_args }
19
+ return [Digest::MD5.hexdigest(Resque.encode(args)), uniqueness_args]
20
+ end
21
+ end
6
22
  if configuration.unique_in_queue
7
23
  # Returns a string, used by LonelyJob, that will be used as the redis key
8
24
  # The example in the readme is bad. The args passed to this method are like:
@@ -10,27 +26,18 @@ module Resque
10
26
  # This is identical to the version from Resque::Plugins::UniqueJob
11
27
  # and we do not want the version from Resque::Plugins::LonelyJob to be used.
12
28
  # Payload is what Resque stored for this job along with the job's class name:
13
- # a hash containing :class and :args
29
+ # a hash containing :class and :args
30
+ # @return [String] the key used to enforce uniqueness (at queue-time)
14
31
  define_method(:redis_key) do |payload|
15
- puts "overriding redis_key in #{self}"
16
- begin
17
- payload = Resque.decode(Resque.encode(payload))
18
- job = payload["class"]
19
- args = payload["args"]
20
- args.map! do |arg|
21
- arg.is_a?(Hash) ? arg.sort : arg
22
- end
23
- # what is the configured arity for uniqueness?
24
- # minus one because zero indexed
25
- uniqueness_args = args[0..(configuration.arity_for_uniqueness - 1)]
26
- args = { class: job, args: uniqueness_args }
27
- key = Digest::MD5.hexdigest Resque.encode(args)
28
- puts "redis key for uniqueness for #{args} is: #{key.green}"
29
- key
30
- rescue => e
31
- Raven.captureMessage("redis_key error", extra: { signature: payload, error: e.class.to_s, message: e.message })
32
- raise e
33
- end
32
+ unique_hash, args_for_uniqueness = redis_unique_hash(payload)
33
+ key = "unique_job:#{self}:#{unique_hash}"
34
+ puts "#{self}.redis_key for #{args_for_uniqueness} is: #{key.green}"
35
+ key
36
+ end
37
+ # @return [Fixnum] number of keys that were deleted
38
+ define_method(:purge_unique_job_redis_keys) do
39
+ keys = Resque.redis.keys("unique_job:#{self}:*")
40
+ Resque.redis.del keys if keys.length > 0
34
41
  end
35
42
  end
36
43
  if configuration.unique_at_runtime
@@ -40,9 +47,17 @@ module Resque
40
47
  # from resque_solo
41
48
  # Does not need any customization for arity, because it funnels down to redis_key,
42
49
  # and we handle the arity option there
50
+ # @return [String] the key used to enforce loneliness (uniqueness at runtime)
43
51
  define_method(:lonely_job_redis_key) do |*args|
44
- puts "overriding lonely_job_redis_key in #{self}"
45
- "lonely_job:#{redis_key({"class" => self.to_s, "args" => args})}"
52
+ unique_hash, args_for_uniqueness = redis_unique_hash({"class" => self.to_s, "args" => args})
53
+ key = "lonely_job:#{self}:#{unique_hash}"
54
+ puts "#{self}.lonely_job_redis_key for #{args_for_uniqueness} is: #{key.yellow}"
55
+ key
56
+ end
57
+ # @return [Fixnum] number of keys that were deleted
58
+ define_method(:purge_lonely_job_redis_keys) do
59
+ keys = Resque.redis.keys("lonely_job:#{self}:*")
60
+ Resque.redis.del keys if keys.length > 0
46
61
  end
47
62
  end
48
63
  end
@@ -1,5 +1,5 @@
1
1
  module Resque
2
2
  module UniqueByArity
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-unique_by_arity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Boling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-29 00:00:00.000000000 Z
11
+ date: 2017-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: resque-lonely_job