resque-unique_at_runtime 3.0.2 → 4.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: 3a8a2e4eac00952a8cccc8cbc74b2b7f4bdbdf7c594f1a0e62891436c0a0c8f8
4
- data.tar.gz: f25b46513a61c07c9c37bf6af101ca4504b4e25534ac7842ea3885b2a5a30a4b
3
+ metadata.gz: dad974343819e8b331d91885f9b24b1856bc0e02e6adfcdaba9421e8f9eb2165
4
+ data.tar.gz: a84c76ce456fdf74343384852dc68a8dac2ca30cd66f382a947a0f419adeeceb
5
5
  SHA512:
6
- metadata.gz: aa08c400e7ad45f3265ad39dd01fb0cdcdd9ea8891bbbf88671972d4625c289cf15d01e7609d56b06a7dafbac0687f19acdf3ceeb54106438cdde231823bdf77
7
- data.tar.gz: 5eee11d58a60feb72ff8585286f19954d6c1c779d1a33288f4d52a5bd54ad08e961226af72c7a559811269c6de1c918c83f505cd6c492caa6bdec0a29b10f70b
6
+ metadata.gz: 6508204325cc2a97856c691bf98ddb9e91ebaab082d9e36fc21e347f2ed409b4ed3ef9fae851394441158dd03fd9e3d99e35403b4594787e01dbe6348ac0dd4f
7
+ data.tar.gz: 122f39d393b7f8fd5cc44d71024a78e4501af60dc289aaea7cd18c4fa5698534a1a50f4aa19d06f10685030bc770bef5bb4aab003eaae7cddcdccd59b3b697c6
data/README.md CHANGED
@@ -58,6 +58,25 @@ Or install it yourself as:
58
58
 
59
59
  ## Usage
60
60
 
61
+ `resque-unique_at_runtime` utilizes 3 class instance variables that can be set
62
+ in your Jobs, in addition to the standard `@queue`. Here they are, with their
63
+ default values:
64
+
65
+ ```ruby
66
+ @runtime_lock_timeout = 60 * 60 * 24 * 5
67
+ @runtime_requeue_interval = 1
68
+ @unique_at_runtime_key_base = 'r-uar'.freeze
69
+ ```
70
+
71
+ The last one, in normal circumstances, shouldn't be set as different per class,
72
+ or uniqueness cleanup becomes more difficult.
73
+
74
+ It should be set only once, globally:
75
+
76
+ ```ruby
77
+ Resque::UniqueAtRuntime.configuration.unique_at_runtime_key_base = 'my-custom'
78
+ ```
79
+
61
80
  #### Example #1 -- One job running per queue
62
81
 
63
82
  require 'resque-unique_at_runtime'
@@ -23,62 +23,27 @@ module Resque
23
23
  module UniqueAtRuntime
24
24
  PLUGIN_TAG = (ColorizedString['[R-UAR] '].blue).freeze
25
25
 
26
- def runtime_unique_log(message, config_proxy = nil)
27
- config_proxy ||= uniqueness_configuration
28
- config_proxy.unique_logger&.send(config_proxy.unique_log_level, message)
26
+ def log(message)
27
+ configuration.logger&.send(configuration.log_level, message) if configuration.logger
29
28
  end
30
29
 
31
- def runtime_unique_debug(message, config_proxy = nil)
32
- config_proxy ||= uniqueness_configuration
33
- config_proxy.unique_logger&.debug("#{PLUGIN_TAG}#{message}") if config_proxy.debug_mode
34
- end
35
-
36
- # There are times when the class will need access to the configuration object,
37
- # such as to override it per instance method
38
- def uniq_config
39
- @uniqueness_configuration
30
+ def debug(message)
31
+ configuration.logger&.debug("#{PLUGIN_TAG}#{message}") if configuration.debug_mode
40
32
  end
41
33
 
42
34
  # For per-class config with a block
43
- def uniqueness_configure
44
- @uniqueness_configuration ||= Configuration.new
45
- yield(@uniqueness_configuration)
35
+ def configure
36
+ yield(@configuration)
46
37
  end
47
38
 
48
39
  #### CONFIG ####
49
40
  class << self
50
- attr_accessor :uniqueness_configuration
51
- end
52
- def uniqueness_config_reset(config = Configuration.new)
53
- @uniqueness_configuration = config
54
- end
55
-
56
- def uniqueness_log_level
57
- @uniqueness_configuration.log_level
58
- end
59
-
60
- def uniqueness_log_level=(log_level)
61
- @uniqueness_configuration.log_level = log_level
62
- end
63
-
64
- def unique_at_runtime_key_base
65
- Configuration.unique_at_runtime_key_base
66
- end
67
-
68
- def unique_at_runtime_key_base=(key_base)
69
- Configuration.unique_at_runtime_key_base = key_base
41
+ attr_accessor :configuration
70
42
  end
