Sha256: 844e3cc7ad54749bdf1ecb432f8925d3792933c7de03ca012aa4176796d2d034

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

#!/usr/bin/env ruby

require 'zk'

def new_stderr_logger
  Logger.new($stderr).tap { |l| l.level = Logger::DEBUG }
end

ZK.logger = new_stderr_logger

class WhatTheFork
  attr_reader :logger

  def initialize
    @zk = ZK.new
    @base_path = '/what-the-fork'
    @path_to_delete = "#{@base_path}/delete_me"
  end

  def setup_logs!
    Zookeeper.logger = ZK.logger = @logger = new_stderr_logger
  end

  def run
    setup_logs!

    @zk.mkdir_p(@path_to_delete)

    @zk.on_connected do |event|
      _debug  "on_connected: #{event.inspect}"
    end

    @zk.on_connecting do |event|
      _debug "on_connecting: #{event.inspect}"
    end

    @zk.on_expired_session do |event|
      _debug "on_expired_session: #{event.inspect}"
    end

    fork_it!

    @zk.block_until_node_deleted(@path_to_delete)
    _debug "exiting main process!"
  end

  def fork_it!
    pid = fork do
      setup_logs!

      _debug "closing zk"
      @zk.close!
      _debug "closed zk"
      @zk = ZK.new
      _debug "created new zk"

      @zk.delete(@path_to_delete)

      _debug "deleted path #{@path_to_delete}, closing new zk instance"

      @zk.close!

      _debug  "EXITING!!"
      exit 0
    end

    _, stat = Process.waitpid2(pid)
    _debug "child exited, stat: #{stat.inspect}"
  ensure
    if pid
      _debug "ensuring #{pid} is really dead"
      Process.kill(9, pid) rescue Errno::ESRCH
    end
  end

  def _debug(str)
    logger.debug { str }
  end
end

WhatTheFork.new.run if __FILE__ == $0

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
zk-1.2.0 spec/informal/what-the-fork.rb
zk-1.1.1 spec/informal/what-the-fork.rb
zk-1.1.0 spec/informal/what-the-fork.rb
zk-1.0.0 spec/informal/what-the-fork.rb
zk-1.0.0.rc.1 spec/informal/what-the-fork.rb
zk-0.9.1 spec/informal/what-the-fork.rb