Class: Wayfarer::Redis::Barrier

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Resettable

#reset!

Constructor Details

#initialize(task) ⇒ Barrier

Returns a new instance of Barrier.

Parameters:



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

#taskHash (readonly)

Returns the task configuration, including redis_pool and batch name.

Returns:

  • (Hash)

    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.

Parameters:

  • key (String)

    the key to check

Returns:

  • (Boolean)

    true if the key has already been seen, false otherwise



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_keyString

Returns the Redis key for this barrier.

Returns:

  • (String)

    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