Module: Once
- Defined in:
- lib/once.rb,
lib/once/version.rb
Overview
Usage:
-
Connect to redis: Once.redis = Redis.new(…)
If you don’t specify the redis connection, we will assume the presence of a $redis global
-
Use to wrap a call that you want done uniquely
Once.do(name: "sending_email", params: { email: "[email protected]" }, within: 1.hour) do
.. stuff that should happen only once ..
end
The combination of the name and params makes the check unique. So typically it would be the command you’re executing, plus the params to that command
Constant Summary collapse
- DEFAULT_TIME =
seconds
3600
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.do(name:, params:, within: DEFAULT_TIME, &block) ⇒ Object
Checks the given params to see if this is a unique string If we’ve seen it within the expiry period (default: 1.hour), then we will not execute the block.
- .redis ⇒ Object
- .redis=(redis) ⇒ Object
Class Method Details
.do(name:, params:, within: DEFAULT_TIME, &block) ⇒ Object
Checks the given params to see if this is a unique string If we’ve seen it within the expiry period (default: 1.hour), then we will not execute the block
name: The name of the check, used as a namespace params: The params that will control whether or not the body executes
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/once.rb', line 36 def do(name:, params:, within: DEFAULT_TIME, &block) hash = Digest::MD5.hexdigest(params.inspect) redis_key = "uniquecheck:#{name}:#{hash}" if redis.set(redis_key, true, ex: within, nx: true) begin block.call rescue redis.expire(redis_key, 0) raise end end end |
.redis ⇒ Object
22 23 24 |
# File 'lib/once.rb', line 22 def redis @redis || $redis end |
.redis=(redis) ⇒ Object
26 27 28 |
# File 'lib/once.rb', line 26 def redis=(redis) @redis = redis end |