Sha256: 9a7360e13f965fdcae02c7890dd6ac53fc3b3d7030001ebca97b020482b158c0

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module RedisRing

  class Shard

    attr_reader :shard_config, :pid

    def initialize(shard_config)
      @shard_config = shard_config
      @status = :stopped
    end

    def shard_number
      shard_config.shard_number
    end

    def host
      shard_config.host
    end

    def port
      shard_config.port
    end

    def status
      if @status == :stopped
        return alive? ? :stopping : :stopped
      elsif @status == :started
        return alive? ? :running : :dead
      else
        raise RuntimeException.new("Unknown status: #{@status.inspect}")
      end
    end

    def start
      shard_config.save
      @pid = fork_redis_server
      @status = :started
    end

    def stop
      send_kill_signal
      @status = :stopped
    end

    protected

    def alive?
      @pid && File.exist?("/proc/#{@pid}")
    end

    def fork_redis_server
      spawn(shard_config.redis_path, shard_config.config_file_name)
    end

    def send_kill_signal
      system("kill -QUIT #{pid}")
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis_ring-0.0.1 lib/redis_ring/shard.rb