File: env.rb

package info (click to toggle)
ruby-spring 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 364 kB
  • ctags: 429
  • sloc: ruby: 2,762; makefile: 6
file content (84 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require "pathname"
require "fileutils"
require "digest/md5"
require "tmpdir"

require "spring/version"
require "spring/sid"
require "spring/configuration"

module Spring
  IGNORE_SIGNALS = %w(INT QUIT)

  class Env
    attr_reader :log_file

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

    def root
      @root ||= Spring.application_root_path
    end

    def project_root
      @project_root ||= Spring.project_root_path
    end

    def version
      Spring::VERSION
    end

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

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

    def socket_path
      tmp_path.join(application_id)
    end

    def socket_name
      socket_path.to_s
    end

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

    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

    def app_name
      root.basename
    end

    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

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