Class: Rector::Backends::Redis
- Inherits:
-
Object
- Object
- Rector::Backends::Redis
- Defined in:
- lib/rector/backends/redis.rb
Constant Summary collapse
- KEY_LIST_SET =
"__keys__"
- WORKER_LIST_SET =
"__workers__"
Instance Attribute Summary collapse
-
#job_id ⇒ Object
readonly
Returns the value of attribute job_id.
Instance Method Summary collapse
- #add_worker(worker_id) ⇒ Object
- #cleanup ⇒ Object
- #finish_worker(worker_id) ⇒ Object
-
#initialize(job_id) ⇒ Redis
constructor
A new instance of Redis.
- #read_job_data_to_hash ⇒ Object
- #update_job_data_from_hash(hsh) ⇒ Object
- #workers_working? ⇒ Boolean
Constructor Details
#initialize(job_id) ⇒ Redis
Returns a new instance of Redis.
9 10 11 |
# File 'lib/rector/backends/redis.rb', line 9 def initialize(job_id) @job_id = job_id end |
Instance Attribute Details
#job_id ⇒ Object (readonly)
Returns the value of attribute job_id.
7 8 9 |
# File 'lib/rector/backends/redis.rb', line 7 def job_id @job_id end |
Instance Method Details
#add_worker(worker_id) ⇒ Object
34 35 36 |
# File 'lib/rector/backends/redis.rb', line 34 def add_worker(worker_id) redis.sadd(WORKER_LIST_SET, worker_id) end |
#cleanup ⇒ Object
46 47 48 49 |
# File 'lib/rector/backends/redis.rb', line 46 def cleanup redis.del(*keys) redis.del(KEY_LIST_SET, WORKER_LIST_SET) end |
#finish_worker(worker_id) ⇒ Object
38 39 40 |
# File 'lib/rector/backends/redis.rb', line 38 def finish_worker(worker_id) redis.srem(WORKER_LIST_SET, worker_id) end |
#read_job_data_to_hash ⇒ Object
30 31 32 |
# File 'lib/rector/backends/redis.rb', line 30 def read_job_data_to_hash Hash[keys.map { |k| [k, read(k)] }] end |
#update_job_data_from_hash(hsh) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rector/backends/redis.rb', line 13 def update_job_data_from_hash(hsh) redis.multi do redis.sadd(KEY_LIST_SET, *hsh.keys) hsh.each do |key, val| case val when Numeric redis.incrby(key, val) when Set redis.sadd(key, *val) when Enumerable redis.rpush(key, *val) end end end end |
#workers_working? ⇒ Boolean
42 43 44 |
# File 'lib/rector/backends/redis.rb', line 42 def workers_working? redis.scard(WORKER_LIST_SET).to_i > 0 end |