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 +4 -4
- data/README.md +20 -0
- data/lib/resque-unique_in_queue.rb +10 -45
- data/lib/resque/plugins/unique_in_queue.rb +17 -13
- data/lib/resque/unique_in_queue/configuration.rb +35 -37
- data/lib/resque/unique_in_queue/queue.rb +1 -1
- data/lib/resque/unique_in_queue/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: 48f097b477503dbe4a6028c2587476e1a0bc45f53627d77aa3a2dc0b461edf59
|
4
|
+
data.tar.gz: b2134a95053feb956cba28906cf98aad1158556ce75c486a98ee5cd243f58301
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
29
|
-
|
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
|
34
|
-
|
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
|
46
|
-
@
|
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 :
|
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.
|
46
|
+
self.configuration = Configuration.instance # setup defaults
|
75
47
|
|
76
|
-
module_function(:
|
77
|
-
:
|
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
|
39
|
-
#
|
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
|
-
# @
|
45
|
+
# @lock_after_execution_period = 40
|
45
46
|
# end
|
46
|
-
def
|
47
|
-
|
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
|
51
|
-
#
|
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
|
-
# @
|
58
|
+
# @ttl = 40
|
58
59
|
# end
|
59
|
-
def
|
60
|
-
|
60
|
+
def ttl
|
61
|
+
instance_variable_get(:@ttl) ||
|
62
|
+
instance_variable_set(:ttl, Resque::UniqueInQueue.configuration&.ttl)
|
61
63
|
end
|
62
64
|
|
63
|
-
#
|
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
|
-
|
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
|
-
|
10
|
-
|
10
|
+
include Singleton
|
11
|
+
|
12
|
+
attr_accessor :debug_mode,
|
11
13
|
:lock_after_execution_period,
|
14
|
+
:log_level,
|
15
|
+
:logger,
|
12
16
|
:ttl,
|
13
|
-
:
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
@
|
23
|
-
|
24
|
-
|
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
|
38
|
-
|
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
|
42
|
-
|
43
|
+
def debug_mode=(val)
|
44
|
+
@debug_mode = !!val
|
43
45
|
end
|
44
46
|
|
45
|
-
|
46
|
-
@unique_in_queue_key_base || self.class.unique_in_queue_key_base
|
47
|
-
end
|
47
|
+
private
|
48
48
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
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.
|
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
|
|
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:
|
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-
|
12
|
+
date: 2018-11-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: colorize
|