Class: Resque::DataStore

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/resque/data_store.rb

Overview

An interface between Resque’s persistence and the actual implementation.

Defined Under Namespace

Classes: FailedQueueAccess, QueueAccess, StatsAccess, Workers

Constant Summary collapse

HEARTBEAT_KEY =
"workers:heartbeat"

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ DataStore

Returns a new instance of DataStore.



9
10
11
12
13
14
15
# File 'lib/resque/data_store.rb', line 9

def initialize(redis)
  @redis                = redis
  @queue_access         = QueueAccess.new(@redis)
  @failed_queue_access  = FailedQueueAccess.new(@redis)
  @workers              = Workers.new(@redis)
  @stats_access         = StatsAccess.new(@redis)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Compatibility with any non-Resque classes that were using Resque.redis as a way to access Redis



57
58
59
60
# File 'lib/resque/data_store.rb', line 57

def method_missing(sym,*args,&block)
  # TODO: deprecation warning?
  @redis.send(sym,*args,&block)
end

Instance Method Details

#all_resque_keysObject

Returns an array of all known Resque keys in Redis. Redis’ KEYS operation is O(N) for the keyspace, so be careful - this can be slow for big databases.



87
88
89
90
91
# File 'lib/resque/data_store.rb', line 87

def all_resque_keys
  @redis.keys("*").map do |key|
    key.sub("#{@redis.namespace}:", '')
  end
end

#identifierObject

Get a string identifying the underlying server. Probably should be private, but was public so must stay public



69
70
71
72
73
74
75
76
77
78
# File 'lib/resque/data_store.rb', line 69

def identifier
  # support 1.x versions of redis-rb
  if @redis.respond_to?(:server)
    @redis.server
  elsif @redis.respond_to?(:nodes) # distributed
    @redis.nodes.map { |n| n.id }.join(', ')
  else
    @redis.client.id
  end
end

#reconnectObject

Force a reconnect to Redis.



81
82
83
# File 'lib/resque/data_store.rb', line 81

def reconnect
  @redis.client.reconnect
end

#respond_to?(method, include_all = false) ⇒ Boolean

make use respond like redis

Returns:

  • (Boolean)


63
64
65
# File 'lib/resque/data_store.rb', line 63

def respond_to?(method,include_all=false)
  @redis.respond_to?(method,include_all) || super
end

#server_timeObject



93
94
95
96
# File 'lib/resque/data_store.rb', line 93

def server_time
  time, _ = redis_time_available? ? @redis.time : Time.now
  Time.at(time)
end