Class: Spring::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/spring/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Env


17
18
19
20
21
# File 'lib/spring/env.rb', line 17

def initialize(options = {})
  @root         = options[:root]
  @project_root = options[:root]
  @log_file     = options[:log_file] || File.open(ENV["SPRING_LOG"] || File::NULL, "a")
end

Instance Attribute Details

#log_fileObject (readonly)

Returns the value of attribute log_file


15
16
17
# File 'lib/spring/env.rb', line 15

def log_file
  @log_file
end

Instance Method Details

#app_nameObject


67
68
69
# File 'lib/spring/env.rb', line 67

def app_name
  root.basename
end

#application_idObject


44
45
46
# File 'lib/spring/env.rb', line 44

def application_id
  ENV["SPRING_APPLICATION_ID"] || Digest::MD5.hexdigest(RUBY_VERSION + project_root.to_s)
end

#kill(sig) ⇒ Object


105
106
107
108
109
110
# File 'lib/spring/env.rb', line 105

def kill(sig)
  pid = self.pid
  Process.kill(sig, pid) if pid
rescue Errno::ESRCH
  # already dead
end

#log(message) ⇒ Object


83
84
85
86
# File 'lib/spring/env.rb', line 83

def log(message)
  log_file.puts "[#{Time.now}] [#{Process.pid}] #{message}"
  log_file.flush
end

#pidObject


60
61
62
63
64
65
# File 'lib/spring/env.rb', line 60

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_pathObject


56
57
58
# File 'lib/spring/env.rb', line 56

def pidfile_path
  Pathname.new(ENV["SPRING_PIDFILE"] || socket_path.dirname.join("#{socket_path.basename(".*")}.pid"))
end

#project_rootObject


27
28
29
# File 'lib/spring/env.rb', line 27

def project_root
  @project_root ||= Spring.project_root_path
end

#rootObject


23
24
25
# File 'lib/spring/env.rb', line 23

def root
  @root ||= Spring.application_root_path
end

#server_commandObject


112
113
114
# File 'lib/spring/env.rb', line 112

def server_command
  ENV["SPRING_SERVER_COMMAND"] || "#{File.expand_path("../../../bin/spring", __FILE__)} server --background"
end

#server_running?Boolean


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/spring/env.rb', line 71

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_nameObject


52
53
54
# File 'lib/spring/env.rb', line 52

def socket_name
  socket_path.to_s
end

#socket_pathObject


48
49
50
# File 'lib/spring/env.rb', line 48

def socket_path
  Pathname.new(ENV["SPRING_SOCKET"] || tmp_path.join(application_id))
end

#stopObject


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/spring/env.rb', line 88

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_pathObject


35
36
37
38
39
40
41
42
# File 'lib/spring/env.rb', line 35

def tmp_path
  path = Pathname.new(
    ENV["SPRING_TMP_PATH"] ||
      File.join(ENV['XDG_RUNTIME_DIR'] || Dir.tmpdir, "spring-#{Process.uid}")
  )
  FileUtils.mkdir_p(path) unless path.exist?
  path
end

#versionObject


31
32
33
# File 'lib/spring/env.rb', line 31

def version
  Spring::VERSION
end