class Alaska::Runtime

Attributes

debug[RW]
nodejs_cmd[RW]
pid[RW]
port[RW]
semaphore[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/alaska/runtime.rb, line 12
def initialize(opts = {})
  srand

  @debug = opts[:debug]
  @nodejs_cmd = "node"
  @pid = nil
  @semaphore = Mutex.new
end

Public Instance Methods

available?() click to toggle source
# File lib/alaska/runtime.rb, line 25
def available?
  ENV["PATH"].split(":").detect do |path|
    %w{
      nodejs
      node
    }.detect do |node|
      File.exist?(File.join(path, node)) || File.symlink?(File.join(path, node))
    end
  end
end
context_class() click to toggle source
# File lib/alaska/runtime.rb, line 40
def context_class
  Alaska::Context
end
deprecated?() click to toggle source
# File lib/alaska/runtime.rb, line 36
def deprecated?
  false
end
name() click to toggle source
# File lib/alaska/runtime.rb, line 21
def name
  "Alaska"
end
provision_socket() click to toggle source

NOTE: this should be thread-safe

# File lib/alaska/runtime.rb, line 45
def provision_socket
  ensure_startup unless @pid

  wait_socket = nil
  checks = 0
  max_retries = 12

  while checks < max_retries
    begin
      checks += 1
      wait_socket = UNIXSocket.new(@port)
      break
    rescue Errno::ENOENT, Errno::ECONNREFUSED, Errno::ENOTDIR
      wait_socket = nil
      sleep 0.5
    end
  end

  if checks >= max_retries
    ensure_shutdown
    raise ExecJS::RuntimeError, "unable to connect to alaska.js server"
  end

  wait_socket
end

Private Instance Methods

ensure_shutdown() click to toggle source
# File lib/alaska/runtime.rb, line 96
def ensure_shutdown
  return unless @pid

  Process.kill("TERM", @pid) rescue Errno::ECHILD
  Process.wait(@pid) rescue Errno::ECHILD

  @port = nil
  @pid = nil
end
ensure_startup() click to toggle source
# File lib/alaska/runtime.rb, line 73
def ensure_startup
  @semaphore.synchronize {
    return if @pid

    @port = begin
      tmpfile = Tempfile.new("alaska")
      path = tmpfile.path
      tmpfile.close
      tmpfile.unlink
      path
    end

    alaska_js_path = File.join(File.dirname(File.expand_path(__FILE__)), '../../alaska.js')
    command_options = [alaska_js_path, "--debug #{!!@debug}"] # --other --command-line --options --go --here

    @pid = Process.spawn({"PORT" => @port.to_s}, @nodejs_cmd, *command_options, {:err => :out})

    at_exit do
      ensure_shutdown
    end
  }
end