class Mrt::Ingest::OneTimeServer

Attributes

dir[R]
port[R]

Public Class Methods

new() click to toggle source
# File lib/mrt/ingest/one_time_server.rb, line 28
def initialize
  @dir = Dir.mktmpdir
  @mutex = Mutex.new
  @known_paths = {}
  @requested = {}
  @port = get_open_port
  @file_callback = ->(req, _res) { @requested[req.path] ||= true }
  @server = WEBrick::HTTPServer.new(Port: @port)
  @server.mount('/', WEBrick::HTTPServlet::FileHandler, @dir, FileCallback: @file_callback)
end

Public Instance Methods

add_file(sourcefile = nil) { |f| ... } click to toggle source

Add a file to this server. Returns the URL to use to fetch the file & the file path

# File lib/mrt/ingest/one_time_server.rb, line 65
def add_file(sourcefile = nil)
  fullpath = temppath
  path = File.basename(fullpath)

  if sourcefile
    @server.mount("/#{path}", WEBrick::HTTPServlet::FileHandler, sourcefile.path, FileCallback: @file_callback)
  else
    File.open(fullpath, 'w+') { |f| yield f }
  end
  ["http://#{Socket.gethostname}:#{@port}/#{path}", fullpath]
end
finished?() click to toggle source

Return true if each file has been served.

# File lib/mrt/ingest/one_time_server.rb, line 40
def finished?
  Dir.entries(@dir).each do |entry|
    next if %w[. ..].include?(entry)
    return false if @requested["/#{entry}"].nil?
  end
  true
end
get_open_port(start = 8081) click to toggle source

Find an open port, starting with start and adding one until we get an open port

# File lib/mrt/ingest/one_time_server.rb, line 15
def get_open_port(start = 8081)
  try_port = start
  loop do
    begin
      s = TCPServer.open(try_port)
      s.close
      return try_port
    rescue Errno::EADDRINUSE
      try_port += 1
    end
  end
end
join_server() click to toggle source

Wait for server to finish serving all files.

# File lib/mrt/ingest/one_time_server.rb, line 94
def join_server
  # ensure that each file is requested once before shutting down
  sleep(1) until finished?
  @server.shutdown
  @thread.join
end
run() click to toggle source

Run the server and wait until each file has been served once. Cleans up files before it returns.

# File lib/mrt/ingest/one_time_server.rb, line 103
def run
  start_server
  join_server
  #    FileUtils.rm_rf(@dir)
  nil
end
start_server() click to toggle source
# File lib/mrt/ingest/one_time_server.rb, line 77
def start_server
  if @thread.nil?
    @thread = Thread.new do
      @server.start
    end
  end
  sleep(0.1) while @server.status != :Running
  @thread
end
stop_server() click to toggle source

Stop server unconditionally.

# File lib/mrt/ingest/one_time_server.rb, line 88
def stop_server
  @server.shutdown
  @thread.join
end
temppath() click to toggle source
# File lib/mrt/ingest/one_time_server.rb, line 48
def temppath
  tmpfile = Tempfile.new('tmp', @dir)
  tmppath = tmpfile.path
  tmpfile.close!
  @mutex.synchronize do
    unless @known_paths.key?(tmppath)
      # no collision
      @known_paths[tmppath] = true
      return tmppath
    end
  end
  # need to retry, there was a collision
  temppath
end