Sha256: 463230521c4cfffad042efb855b3a9c4c8209c37ea7b0df43ca46d941a50be34
Contents?: true
Size: 1.55 KB
Versions: 2
Compression:
Stored size: 1.55 KB
Contents
require "set" module Rector module Backends class Redis KEY_LIST_SET = "__keys__" WORKER_LIST_SET = "__workers__" attr_reader :job_id def initialize(job_id) @job_id = job_id end 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 def read_job_data_to_hash Hash[keys.map { |k| [k, read(k)] }] end def add_worker(worker_id) redis.sadd(WORKER_LIST_SET, worker_id) end def finish_worker(worker_id) redis.srem(WORKER_LIST_SET, worker_id) end def workers_working? redis.scard(WORKER_LIST_SET).to_i > 0 end def cleanup redis.del(*keys) redis.del(KEY_LIST_SET, WORKER_LIST_SET) end private def redis @redis ||= ::Redis::Namespace.new(@job_id, redis: Rector.configuration.redis) end def keys redis.smembers(KEY_LIST_SET) end def read(key) case redis.type(key) when "string" redis.get(key).to_i when "set" Set.new(redis.smembers(key)) when "list" redis.lrange(key, 0, -1) end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rector-0.0.4 | lib/rector/backends/redis.rb |
rector-0.0.3 | lib/rector/backends/redis.rb |