Class: Siding::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/siding/client.rb

Constant Summary collapse

ACTIVE_ENVIRONMENTS =
%w[development test].freeze
DISABLE_OFF_VALUES =
["0", "false", "no", "off", ""].freeze
DEFAULT_BOOT_TIMEOUT =
90.0
NOTICE_AFTER =
0.75
FORWARDED_SIGNALS =
%w[INT TERM QUIT TSTP WINCH].freeze
SUSPEND =
"TSTP"
SIGNAL_LINES =
FORWARDED_SIGNALS.to_h { |name| [name, "#{name}\n".freeze] }.freeze
RESTART =
:restart
MAX_HANDOVERS =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, env: ENV, cwd: Dir.pwd) ⇒ Client

Returns a new instance of Client.



26
27
28
29
30
31
# File 'lib/siding/client.rb', line 26

def initialize(argv, env: ENV, cwd: Dir.pwd)
  @argv = argv.dup
  @env = env
  @cwd = cwd
  @logger = Logger.new(env: env)
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



24
25
26
# File 'lib/siding/client.rb', line 24

def argv
  @argv
end

#cwdObject (readonly)

Returns the value of attribute cwd.



24
25
26
# File 'lib/siding/client.rb', line 24

def cwd
  @cwd
end

#envObject (readonly)

Returns the value of attribute env.



24
25
26
# File 'lib/siding/client.rb', line 24

def env
  @env
end

#loggerObject (readonly)

Returns the value of attribute logger.



24
25
26
# File 'lib/siding/client.rb', line 24

def logger
  @logger
end

Class Method Details

.discover_app_root(start) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/siding/client.rb', line 59

def self.discover_app_root(start)
  dir = File.expand_path(start)
  loop do
    return dir if File.file?(File.join(dir, "config", "application.rb"))

    parent = File.dirname(dir)
    return nil if parent == dir

    dir = parent
  end
end

Instance Method Details

#app_envObject



79
# File 'lib/siding/client.rb', line 79

def app_env = ProjectKey.app_env_from(env)

#app_rootObject



53
54
55
56
57
# File 'lib/siding/client.rb', line 53

def app_root
  return @app_root if defined?(@app_root)

  @app_root = self.class.discover_app_root(cwd)
end

#decline_reasonObject



108
109
110
111
112
# File 'lib/siding/client.rb', line 108

def decline_reason
  return "#{argv.first} is not in the accelerated set" unless CLI.accelerated?(argv)

  unusable_reason
end

#project_keyObject



71
72
73
# File 'lib/siding/client.rb', line 71

def project_key
  @project_key ||= app_root && ProjectKey.for(app_root, env:)
end

#runObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/siding/client.rb', line 33

def run
  return CLI.new(argv, self).run if CLI.management?(argv.first) || argv.empty?

  reason = decline_reason
  if reason
    logger.debug("running unaccelerated: #{reason}")
    return passthrough
  end

  accelerated_run
rescue Protocol::VersionMismatch => e
  logger.debug(e.message)
  replace_mismatched_server
rescue StandardError => e
  logger.debug("running unaccelerated after #{e.class}: #{e.message}")
  passthrough
ensure
  logger.close
end

#runtimeObject



75
76
77
# File 'lib/siding/client.rb', line 75

def runtime
  @runtime ||= project_key && Runtime.for(project_key, env:)
end

#unusable_reasonObject



114
115
116
117
118
119
120
121
# File 'lib/siding/client.rb', line 114

def unusable_reason
  return "SIDING_DISABLE is set" if disabled?
  return Platform.unsupported_reason unless Platform.supported?
  return "no Rails application found above #{cwd}" if app_root.nil?
  return "#{app_env.inspect} is not an accelerated environment" unless ACTIVE_ENVIRONMENTS.include?(app_env)

  runtime.unavailable_reason
end

#warm_upObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/siding/client.rb', line 81

def warm_up
  reason = unusable_reason
  if reason
    logger.debug("not warming up: #{reason}")
    return false
  end

  runtime.prepare
  # The connection attempt under the boot lock is the only authoritative answer to "was one
  # already warm?", so the distinction is carried out from there rather than reconstructed by
  # the caller from a record that can go stale between the two reads.
  outcome, socket = with_boot_lock do
    connected = try_connect
    next [:already_warm, connected] if connected

    runtime.discard_socket
    [:booted, boot_server]
  end
  return false if socket.nil?

  socket.close
  outcome
rescue StandardError => e
  logger.debug("could not warm up: #{e.class}: #{e.message}")
  false
end