Class: Rector::Backends::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/rector/backends/redis.rb

Constant Summary collapse

KEY_LIST_SET =
"__keys__"
WORKER_LIST_SET =
"__workers__"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_id) ⇒ Redis

Returns a new instance of Redis.



11
12
13
# File 'lib/rector/backends/redis.rb', line 11

def initialize(job_id)
  @job_id = job_id
end

Instance Attribute Details

#job_idObject (readonly)

Returns the value of attribute job_id.



9
10
11
# File 'lib/rector/backends/redis.rb', line 9

def job_id
  @job_id
end

Instance Method Details

#add_worker(worker_id) ⇒ Object



36
37
38
# File 'lib/rector/backends/redis.rb', line 36

def add_worker(worker_id)
  redis.sadd(WORKER_LIST_SET, worker_id)
end

#cleanupObject



48
49
50
51
# File 'lib/rector/backends/redis.rb', line 48

def cleanup
  redis.del(*keys)
  redis.del(KEY_LIST_SET, WORKER_LIST_SET)
end

#finish_worker(worker_id) ⇒ Object



40
41
42
# File 'lib/rector/backends/redis.rb', line 40

def finish_worker(worker_id)
  redis.srem(WORKER_LIST_SET, worker_id)
end

#read_job_data_to_hashObject



32
33
34
# File 'lib/rector/backends/redis.rb', line 32

def read_job_data_to_hash
  Hash[keys.map { |k| [k, read(k)] }]
end

#update_job_data_from_hash(hsh) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rector/backends/redis.rb', line 15

def update_job_data_from_hash(hsh)
  redis.multi do
    hsh.keys.each { |k| redis.sadd(KEY_LIST_SET, k) }

    hsh.each do |key, val|
      case val
      when Numeric
        redis.incrby(key, val)
      when Set
        val.each { |v| redis.sadd(key, v) }
      when Enumerable
        val.each { |v| redis.rpush(key, v) }
      end
    end
  end
end

#workers_working?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rector/backends/redis.rb', line 44

def workers_working?
  redis.scard(WORKER_LIST_SET).to_i > 0
end