class Spring::Env

Attributes

log_file[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/spring/env.rb, line 13
def initialize(options = {})
  @root         = options[:root]
  @project_root = options[:root]
  @log_file     = options[:log_file] || File.open(ENV["SPRING_LOG"] || File::NULL, "a")
end

Public Instance Methods

app_name() click to toggle source
# File lib/spring/env.rb, line 66
def app_name
  root.basename
end
application_id() click to toggle source
# File lib/spring/env.rb, line 42
def application_id
  require "digest/md5"
  ENV["SPRING_APPLICATION_ID"] || Digest::MD5.hexdigest(RUBY_VERSION + project_root.to_s)
end
kill(sig) click to toggle source
# File lib/spring/env.rb, line 104
def kill(sig)
  pid = self.pid
  Process.kill(sig, pid) if pid
rescue Errno::ESRCH
  # already dead
end
log(message) click to toggle source
# File lib/spring/env.rb, line 82
def log(message)
  log_file.puts "[#{Time.now}] [#{Process.pid}] #{message}"
  log_file.flush
end
pid() click to toggle source
# File lib/spring/env.rb, line 59
def pid
  pidfile_path.exist? ? pidfile_path.read.to_i : nil
rescue Errno::ENOENT
  # This can happen if the pidfile is removed after we check it
  # exists
end
pidfile_path() click to toggle source
# File lib/spring/env.rb, line 55
def pidfile_path
  Pathname.new(ENV["SPRING_PIDFILE"] || socket_path.dirname.join("#{socket_path.basename(".*")}.pid"))
end
project_root() click to toggle source
# File lib/spring/env.rb, line 23
def project_root
  @project_root ||= Spring.project_root_path
end
root() click to toggle source
# File lib/spring/env.rb, line 19
def root
  @root ||= Spring.application_root_path
end
server_command() click to toggle source
# File lib/spring/env.rb, line 111
def server_command
  ENV["SPRING_SERVER_COMMAND"] || "#{File.expand_path("../../../bin/spring", __FILE__)} server --background"
end
server_running?() click to toggle source
# File lib/spring/env.rb, line 70
def server_running?
  pidfile = pidfile_path.open('r+')
  !pidfile.flock(File::LOCK_EX | File::LOCK_NB)
rescue Errno::ENOENT
  false
ensure
  if pidfile
    pidfile.flock(File::LOCK_UN)
    pidfile.close
  end
end
socket_name() click to toggle source
# File lib/spring/env.rb, line 51
def socket_name
  socket_path.to_s
end
socket_path() click to toggle source
# File lib/spring/env.rb, line 47
def socket_path
  Pathname.new(ENV["SPRING_SOCKET"] || tmp_path.join(application_id))
end
stop() click to toggle source
# File lib/spring/env.rb, line 87
def stop
  if server_running?
    timeout = Time.now + STOP_TIMEOUT
    kill 'TERM'
    sleep 0.1 until !server_running? || Time.now >= timeout

    if server_running?
      kill 'KILL'
      :killed
    else
      :stopped
    end
  else
    :not_running
  end
end
tmp_path() click to toggle source
# File lib/spring/env.rb, line 31
def tmp_path
  require "tmpdir"
  path = Pathname.new(
    ENV["SPRING_TMP_PATH"] ||
      File.join(ENV['XDG_RUNTIME_DIR'] || Dir.tmpdir, "spring-#{Process.uid}")
  )
  require "fileutils"
  FileUtils.mkdir_p(path) unless path.exist?
  path
end
version() click to toggle source
# File lib/spring/env.rb, line 27
def version
  Spring::VERSION
end