Class: SidekiqSmartCache::Redis

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

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ Redis

Returns a new instance of Redis.



12
13
14
# File 'lib/sidekiq_smart_cache/redis.rb', line 12

def initialize(pool)
  @pool = pool
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sidekiq_smart_cache/redis.rb', line 39

def method_missing(name, ...)
  @pool.with do |r|
    if COMMANDS.include? name
      retryable = true
      begin
        # log_msg("#{name} #{args}")
        if r.respond_to?(name)
          # old redis gem implements methods including `brpop` and `flusdb`
          r.send(name, ...)
        elsif BLOCKING_COMMANDS.include? name
          # support redis-client semantics
          make_blocking_call(r, name, ...)
        else
          r.call(name.to_s.upcase, ...)
        end
        # stolen from sidekiq - Thanks Mike!
        # Quietly consume this one and return nil
      rescue ERROR_TO_CATCH => ex
        # 2550 Failover can cause the server to become a replica, need
        # to disconnect and reopen the socket to get back to the primary.
        if retryable && ex.message =~ /READONLY/
          r.disconnect!
          retryable = false
          retry
        end
        raise
      end
    else
      super
    end
  end
end

Instance Method Details

#job_completion_key(key) ⇒ Object



16
17
18
# File 'lib/sidekiq_smart_cache/redis.rb', line 16

def job_completion_key(key)
  key + '/done'
end

#log_msg(msg) ⇒ Object

WIP all this



35
36
37
# File 'lib/sidekiq_smart_cache/redis.rb', line 35

def log_msg(msg) # WIP all this
  Rails.logger.info("#{Time.now.iso8601(3)} #{Thread.current[:name]} redis #{msg}")
end

#make_blocking_call(r, name, *args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sidekiq_smart_cache/redis.rb', line 72

def make_blocking_call(r, name, *args)
  # The Redis `brpop` implementation seems to allow timeout to be the last argument
  # or a key in a hash `timeout: 5`, so:
  # `r.brpop('key1', 'key2', 5)` - wait five seconds for either queue
  # `r.brpop('key1', 'key2', timeout: 5)` - same
  # `r.brpop('key1', 'key2')` - wait forever on either queue
  timeout = if args.last.is_a?(Hash)
    options = args.pop
    options[:timeout]
  else
    args.pop
  end

  # With RedisClient, the doc is a little thin, but it looks like we want to start with the timeout
  # then the verb, then the array of keys
  # and end with ... a 0?
  blocking_call_args = [timeout, name.to_s.upcase] + args + [0]
  r.blocking_call(*blocking_call_args)
rescue ::RedisClient::TimeoutError
  nil # quietly return nil in this case
end

#send_done_message(key) ⇒ Object



20
21
22
23
# File 'lib/sidekiq_smart_cache/redis.rb', line 20

def send_done_message(key)
  lpush(job_completion_key(key), 'done')
  expire(job_completion_key(key), 1)
end

#wait_for_done_message(key, timeout) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/sidekiq_smart_cache/redis.rb', line 25

def wait_for_done_message(key, timeout)
  return true if defined?(Sidekiq::Testing) && Sidekiq::Testing.inline?

  if brpop(job_completion_key(key), timeout: timeout.to_i)
    # log_msg("got done message for #{key}")
    send_done_message(key) # put it back for any other readers
    true
  end
end