Class: Wayfarer::Redis::Barrier
- Inherits:
-
Object
- Object
- Wayfarer::Redis::Barrier
- Includes:
- Resettable
- Defined in:
- lib/wayfarer/redis/barrier.rb
Overview
A Barrier prevents processing the same key more than once. It marks keys in a Redis hash as they are encountered. Once a key is added, it cannot get removed.
Constant Summary collapse
- VALUE =
""
Instance Attribute Summary collapse
-
#task ⇒ Hash
readonly
The task configuration, including redis_pool and batch name.
Instance Method Summary collapse
-
#check!(key) ⇒ Boolean
Checks if a key has already been passed through the barrier.
-
#initialize(task) ⇒ Barrier
constructor
A new instance of Barrier.
-
#redis_key ⇒ String
The Redis key for this barrier.
Methods included from Resettable
Constructor Details
#initialize(task) ⇒ Barrier
Returns a new instance of Barrier.
17 18 19 20 |
# File 'lib/wayfarer/redis/barrier.rb', line 17 def initialize(task) @task = task @redis_pool = task[:redis_pool] end |
Instance Attribute Details
#task ⇒ Hash (readonly)
Returns the task configuration, including redis_pool and batch name.
12 13 14 |
# File 'lib/wayfarer/redis/barrier.rb', line 12 def task @task end |
Instance Method Details
#check!(key) ⇒ Boolean
Checks if a key has already been passed through the barrier.
31 32 33 |
# File 'lib/wayfarer/redis/barrier.rb', line 31 def check!(key) !redis_pool.with { |conn| conn.hsetnx(redis_key, key, VALUE) } end |
#redis_key ⇒ String
Returns the Redis key for this barrier.
23 24 25 |
# File 'lib/wayfarer/redis/barrier.rb', line 23 def redis_key "wayfarer-barrier-#{task.batch}" end |