resque-unique_in_queue 1.0.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e2b27c8cf85da28ed578801b934b56a5865ea25518013b904379faf98f5ee7c
4
- data.tar.gz: 2b4cc6f023e5b6c54fe80f5b0adbb026fc93e8510830e5b39f129272d5f3d95d
3
+ metadata.gz: 48f097b477503dbe4a6028c2587476e1a0bc45f53627d77aa3a2dc0b461edf59
4
+ data.tar.gz: b2134a95053feb956cba28906cf98aad1158556ce75c486a98ee5cd243f58301
5
5
  SHA512:
6
- metadata.gz: 111c91bbb3c9fd405c528c95276e548355a144d54e220f8889866664c67adeea634438b8ed268d1b1fab9901b777612449681c76bcbe1d18f35323ec4efadc92
7
- data.tar.gz: 7a1757c1e3ff7d3c56bccaddc14b38b635fe57b3fdcd6dc213b4e08864d85b1897122d081708c4c3e6185918599ae22b3bc7ceb57407ddb49314f78670f7e163
6
+ metadata.gz: f5e2e3bb2738e06343e50a2ff81f9fc5075227f5ed2f5ba8b550173056b90fe4e0a095334a61347b04e5520ec9df88ab06e022879cebf1cf7b00a380fecf5fbc
7
+ data.tar.gz: 020f470aa13f3c8a4f3b3575d98cdd27e87983422f73ee7e91dee2e53bdd95be98e58582b978d0eb91fed89acf33bf925a5cab84ba3319b1ba3f31620cd034e7
data/README.md CHANGED
@@ -33,6 +33,26 @@ gem 'resque-unique_in_queue'
33
33
 
34
34
  ## Usage
35
35
 
