Slock
Slock implements Semaphore via Redis.
Installation
Add this line to your application's Gemfile:
gem 'slock'
Usage
Singleton Class
class MySemaphore
include Slock::Semaphore::Singleton
SIZE = 2 # max count of simultaneous locks
LIFETIME = 600 # max time that lock lives after acquring (in seconds)
TIMEOUT = 900 # max time that semaphore waits for lock to acquire before raising an error
def semaphore_opts
{
redis: Redis.new(url: ENV['REDIS_URL']),
size: SIZE,
lifetime: LIFETIME,
timeout: TIMEOUT
}
end
end
MySemaphore.acquire { do_something }
Simple
sempahore = Slock::Semaphore.new 'uniq_semaphore_key',
redis: Redis.new(ENV['REDIS_URL']),
lifetime: 600,
timeout: 900
semaphore.acquire { do_something }