Sha256: ad0631684fecb74b22017d70910296ab4ce2ba1f286036ce4b3008ef90be418e

Contents?: true

Size: 1.38 KB

Versions: 15

Compression:

Stored size: 1.38 KB

Contents

#!/usr/bin/env ruby
#
# A simple example showing how to use Einhorn's shared-socket
# features. Einhorn translates the (addr:port[,flags...]) bind spec in
# into a file descriptor number in the EINHORN_FD_# environment
# variables.
#
# Invoke through Einhorn as
#
#     einhorn -b 127.0.0.1:2345,r ./time_server
#
# or, if you want to try out preloading:
#
#     einhorn -b 127.0.0.1:2345,r -p ./time_server ./time_server
require 'rubygems'
require 'einhorn/worker'

def log(msg)
  puts "=== [#{$$}] #{msg}"
end

def einhorn_main
  log "Called with ENV['EINHORN_FD_0']: #{ENV['EINHORN_FD_0']}"

  fd_num = Einhorn::Worker.socket!
  socket = Socket.for_fd(fd_num)

  sleep_before_shutdown = 0
  sleep_before_ack = 0

  # Came up successfully, so let's set up graceful handler and ACK the
  # master.
  Einhorn::Worker.graceful_shutdown do
    if sleep_before_shutdown > 0
      log "sleeping #{sleep_before_shutdown}s before shutdown"
      sleep sleep_before_shutdown
    end
    log "Goodbye!"
    exit(0)
  end

  if sleep_before_ack > 0
    log "sleeping #{sleep_before_ack}s before ack"
    sleep sleep_before_ack
  end
  log "worker ack"

  Einhorn::Worker.ack!

  # Real work happens here.
  begin
    while true
      accepted, _ = socket.accept
      accepted.write("[#{$$}] The current time is: #{Time.now}!\n")
      accepted.close
    end
  rescue Exception
  end
end

if $0 == __FILE__
  einhorn_main
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
einhorn-0.8.2 example/time_server
einhorn-0.7.4 example/time_server
einhorn-0.7.3 example/time_server
einhorn-0.7.2 example/time_server
einhorn-0.7.1 example/time_server
einhorn-0.7.0 example/time_server
einhorn-0.6.5 example/time_server
einhorn-0.6.4 example/time_server
einhorn-0.6.3 example/time_server
einhorn-0.6.2 example/time_server
einhorn-0.6.1 example/time_server
einhorn-0.6.0 example/time_server
einhorn-0.5.7 example/time_server
einhorn-0.5.6 example/time_server
einhorn-0.5.5 example/time_server