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
reset:  delete existing job timing keys in Redis

Parameters:

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

    options for the Redis backend



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

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

  reset! if opts.fetch(:reset, true)
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



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

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