Module: Resque::Plugins::WaitingRoom
- Defined in:
- lib/resque/plugins/version.rb,
lib/resque/plugins/waiting_room.rb
Defined Under Namespace
Classes: MissingParams
Constant Summary collapse
- VERSION =
'0.3.0'
Instance Method Summary collapse
- #before_perform_waiting_room(*args) ⇒ Object
- #can_be_performed(params) ⇒ Object
- #remaining_performs_key?(key) ⇒ Boolean
- #repush(*args) ⇒ Object
- #waiting_room_redis_key(*_args) ⇒ Object
Instance Method Details
#before_perform_waiting_room(*args) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/resque/plugins/waiting_room.rb', line 17 def before_perform_waiting_room(*args) key = waiting_room_redis_key(args) return unless remaining_performs_key?(key) performs_left = Resque.redis.decrby(key, 1).to_i if performs_left < 1 Resque.push 'waiting_room', class: self.to_s, args: args raise Resque::Job::DontPerform end end |
#can_be_performed(params) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/resque/plugins/waiting_room.rb', line 6 def can_be_performed(params) raise MissingParams unless params.is_a?(Hash) && params.keys.sort == %i[period times] @period ||= params[:period] @max_performs ||= params[:times].to_i end |
#remaining_performs_key?(key) ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/resque/plugins/waiting_room.rb', line 28 def remaining_performs_key?(key) # if true then we will only set a key if it doesn't exist # if false then we will set the key regardless. If we have # a negative number then redis either doesn't know about the key # or it doesn't have a ttl, either way we want to create a new key # with a new ttl. nx = Resque.redis.ttl(key).to_i > 0 # Redis SET: with the ex and nx option sets the keys if it doesn't exist, # returns true if key was created redis => 2.6 required # http://redis.io/commands/SET !Resque.redis.set(key, @max_performs - 1, ex: @period, nx: nx) end |
#repush(*args) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/resque/plugins/waiting_room.rb', line 42 def repush(*args) key = waiting_room_redis_key(args) value = Resque.redis.get(key) no_performs_left = value && value != '' && value.to_i <= 0 Resque.push 'waiting_room', class: self.to_s, args: args if no_performs_left return no_performs_left end |
#waiting_room_redis_key(*_args) ⇒ Object
13 14 15 |
# File 'lib/resque/plugins/waiting_room.rb', line 13 def waiting_room_redis_key(*_args) [self.to_s, 'remaining_performs'].compact.join(':') end |