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(options = {}) ⇒ Env
constructor
A new instance of Env.
- #kill(sig) ⇒ Object
- #log(message) ⇒ Object
- #pid ⇒ Object
- #pidfile_path ⇒ Object
- #project_root ⇒ Object
- #root ⇒ Object
- #server_command ⇒ Object
- #server_running? ⇒ Boolean
- #socket_name ⇒ Object
- #socket_path ⇒ Object
- #stop ⇒ Object
- #tmp_path ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Env
Returns a new instance of Env.
13 14 15 16 17 |
# File 'lib/spring/env.rb', line 13 def initialize( = {}) @root = [:root] @project_root = [:root] @log_file = [:log_file] || File.open(ENV["SPRING_LOG"] || File::NULL, "a") end |
Instance Attribute Details
#log_file ⇒ Object (readonly)
Returns the value of attribute log_file.
11 12 13 |
# File 'lib/spring/env.rb', line 11 def log_file @log_file end |
Instance Method Details
#app_name ⇒ Object
66 67 68 |
# File 'lib/spring/env.rb', line 66 def app_name root.basename end |
#application_id ⇒ Object
42 43 44 45 |
# 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) ⇒ Object
104 105 106 107 108 109 |
# 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) ⇒ Object
82 83 84 85 |
# File 'lib/spring/env.rb', line 82 def log() log_file.puts "[#{Time.now}] [#{Process.pid}] #{}" log_file.flush end |
#pid ⇒ Object
59 60 61 62 63 64 |
# 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 ⇒ Object
55 56 57 |
# 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 ⇒ Object
23 24 25 |
# File 'lib/spring/env.rb', line 23 def project_root @project_root ||= Spring.project_root_path end |
#root ⇒ Object
19 20 21 |
# File 'lib/spring/env.rb', line 19 def root @root ||= Spring.application_root_path end |
#server_command ⇒ Object
111 112 113 |
# File 'lib/spring/env.rb', line 111 def server_command ENV["SPRING_SERVER_COMMAND"] || "#{File.("../../../bin/spring", __FILE__)} server --background" end |
#server_running? ⇒ Boolean
70 71 72 73 74 75 76 77 78 79 80 |
# 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 ⇒ Object
51 52 53 |
# File 'lib/spring/env.rb', line 51 def socket_name socket_path.to_s end |
#socket_path ⇒ Object
47 48 49 |
# File 'lib/spring/env.rb', line 47 def socket_path Pathname.new(ENV["SPRING_SOCKET"] || tmp_path.join(application_id)) end |
#stop ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# 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 ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# 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 |