class StaticRails::Server

Public Class Methods

new(site) click to toggle source
# File lib/static-rails/server.rb, line 5
def initialize(site)
  @site = site
  @ready = false
  @waits_for_connection = WaitsForConnection.new
end

Public Instance Methods

start() click to toggle source
# File lib/static-rails/server.rb, line 11
def start
  return if started?
  @pid = spawn_process
  set_at_exit_hook
  nil
end
started?() click to toggle source
# File lib/static-rails/server.rb, line 18
def started?
  return false unless @pid.present?

  begin
    Process.getpgid(@pid)
    true
  rescue Errno::ESRCH
    @ready = false
    false
  end
end
wait_until_ready() click to toggle source
# File lib/static-rails/server.rb, line 30
def wait_until_ready
  return if @ready
  @waits_for_connection.call(@site)
  @ready = true
end

Private Instance Methods

set_at_exit_hook() click to toggle source
# File lib/static-rails/server.rb, line 55
def set_at_exit_hook
  return if @at_exit_hook_set
  at_exit do
    if started?
      Rails.logger.info "=> Stopping #{@site.name} static server"
      Process.kill("INT", @pid)
    end
  end
  @at_exit_hook_set = true
end
spawn_process() click to toggle source
# File lib/static-rails/server.rb, line 38
def spawn_process
  options = {
    in: "/dev/null",
    out: "/dev/stdout",
    err: "/dev/stderr",
    close_others: true,
    chdir: StaticRails.config.app.root.join(@site.source_dir).to_s
  }

  Rails.logger.info "=> Starting #{@site.name} static server"
  Bundler.with_unbundled_env do
    Process.spawn(@site.env, @site.server_command, options).tap do |pid|
      Process.detach(pid)
    end
  end
end