Module: Steep::Daemon

Defined in:
lib/steep/daemon.rb,
lib/steep/daemon/server.rb,
lib/steep/daemon/configuration.rb

Defined Under Namespace

Classes: Configuration, Server

Constant Summary collapse

SOCKET_DIR =
File.join(Dir.tmpdir, "steep-server")
LARGE_LOG_FILE_THRESHOLD =
10 * 1024 * 1024

Class Method Summary collapse

Class Method Details

.cleanupObject



62
63
64
65
66
67
68
# File 'lib/steep/daemon.rb', line 62

def cleanup
  [socket_path, pid_path].each do |path|
    File.delete(path)
  rescue Errno::ENOENT
    # File already deleted
  end
end

.configObject



15
16
17
# File 'lib/steep/daemon.rb', line 15

def config
  @config ||= Configuration.new
end

.log_pathObject



31
32
33
# File 'lib/steep/daemon.rb', line 31

def log_path
  config.log_path
end

.pid_pathObject



27
28
29
# File 'lib/steep/daemon.rb', line 27

def pid_path
  config.pid_path
end

.project_idObject



19
20
21
# File 'lib/steep/daemon.rb', line 19

def project_id
  config.project_id
end

.running?Boolean

Returns true when something is serving the socket, whether it is a daemon or a language server

The socket is the only reliable signal: a language server records its pid elsewhere, so requiring pid_path here would hide it from steep check and steep query.

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
# File 'lib/steep/daemon.rb', line 51

def running?
  return false unless File.exist?(socket_path)

  socket = UNIXSocket.new(socket_path)
  socket.close
  true
rescue Errno::ENOENT, Errno::ECONNREFUSED, Errno::ENOTSOCK
  false
end

.socket_pathObject



23
24
25
# File 'lib/steep/daemon.rb', line 23

def socket_path
  config.socket_path
end

.start(stderr:) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
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
107
108
109
110
111
112
113
114
# File 'lib/steep/daemon.rb', line 70

def start(stderr:)
  if pid = live_pid(pid_path)
    stderr.puts "Steep server already running (PID: #{pid})"
    return true
  end

  if running?
    stderr.puts "Another process is already serving this project (e.g. `steep langserver`)"
    return true
  end

  cleanup

  unless Steep.can_fork?
    stderr.puts "The daemon server is not supported on this platform (fork() is not available)"
    return false
  end

  child_pid = fork do
    Process.setsid
    daemon_pid = fork do
      File.write(pid_path, Process.pid.to_s)
      log_file = File.open(log_path, "a")
      log_file.sync = true
      $stdout.reopen(log_file)
      $stderr.reopen(log_file)
      $stdin.reopen("/dev/null")
      run_server(stderr:)
    end
    exit!(0) if daemon_pid
  end

  Process.waitpid(child_pid) if child_pid

  40.times do
    sleep 0.5
    next unless running?

    stderr.puts "Steep server started (PID: #{File.read(pid_path).strip})"
    return true
  end

  stderr.puts "Failed to start steep server. Check log: #{log_path}"
  false
end

.starting?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/steep/daemon.rb', line 35

def starting?
  return false unless File.exist?(pid_path)
  return false if File.exist?(socket_path)

  pid = File.read(pid_path).to_i
  Process.kill(0, pid)
  true
rescue Errno::ESRCH, Errno::ENOENT
  false
end

.status(stderr:) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/steep/daemon.rb', line 161

def status(stderr:)
  if running?
    unless pid = live_pid(pid_path)
      stderr.puts "This project is served by another process (e.g. `steep langserver`)"
      stderr.puts "  Socket: #{socket_path}"
      return
    end

    stderr.puts "Steep server running (PID: #{pid})"
    stderr.puts "  Socket: #{socket_path}"
    stderr.puts "  Log:    #{log_path}"

    if File.exist?(log_path)
      log_content = if File.size(log_path) > LARGE_LOG_FILE_THRESHOLD
                      # SAFE: log_path is controlled internally, no user input
                      `tail -n 20 #{log_path.shellescape}`
                    else
                      File.readlines(log_path).last(20).join
                    end

      if log_content.include?("Warm-up complete")
        stderr.puts "  Status: Ready"
      elsif log_content.include?("Warming up type checker")
        stderr.puts "  Status: Warming up (loading RBS environment)"
      else
        stderr.puts "  Status: Starting"
      end
    end
  else
    stderr.puts "Steep server is not running"

    if File.exist?(pid_path) || File.exist?(socket_path)
      stderr.puts "  (Found stale files - run 'steep server stop' to clean up)"
    end
  end
end

.stop(stderr:) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/steep/daemon.rb', line 116

def stop(stderr:)
  unless File.exist?(pid_path)
    if running?
      stderr.puts "Steep server is not running. This project is served by another process"
      stderr.puts "(e.g. `steep langserver`), which this command does not stop."
    else
      stderr.puts "Steep server is not running"
    end
    return
  end

  pid = File.read(pid_path).to_i
  Process.kill("TERM", pid)
  process_alive = true
  20.times do
    sleep 0.5
    Process.kill(0, pid)
  rescue Errno::ESRCH
    process_alive = false
    break
  end

  if process_alive
    Process.kill("KILL", pid)
    stderr.puts "Steep server did not stop gracefully, forcefully killed (PID: #{pid})"
  else
    stderr.puts "Steep server stopped (PID: #{pid})"
  end
  cleanup
rescue Errno::ESRCH
  # The pid file was stale. The socket may still belong to another live server,
  # which `cleanup` would break by deleting it.
  if running?
    begin
      File.delete(pid_path)
    rescue Errno::ENOENT
      # Already deleted
    end
    stderr.puts "Removed a stale pid file. This project is served by another process."
  else
    cleanup
    stderr.puts "Steep server was not running (cleaned up stale files)"
  end
end