Class: Allora::Backend::Redis
- Inherits:
-
Allora::Backend
- Object
- Allora::Backend
- Allora::Backend::Redis
- 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
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Attributes inherited from Allora::Backend
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Redis
constructor
Initialize the Redis backed with the given options.
- #reschedule(jobs) ⇒ Object
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
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
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
35 36 37 |
# File 'lib/allora/backend/redis.rb', line 35 def prefix @prefix end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
34 35 36 |
# File 'lib/allora/backend/redis.rb', line 34 def redis @redis end |
#ttl ⇒ Object (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 |