71
43
 
72
- self.uniqueness_configuration = Configuration.new # setup defaults
44
+ self.configuration = Configuration.instance # setup defaults
73
45
 
74
- module_function(:runtime_unique_log,
75
- :runtime_unique_debug,
76
- :uniq_config,
77
- :uniqueness_configure,
78
- :uniqueness_config_reset,
79
- :uniqueness_log_level,
80
- :uniqueness_log_level=,
81
- :unique_at_runtime_key_base,
82
- :unique_at_runtime_key_base=)
46
+ module_function(:log,
47
+ :debug)
83
48
  end
84
49
  end
@@ -25,17 +25,24 @@ module Resque
25
25
  end
26
26
 
27
27
  def runtime_lock_timeout
28
- instance_variable_get(:@runtime_lock_timeout) || Resque::UniqueAtRuntime.uniq_config&.lock_timeout
28
+ instance_variable_get(:@runtime_lock_timeout) ||
29
+ instance_variable_set(:@runtime_lock_timeout, Resque::UniqueAtRuntime.configuration&.lock_timeout)
29
30
  end
30
31
 
31
32
  def runtime_requeue_interval
32
- instance_variable_get(:@runtime_requeue_interval) || Resque::UniqueAtRuntime.uniq_config&.requeue_interval
33
+ instance_variable_get(:@runtime_requeue_interval) ||
34
+ instance_variable_set(:@runtime_requeue_interval, Resque::UniqueAtRuntime.configuration&.requeue_interval)
35
+ end
36
+
37
+ def unique_at_runtime_key_base
38
+ instance_variable_get(:@unique_at_runtime_key_base) ||
39
+ instance_variable_set(:@unique_at_runtime_key_base, Resque::UniqueAtRuntime.configuration&.unique_at_runtime_key_base)
33
40
  end
34
41
 
35
42
  # Overwrite this method to uniquely identify which mutex should be used
36
43
  # for a resque worker.
37
44
  def unique_at_runtime_redis_key(*_)
38
- Resque::UniqueAtRuntime.runtime_unique_debug("getting key for #{@queue}!")
45
+ Resque::UniqueAtRuntime.debug("getting key for #{@queue}!")
39
46
  "#{unique_at_runtime_key_base}:#{@queue}"
40
47
  end
41
48
 
@@ -50,7 +57,7 @@ module Resque
50
57
  key = unique_at_runtime_redis_key(*args)
51
58
  timeout = runtime_lock_timeout_at(now)
52
59
 
53
- Resque::UniqueAtRuntime.runtime_unique_debug("attempting to lock queue with #{key}")
60
+ Resque::UniqueAtRuntime.debug("attempting to lock queue with #{key}")
54
61
 
55
62
  # Per http://redis.io/commands/setnx
56
63
  return false if Resque.redis.setnx(key, timeout)
@@ -62,7 +69,7 @@ module Resque
62
69
 
63
70
  def unlock_queue(*args)
64
71
  key = unique_at_runtime_redis_key(*args)
65
- Resque::UniqueAtRuntime.runtime_unique_debug("unlock queue with #{key}")
72
+ Resque::UniqueAtRuntime.debug("unlock queue with #{key}")
66
73
  Resque.redis.del(key)
67
74
  end
68
75
 
@@ -72,7 +79,7 @@ module Resque
72
79
 
73
80
  def before_perform_lock_runtime(*args)
74
81
  if (key = queue_locked?(*args))
75
- Resque::UniqueAtRuntime.runtime_unique_debug("failed to lock queue with #{key}")
82
+ Resque::UniqueAtRuntime.debug("failed to lock queue with #{key}")
76
83
 
77
84
  # Sleep so the CPU's rest
78
85
  sleep(runtime_requeue_interval)
@@ -83,7 +90,7 @@ module Resque
83
90
  # and don't perform
84
91
  raise Resque::Job::DontPerform
85
92
  else
86
- Resque::UniqueAtRuntime.runtime_unique_debug('check passed will perform')
93
+ Resque::UniqueAtRuntime.debug('check passed will perform')
87
94
  true
88
95
  end
89
96
  end
@@ -98,13 +105,9 @@ module Resque
98
105
  # duplicates the on_failure unlock, but that's a small price to pay for
99
106
  # uniqueness.
100
107
  def on_failure_unlock_runtime(*args)
