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(root = nil) ⇒ Env

Returns a new instance of Env.



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

def initialize(root = nil)
  @root         = root
  @project_root = root
  @log_file     = File.open(ENV["SPRING_LOG"] || "/dev/null", "a")
end

Instance Attribute Details

#log_fileObject (readonly)

Returns the value of attribute log_file.



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

def log_file
  @log_file
end

Instance Method Details

#app_nameObject



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

def app_name
  root.basename
end

#application_idObject



40
41
42
# File 'lib/spring/env.rb', line 40

def application_id
  Digest::MD5.hexdigest(project_root.to_s)
end

#log(message) ⇒ Object



79
80
81
82
# File 'lib/spring/env.rb', line 79

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

#pidObject



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

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



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

def pidfile_path
  tmp_path.join("#{application_id}.pid")
end

#project_rootObject



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

def project_root
  @project_root ||= Spring.project_root_path
end

#rootObject



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

def root
  @root ||= Spring.application_root_path
end

#server_running?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/spring/env.rb', line 67

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



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

def socket_name
  socket_path.to_s
end

#socket_pathObject



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

def socket_path
  tmp_path.join(application_id)
end

#tmp_pathObject



34
35
36
37
38
# File 'lib/spring/env.rb', line 34

def tmp_path
  path = Pathname.new(Dir.tmpdir + "/spring")
  FileUtils.mkdir_p(path) unless path.exist?
  path
end

#versionObject



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

def version
  Spring::VERSION
end