Sha256: 7b91cdf1c77a74e812dee886de8d84bf10d2df6d4e27d72d7336c830da98215f

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

module RedisRing

  class ShardAlreadyStarted < StandardError; end

  class ProcessManager

    include RedisRing::BackgroundThread

    def initialize
      @shards = {}
      @shards_to_stop = []
      @mutex = Mutex.new
    end

    def do_work
      monitor_processes
      sleep(0.5)
    end

    def after_halt
      shards.each do |shard_no, shard|
        if shard.alive?
          puts "Stopping shard #{shard_no}"
          shard.stop
        end
      end
    end

    def start_shard(shard)
      @mutex.synchronize do
        if shards.key?(shard.shard_number)
          raise ShardAlreadyStarted.new("Shard: #{shard.shard_number} already started!")
        end

        shards[shard.shard_number] = shard
      end
    end

    def stop_shard(shard)
      @mutex.synchronize do
        shards.delete(shard.shard_number)
        shards_to_stop << shard
      end
    end

    protected

    attr_reader :shards, :shards_to_stop

    def monitor_processes
      @mutex.synchronize do
        shards_to_stop.each do |shard|
          puts "Stopping shard #{shard.shard_number}"
          shard.stop
        end
        @shards_to_stop = []

        shards.each do |shard_no, shard|
          unless shard.alive?
            puts "Restarting shard #{shard_no}"
            shard.start
          end
        end
      end
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redis_ring-0.1.3 lib/redis_ring/process_manager.rb
redis_ring-0.1.2 lib/redis_ring/process_manager.rb
redis_ring-0.1.1 lib/redis_ring/process_manager.rb
redis_ring-0.1.0 lib/redis_ring/process_manager.rb