Class: Spring::Env
- Inherits:
-
Object
- Object
- Spring::Env
- Defined in:
- lib/spring/env.rb
Instance Attribute Summary collapse
-
#log_file ⇒ Object
readonly
Returns the value of attribute log_file.
Instance Method Summary collapse
- #app_name ⇒ Object
- #application_id ⇒ Object
-
#initialize(root = nil) ⇒ Env
constructor
A new instance of Env.
- #kill(sig) ⇒ Object
- #log(message) ⇒ Object
- #pid ⇒ Object
- #pidfile_path ⇒ Object
- #project_root ⇒ Object
- #root ⇒ Object
- #server_running? ⇒ Boolean
- #socket_name ⇒ Object
- #socket_path ⇒ Object
- #stop ⇒ Object
- #tmp_path ⇒ Object
- #version ⇒ Object
Constructor Details
Instance Attribute Details
#log_file ⇒ Object (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_name ⇒ Object
64 65 66 |
# File 'lib/spring/env.rb', line 64 def app_name root.basename end |
#application_id ⇒ Object
41 42 43 |
# File 'lib/spring/env.rb', line 41 def application_id Digest::MD5.hexdigest(RUBY_VERSION + project_root.to_s) end |
#kill(sig) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/spring/env.rb', line 102 def kill(sig) pid = self.pid Process.kill(sig, pid) if pid rescue Errno::ESRCH # already dead end |
#log(message) ⇒ Object
80 81 82 83 |
# File 'lib/spring/env.rb', line 80 def log() log_file.puts "[#{Time.now}] [#{Process.pid}] #{}" log_file.flush end |
#pid ⇒ Object
57 58 59 60 61 62 |
# File 'lib/spring/env.rb', line 57 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 ⇒ Object
53 54 55 |
# File 'lib/spring/env.rb', line 53 def pidfile_path tmp_path.join("#{application_id}.pid") end |
#project_root ⇒ Object
27 28 29 |
# File 'lib/spring/env.rb', line 27 def project_root @project_root ||= Spring.project_root_path end |
#root ⇒ Object
23 24 25 |
# File 'lib/spring/env.rb', line 23 def root @root ||= Spring.application_root_path end |
#server_running? ⇒ Boolean
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/spring/env.rb', line 68 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 ⇒ Object
49 50 51 |
# File 'lib/spring/env.rb', line 49 def socket_name socket_path.to_s end |
#socket_path ⇒ Object
45 46 47 |
# File 'lib/spring/env.rb', line 45 def socket_path tmp_path.join(application_id) end |
#stop ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/spring/env.rb', line 85 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 ⇒ Object
35 36 37 38 39 |
# File 'lib/spring/env.rb', line 35 def tmp_path path = Pathname.new(File.join(ENV['XDG_RUNTIME_DIR'] || Dir.tmpdir, "spring")) FileUtils.mkdir_p(path) unless path.exist? path end |