Skip to content

Commit 936a58e

Browse files
committed
Another rewrite
1 parent e3bcc4a commit 936a58e

File tree

5 files changed

+83
-96
lines changed

5 files changed

+83
-96
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ gem 'resque-unique_in_queue'
3333

3434
## Usage
3535

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+
3656
```ruby
3757
class UpdateCat
3858
include Resque::Plugins::UniqueInQueue

lib/resque-unique_in_queue.rb

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,62 +25,27 @@ module Resque
2525
module UniqueInQueue
2626
PLUGIN_TAG = (ColorizedString['[R-UIQ] '].blue).freeze
2727

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
3130
end
3231

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
4234
end
4335

4436
# 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)
4839
end
4940

5041
#### CONFIG ####
5142
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
7244
end
7345

74-
self.uniqueness_configuration = Configuration.new # setup defaults
46+
self.configuration = Configuration.instance # setup defaults
7547

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)
8550
end
8651
end

lib/resque/plugins/unique_in_queue.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,40 @@ def redis_key(payload)
3535
Digest::MD5.hexdigest Resque.encode(class: job, args: args)
3636
end
3737

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).
4041
# For example:
4142
#
4243
# class FooJob
4344
# include Resque::Plugins::UniqueInQueue
44-
# @ttl = 40
45+
# @lock_after_execution_period = 40
4546
# 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)
4850
end
4951

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).
5354
# For example:
5455
#
5556
# class FooJob
5657
# include Resque::Plugins::UniqueInQueue
57-
# @lock_after_execution_period = 40
58+
# @ttl = 40
5859
# 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)
6163
end
6264

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.
6467
# It wouldn't be able to determine or enforce uniqueness across queues,
6568
# and general cleanup of stray keys would be nearly impossible.
6669
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)
6872
end
6973
end
7074
end

lib/resque/unique_in_queue/configuration.rb

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,53 @@
22
module Resque
33
module UniqueInQueue
44
class Configuration
5-
DEFAULT_IN_QUEUE_KEY_BASE = 'r-uiq'.freeze
65
DEFAULT_LOCK_AFTER_EXECUTION_PERIOD = 0
76
DEFAULT_TTL = -1
7+
DEFAULT_UNIQUE_IN_QUEUE_KEY_BASE = 'r-uiq'.freeze
8+
DEFAULT_LOG_LEVEL = :debug
89

9-
attr_accessor :logger,
10-
:log_level,
10+
include Singleton
11+
12+
attr_accessor :debug_mode,
1113
:lock_after_execution_period,
14+
:log_level,
15+
:logger,
1216
: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
3530
end
3631

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+
}
3941
end
4042

41-
def log(msg)
42-
Resque::UniqueInQueue.in_queue_unique_log(msg, self)
43+
def debug_mode=(val)
44+
@debug_mode = !!val
4345
end
4446

45-
def unique_in_queue_key_base
46-
@unique_in_queue_key_base || self.class.unique_in_queue_key_base
47-
end
47+
private
4848

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/)))
5452
end
5553
end
5654
end

lib/resque/unique_in_queue/queue.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def destroy(queue, klass, *args)
6464
end
6565

6666
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:*")
6868
redis.del(*keys) if keys.any?
6969
end
7070

0 commit comments

Comments
 (0)