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



58
59
60
61
62
63
64
# File 'lib/steep/daemon.rb', line 58

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:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/steep/daemon.rb', line 46

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

  pid = File.read(pid_path).to_i
  Process.kill(0, pid)
  socket = UNIXSocket.new(socket_path)
  socket.close
  true
rescue Errno::ESRCH, 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



66
67
68
69
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
# File 'lib/steep/daemon.rb', line 66

def start(stderr:)
  if running?
    stderr.puts "Steep server already running (PID: #{File.read(pid_path).strip})"
    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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/steep/daemon.rb', line 136

def status(stderr:)
  if running?
    pid = File.read(pid_path).to_i
    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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/steep/daemon.rb', line 107

def stop(stderr:)
  unless File.exist?(pid_path)
    stderr.puts "Steep server is not running"
    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
  cleanup
  stderr.puts "Steep server was not running (cleaned up stale files)"
end