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 entries in Redis
ttl:    ttl for job timing entries in Redis

Parameters:

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

    options for the Redis backend



50
51
52
53
54
55
56
# File 'lib/allora/backend/redis.rb', line 50

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

  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

#ttlObject (readonly)

Returns the value of attribute ttl.



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

def ttl
  @ttl
end

Instance Method Details

#reschedule(jobs) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/allora/backend/redis.rb', line 58

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