Class: MailRoom::Arbitration::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_room/arbitration/redis.rb

Defined Under Namespace

Classes: Options

Constant Summary collapse

EXPIRATION =

Expire after 10 minutes so Redis doesn’t get filled up with outdated data.

600

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Redis

Returns a new instance of Redis.



20
21
22
# File 'lib/mail_room/arbitration/redis.rb', line 20

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/mail_room/arbitration/redis.rb', line 18

def options
  @options
end

Instance Method Details

#deliver?(uid) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mail_room/arbitration/redis.rb', line 24

def deliver?(uid)
  key = "delivered:#{uid}"

  incr = nil
  redis.multi do |client|
    # At this point, `incr` is a future, which will get its value after 
    # the MULTI command returns.
    incr = client.incr(key)

    client.expire(key, EXPIRATION)
  end

  # If INCR returns 1, that means the key didn't exist before, which means
  # we are the first mail_room to try to deliver this message, so we get to.
  # If we get any other value, another mail_room already (tried to) deliver
  # the message, so we don't have to anymore.
  incr.value == 1
end