Class: Continuity::RedisBackend
- Inherits:
-
Object
- Object
- Continuity::RedisBackend
- Defined in:
- lib/continuity/redis_backend.rb
Constant Summary collapse
- LOCK_KEY =
"continuity_lock"
- LAST_SCHED_KEY =
"continuity_scheduled_up_to"
Instance Method Summary collapse
-
#initialize(redis, frequency = 10, lock_timeout = 30) ⇒ RedisBackend
constructor
A new instance of RedisBackend.
- #lock_for_scheduling(now) ⇒ Object
Constructor Details
#initialize(redis, frequency = 10, lock_timeout = 30) ⇒ RedisBackend
Returns a new instance of RedisBackend.
6 7 8 9 10 |
# File 'lib/continuity/redis_backend.rb', line 6 def initialize(redis, frequency = 10, lock_timeout = 30) @redis = redis @lock_timeout = lock_timeout @frequency = frequency end |
Instance Method Details
#lock_for_scheduling(now) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/continuity/redis_backend.rb', line 12 def lock_for_scheduling(now) scheduled_up_to = @redis.get(LAST_SCHED_KEY).to_i # bootstrap if scheduled_up_to == 0 lock(now) do # double check that someone else has bootstrapped # since we fetched the key scheduled_up_to = @redis.get(LAST_SCHED_KEY).to_i if scheduled_up_to == 0 yield now - 1 @redis.set(LAST_SCHED_KEY, now) return now else return scheduled_up_to end end end # this is tricky, we only want to attempt a lock # if there is a possibility we can schedule things. # BUT, once we attain a lock we need to make sure # someone else hasn't already scheduled that period if (now - scheduled_up_to) >= @frequency lock(now) do scheduled_up_to = @redis.get(LAST_SCHED_KEY).to_i if (now - scheduled_up_to) >= @frequency # good we should still schedule yield scheduled_up_to @redis.set(LAST_SCHED_KEY, now) scheduled_up_to = now end end end scheduled_up_to end |