Class: Expedite::Env

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: nil, app_name: nil, log_file: nil, bundler: true) ⇒ Env

The environment containing the target application.

The root and app_name are used to derive the socket.

Parameters:

  • root (String) (defaults to: nil)

    Path to the root directory.

  • app_name (String) (defaults to: nil)

    The name of the application.

  • log_file (IO) (defaults to: nil)

    Path to log file. If nil, logs are discarded.

  • bundler (Boolean) (defaults to: true)

    If true, ‘bundle exec` will be added in front of the server command. Defaults to true.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/expedite/env.rb', line 21

def initialize(root: nil, app_name: nil, log_file: nil, bundler: true)
  # Use realpath so that directories that are symlinked end up with
  # the same root. This is important for getting the correct socket.
  @root = File.realpath(root || Dir.pwd)
  @app_name = app_name || File.basename(@root)
  @log_file = log_file || File.open(File::NULL, "a")
  @bundler = bundler

  @application_id = Digest::SHA1.hexdigest(@root + "|" + @app_name)
  @tmp_path = nil

  # TODO: @applications should only be available in the server
  @applications = Server::ApplicationManager.new(self)
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



9
10
11
# File 'lib/expedite/env.rb', line 9

def app_name
  @app_name
end

#application_idObject

Returns the value of attribute application_id.



9
10
11
# File 'lib/expedite/env.rb', line 9

def application_id
  @application_id
end

#applicationsObject (readonly)

Returns the value of attribute applications.



10
11
12
# File 'lib/expedite/env.rb', line 10

def applications
  @applications
end

#bundlerObject

Returns the value of attribute bundler.



9
10
11
# File 'lib/expedite/env.rb', line 9

def bundler
  @bundler
end

#log_fileObject

Returns the value of attribute log_file.



9
10
11
# File 'lib/expedite/env.rb', line 9

def log_file
  @log_file
end

#rootObject

Returns the value of attribute root.



8
9
10
# File 'lib/expedite/env.rb', line 8

def root
  @root
end

Instance Method Details

#graceful_termination_timeoutObject



73
74
75
# File 'lib/expedite/env.rb', line 73

def graceful_termination_timeout
  2
end

#helper_pathObject



77
78
79
# File 'lib/expedite/env.rb', line 77

def helper_path
  Pathname.new(root).join("expedite_helper.rb")
end

#load_helperObject



81
82
83
84
85
86
87
# File 'lib/expedite/env.rb', line 81

def load_helper
  path = helper_path
  if path.exist?
    log "loading #{path}"
    load(path)
  end
end

#log(message) ⇒ Object



58
59
60
61
# File 'lib/expedite/env.rb', line 58

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

#pidfile_pathObject



54
55
56
# File 'lib/expedite/env.rb', line 54

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

#server_commandObject



63
64
65
66
67
68
69
70
71
# File 'lib/expedite/env.rb', line 63

def server_command
  bin_expedite = File.expand_path("../../../bin/expedite", __FILE__)
  cmd = if bundler
    "bundle exec #{bin_expedite}"
  else
    bin_expedite
  end
  "#{cmd} server --background"
end

#socket_pathObject



50
51
52
# File 'lib/expedite/env.rb', line 50

def socket_path
  tmp_path.join(application_id)
end

#tmp_pathObject



40
41
42
43
44
45
46
47
48
# File 'lib/expedite/env.rb', line 40

def tmp_path
  return @tmp_path unless @tmp_path.nil?

  require "tmpdir"
  path = Pathname.new(File.join(Dir.tmpdir, "expedite-#{Process.uid}"))
  require "fileutils"
  FileUtils.mkdir_p(path) unless path.exist?
  @tmp_path = path
end

#versionObject



36
37
38
# File 'lib/expedite/env.rb', line 36

def version
  Expedite::VERSION
end