36
+ `resque-unique_in_queue` utilizes 3 class instance variables that can be set
37
+ in your Jobs, in addition to the standard `@queue`. Here they are, with their
38
+ default values:
39
+
40
+ ```ruby
41
+ @lock_after_execution_period = 0
42
+ @ttl = -1
43
+ @unique_in_queue_key_base = 'r-uiq'.freeze
44
+ ```
45
+
46
+ The last one, in normal circumstances, shouldn't be set as different per class,
47
+ or uniqueness cleanup becomes more difficult.
48
+
49
+ It should be set only once, globally:
50
+
51
+ ```ruby
52
+ Resque::UniqueInQueue.configuration.unique_in_queue_key_base = 'my-custom'
53
+ ```
54
+
55
+
36
56
  ```ruby
37
57
  class UpdateCat
38
58
  include Resque::Plugins::UniqueInQueue
@@ -25,62 +25,27 @@ module Resque
25
25
  module UniqueInQueue
26
26
  PLUGIN_TAG = (ColorizedString['[R-UIQ] '].blue).freeze
27
27
 
28
- def in_queue_unique_log(message, config_proxy = nil)
29
- config_proxy ||= uniqueness_configuration
30
- config_proxy.unique_logger&.send(config_proxy.unique_log_level, message) if config_proxy.unique_logger
28
+ def log(message)
29
+ configuration.logger&.send(configuration.log_level, message) if configuration.logger
31
30
  end
32
31
 
33
- def in_queue_unique_debug(message, config_proxy = nil)
34
- config_proxy ||= uniqueness_configuration
35
- config_proxy.unique_logger&.debug("#{PLUGIN_TAG}#{message}") if config_proxy.debug_mode
36
- end
37
-
38
- # There are times when the class will need access to the configuration object,
39
- # such as to override it per instance method
40
- def uniq_config
41
- @uniqueness_configuration
32
+ def debug(message)
33
+ configuration.logger&.debug("#{PLUGIN_TAG}#{message}") if configuration.debug_mode
42
34
  end
43
35
 
44
36
  # For per-class config with a block
45
- def uniqueness_configure
46
- @uniqueness_configuration ||= Configuration.new
47
- yield(@uniqueness_configuration)
37
+ def configure
38
+ yield(@configuration)
48
39
  end
49
40
 
50
41
  #### CONFIG ####
51
42
  class << self
52
- attr_accessor :uniqueness_configuration
53
- end
54
- def uniqueness_config_reset(config = Configuration.new)
55
- @uniqueness_configuration = config
56
- end
57
-
58
- def uniqueness_log_level
59
- @uniqueness_configuration.log_level
60
- end
61
-
62
- def uniqueness_log_level=(log_level)
63
- @uniqueness_configuration.log_level = log_level
64
- end
65
-
66
- def unique_in_queue_key_base
67
- Configuration.unique_in_queue_key_base
68
- end
69
-
70
- def unique_in_queue_key_base=(key_base)
71
- Configuration.unique_in_queue_key_base = key_base
43
+ attr_accessor :configuration
72
44
  end
73
45
 
74
- self.uniqueness_configuration = Configuration.new # setup defaults
46
+ self.configuration = Configuration.instance # setup defaults
75
47
 
76
- module_function(:in_queue_unique_log,
77
- :in_queue_unique_debug,
78
- :uniq_config,
79
- :uniqueness_configure,
80
- :uniqueness_config_reset,
81
- :uniqueness_log_level,
82
- :uniqueness_log_level=,
83
- :unique_in_queue_key_base,
84
- :unique_in_queue_key_base=)
48
+ module_function(:log,
49
+ :debug)
85
50
  end
86
51
  end
@@ -35,36 +35,40 @@ module Resque
35
35
  Digest::MD5.hexdigest Resque.encode(class: job, args: args)
36
36
  end
37
37
 
38
- # The default ttl of a locking key is -1 (forever).
39
- # To expire the lock after a certain amount of time, set a ttl (in seconds).
38
+ # The default ttl of a persisting key is 0, i.e. immediately deleted.
39
+ # Set lock_after_execution_period to block the execution
40
+ # of the job for a certain amount of time (in seconds).
40
41
  # For example:
41
42
  #
42
43
  # class FooJob
43
44
  # include Resque::Plugins::UniqueInQueue
44
- # @ttl = 40
45
+ # @lock_after_execution_period = 40
45
46
  # end
46
- def ttl
47
- @ttl ||= Resque::UniqueInQueue.uniq_config&.ttl
47
+ def lock_after_execution_period
48
+ instance_variable_get(:@lock_after_execution_period) ||
49
+ instance_variable_set(:lock_after_execution_period, Resque::UniqueInQueue.configuration&.lock_after_execution_period)
48
50
  end
49
51
 
50
- # The default ttl of a persisting key is 0, i.e. immediately deleted.
51
- # Set lock_after_execution_period to block the execution
52
- # of the job for a certain amount of time (in seconds).
52
+ # The default ttl of a locking key is -1 (forever).
53
+ # To expire the lock after a certain amount of time, set a ttl (in seconds).
53
54
  # For example:
54
55
  #
55
56
  # class FooJob
56
57
  # include Resque::Plugins::UniqueInQueue
57
- # @lock_after_execution_period = 40
58
+ # @ttl = 40
58
59
  # end
59
- def lock_after_execution_period
60
- @lock_after_execution_period ||= Resque::UniqueInQueue.uniq_config&.lock_after_execution_period
60
+ def ttl
61
+ instance_variable_get(:@ttl) ||
62
+ instance_variable_set(:ttl, Resque::UniqueInQueue.configuration&.ttl)
61
63
  end
62
64
 
63
- # Can't be overridden per each class because it wouldn't make sense.
65
+ # Should not generally be overridden per each class because it wouldn't
66
+ # make sense.
64
67
  # It wouldn't be able to determine or enforce uniqueness across queues,
65
68
  # and general cleanup of stray keys would be nearly impossible.
66
69
  def unique_in_queue_key_base
67
- Resque::UniqueInQueue.uniq_config&.unique_in_queue_key_base
70
+ instance_variable_get(:@unique_in_queue_key_base) ||
71
+ instance_variable_set(:@unique_in_queue_key_base, Resque::UniqueInQueue.configuration&.unique_in_queue_key_base)
68
72
  end
69
73
  end
70
74
  end
@@ -2,55 +2,53 @@ require 'logger'
2
2
  module Resque
3
3
  module UniqueInQueue
4
4
  class Configuration
5
- DEFAULT_IN_QUEUE_KEY_BASE = 'r-uiq'.freeze
6
5
  DEFAULT_LOCK_AFTER_EXECUTION_PERIOD = 0
7
6
  DEFAULT_TTL = -1
7
+ DEFAULT_UNIQUE_IN_QUEUE_KEY_BASE = 'r-uiq'.freeze
8
+ DEFAULT_LOG_LEVEL = :debug
8
9
 
9
- attr_accessor :logger,
10
- :log_level,
10
+ include Singleton
11
+
12
+ attr_accessor :debug_mode,
11
13
  :lock_after_execution_period,
14
+ :log_level,
15
+ :logger,
12
16
  :ttl,
13
- :debug_mode
14
-
15
- class << self
16
- attr_accessor :unique_in_queue_key_base
17
- end
18
- # Normally isn't set per job, so it can match across all queued jobs.
19
- @unique_in_queue_key_base = DEFAULT_IN_QUEUE_KEY_BASE
20
-
21
- def initialize(**options)
22
- @logger = options.key?(:logger) ? options[:logger] : Logger.new(STDOUT)
23
- @log_level = options.key?(:log_level) ? options[:log_level] : :debug
24
- @unique_in_queue_key_base = options.key?(:unique_in_queue_key_base) ? options[:unique_in_queue_key_base] : nil
25
-
26
- # Can be set per each job:
27
- @lock_after_execution_period = options.key?(:lock_after_execution_period) ? options[:lock_after_execution_period] : DEFAULT_LOCK_AFTER_EXECUTION_PERIOD
28
- @ttl = options.key?(:ttl) ? options[:ttl] : DEFAULT_TTL
29
- env_debug = ENV['RESQUE_DEBUG']
30
- @debug_mode = options.key?(:debug_mode) ? options[:debug_mode] : env_debug == 'true' || (env_debug.is_a?(String) && env_debug.match?(/in_queue/))
31
- end
32
-
33
- def unique_logger
34
- logger
17
+ :unique_in_queue_key_base
18
+
19
+ def initialize
20
+ debug_mode_from_env
21
+ @lock_after_execution_period = DEFAULT_LOCK_AFTER_EXECUTION_PERIOD
22
+ @log_level = DEFAULT_LOG_LEVEL
23
+ @logger = nil
24
+ @ttl = DEFAULT_TTL
25
+ @unique_in_queue_key_base = DEFAULT_UNIQUE_IN_QUEUE_KEY_BASE
26
+ if @debug_mode
27
+ # Make sure there is a logger when in debug_mode
28
+ @logger ||= Logger.new(STDOUT)
29
+ end
35
30
  end
36
31
 
37
- def unique_log_level
38
- log_level
32
+ def to_hash
33
+ {
34
+ debug_mode: debug_mode,
35
+ lock_after_execution_period: lock_after_execution_period,
36
+ log_level: log_level,
37
+ logger: logger,
38
+ ttl: ttl,
39
+ unique_in_queue_key_base: unique_in_queue_key_base
40
+ }
39
41
  end
40
42
 
41
- def log(msg)
42
- Resque::UniqueInQueue.in_queue_unique_log(msg, self)
43
+ def debug_mode=(val)
44
+ @debug_mode = !!val
43
45
  end
44
46
 
45
- def unique_in_queue_key_base
46
- @unique_in_queue_key_base || self.class.unique_in_queue_key_base
47
- end
47
+ private
48
48
 
49
- def to_hash
50
- {
51
- logger: logger,
52
- log_level: log_level
53
- }
49
+ def debug_mode_from_env
50
+ env_debug = ENV['RESQUE_DEBUG']
51
+ @debug_mode = !!(env_debug == 'true' || (env_debug.is_a?(String) && env_debug.match?(/queue/)))
54
52
  end
55
53
  end
56
54
  end
@@ -64,7 +64,7 @@ module Resque
64
64
  end
65
65
 
66
66
  def cleanup(queue)
67
- keys = redis.keys("#{Resque::UniqueInQueue.uniq_config&.unique_in_queue_key_base}:queue:#{queue}:job:*")
67
+ keys = redis.keys("#{Resque::UniqueInQueue.configuration&.unique_in_queue_key_base}:queue:#{queue}:job:*")
68
68
  redis.del(*keys) if keys.any?
69
69
  end
70
70
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Resque
4
4
  module UniqueInQueue
5
- VERSION = '1.0.3'.freeze
5
+ VERSION = '2.0.0'.freeze
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-unique_in_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter H. Boling
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-11-10 00:00:00.000000000 Z
12
+ date: 2018-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colorize