101
- Resque::UniqueAtRuntime.runtime_unique_debug('on failure unlock')
108
+ Resque::UniqueAtRuntime.debug('on failure unlock')
102
109
  unlock_queue(*args)
103
110
  end
104
-
105
- def unique_at_runtime_key_base
106
- instance_variable_get(:@unique_at_runtime_key_base) || Resque::UniqueAtRuntime.uniq_config&.unique_at_runtime_key_base
107
- end
108
111
  end
109
112
  end
110
113
  end
@@ -4,58 +4,54 @@ require 'logger'
4
4
  module Resque
5
5
  module UniqueAtRuntime
6
6
  class Configuration
7
- DEFAULT_AT_RUNTIME_KEY_BASE = 'r-uae'
8
7
  DEFAULT_LOCK_TIMEOUT = 60 * 60 * 24 * 5
9
8
  DEFAULT_REQUEUE_INTERVAL = 1
9
+ DEFAULT_UNIQUE_AT_RUNTIME_KEY_BASE = 'r-uar'.freeze
10
+ DEFAULT_LOG_LEVEL = :debug
10
11
 
11
- attr_accessor :logger,
12
- :log_level,
12
+ include Singleton
13
+
14
+ attr_accessor :debug_mode,
13
15
  :lock_timeout,
16
+ :log_level,
17
+ :logger,
14
18
  :requeue_interval,
15
- :debug_mode
16
-
17
- class << self
18
- attr_accessor :unique_at_runtime_key_base
19
- end
20
- # Normally isn't set per job, so it will match across all runtime jobs.
21
- @unique_at_runtime_key_base = DEFAULT_AT_RUNTIME_KEY_BASE
22
-
23
- def initialize(**options)
24
- @logger = options.key?(:logger) ? options[:logger] : Logger.new(STDOUT)
25
- @log_level = options.key?(:log_level) ? options[:log_level] : :debug
26
- @unique_at_runtime_key_base = options.key?(:unique_at_runtime_key_base) ? options[:unique_at_runtime_key_base] : nil
27
- @lock_timeout = options.key?(:lock_timeout) ? options[:lock_timeout] : DEFAULT_LOCK_TIMEOUT
28
- @requeue_interval = options.key?(:requeue_interval) ? options[:requeue_interval] : DEFAULT_REQUEUE_INTERVAL
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?(/runtime/)))
31
- end
32
-
33
- def unique_logger
34
- logger
35
- end
36
-
37
- def unique_log_level
38
- log_level
39
- end
40
-
41
- def log(msg)
42
- Resque::UniqueAtRuntime.runtime_unique_log(msg, self)
43
- end
44
-
45
- def unique_at_runtime_key_base
46
- @unique_at_runtime_key_base || self.class.unique_at_runtime_key_base
19
+ :unique_at_runtime_key_base
20
+
21
+ def initialize
22
+ debug_mode_from_env
23
+ @lock_timeout = DEFAULT_LOCK_TIMEOUT
24
+ @log_level = DEFAULT_LOG_LEVEL
25
+ @logger = nil
26
+ @requeue_interval = DEFAULT_REQUEUE_INTERVAL
27
+ @unique_at_runtime_key_base = DEFAULT_UNIQUE_AT_RUNTIME_KEY_BASE
28
+ if @debug_mode
29
+ # Make sure there is a logger when in debug_mode
30
+ @logger ||= Logger.new(STDOUT)
31
+ end
47
32
  end
48
33
 
49
34
  def to_hash
50
35
  {
51
- logger: logger,
52
- log_level: log_level,
53
36
  debug_mode: debug_mode,
54
- unique_at_runtime_key_base: unique_at_runtime_key_base,
55
37
  lock_timeout: lock_timeout,
56
- requeue_interval: requeue_interval
38
+ log_level: log_level,
39
+ logger: logger,
40
+ requeue_interval: requeue_interval,
41
+ unique_at_runtime_key_base: unique_at_runtime_key_base
57
42
  }
58
43
  end
44
+
45
+ def debug_mode=(val)
46
+ @debug_mode = !!val
47
+ end
48
+
49
+ private
50
+
51
+ def debug_mode_from_env
52
+ env_debug = ENV['RESQUE_DEBUG']
53
+ @debug_mode = !!(env_debug == 'true' || (env_debug.is_a?(String) && env_debug.match?(/runtime/)))
54
+ end
59
55
  end
60
56
  end
61
57
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Resque
4
4
  module UniqueAtRuntime
5
- VERSION = '3.0.2'
5
+ VERSION = '4.0.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-unique_at_runtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 4.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