resque-unique_at_runtime 3.0.2 → 4.0.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 +4 -4
- data/README.md +19 -0
- data/lib/resque-unique_at_runtime.rb +10 -45
- data/lib/resque/plugins/unique_at_runtime.rb +15 -12
- data/lib/resque/unique_at_runtime/configuration.rb +35 -39
- data/lib/resque/unique_at_runtime/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dad974343819e8b331d91885f9b24b1856bc0e02e6adfcdaba9421e8f9eb2165
|
4
|
+
data.tar.gz: a84c76ce456fdf74343384852dc68a8dac2ca30cd66f382a947a0f419adeeceb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
27
|
-
|
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
|
32
|
-
|
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
|
44
|
-
@
|
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 :
|
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.
|
44
|
+
self.configuration = Configuration.instance # setup defaults
|
73
45
|
|
74
|
-
module_function(:
|
75
|
-
:
|
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) ||
|
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) ||
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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
|
-
|
12
|
-
|
12
|
+
include Singleton
|
13
|
+
|
14
|
+
attr_accessor :debug_mode,
|
13
15
|
:lock_timeout,
|
16
|
+
:log_level,
|
17
|
+
:logger,
|
14
18
|
:requeue_interval,
|
15
|
-
:
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
@
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
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
|
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:
|
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-
|
12
|
+
date: 2018-11-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: colorize
|