Class: Allora::Backend::Redis

Inherits:
Allora::Backend show all
Defined in:
lib/allora/backend/redis.rb

Overview

A backend that uses Redis to maintain schedule state.

When using this backend, it is possible to run the scheduler process on more than one machine in the network, connected to the same Redis instace. Whichever scheduler finds a runnable job first updates the ‘next run time’ information in Redis, using an optimistic locking strategy, then executes the job if the write succeeds. No two machines will ever run the same job twice.

Instance Attribute Summary collapse

Attributes inherited from Allora::Backend

#options

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Redis

Initialize the Redis backed with the given options.

Options:

client: an already instantiated Redis client object.
host:   the hostname of a Redis server
port:   the port number of a Redis server
prefix: a namespace prefix to use

Parameters:

  • opts (Hash) (defaults to: {})

    options for the Redis backend



47
48
49
50
51
52
# File 'lib/allora/backend/redis.rb', line 47

def initialize(opts = {})
  @redis  = create_redis(opts)
  @prefix = opts.fetch(:prefix, "allora")

  reset!
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

#redisObject (readonly)

Returns the value of attribute redis.



34
35
36
# File 'lib/allora/backend/redis.rb', line 34

def redis
  @redis
end

Instance Method Details

#reschedule(jobs) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/allora/backend/redis.rb', line 54

def reschedule(jobs)
  current_time = Time.now
  last_time    = send(:last_time)
  set_last_time(current_time)

  jobs.select do |name, job|
    redis.setnx(job_info_key(name), time_to_int(job.next_at(last_time)))
    update_job_info(job, name, current_time)
  end
end