Class: Liquidbook::PidManager
- Inherits:
-
Object
- Object
- Liquidbook::PidManager
- Defined in:
- lib/liquidbook/pid_manager.rb
Constant Summary collapse
- PID_FILE_NAME =
"server.pid"
Instance Method Summary collapse
-
#ensure_can_start! ⇒ Object
Check the current state and ensure it’s safe to start Returns :ok, :stale_pid_cleaned, or raises an error.
-
#initialize(theme_root:) ⇒ PidManager
constructor
A new instance of PidManager.
- #pid_file_path ⇒ Object
-
#process_alive?(pid) ⇒ Boolean
Check if a process with the given PID is alive.
-
#read_pid ⇒ Object
Read the PID from the PID file, returns nil if not found.
-
#remove_pid ⇒ Object
Remove the PID file.
-
#stop! ⇒ Object
Stop the running server process Returns :stopped, :not_running, or :stale_pid_cleaned.
-
#write_pid ⇒ Object
Write the current process PID to the PID file.
Constructor Details
#initialize(theme_root:) ⇒ PidManager
Returns a new instance of PidManager.
7 8 9 |
# File 'lib/liquidbook/pid_manager.rb', line 7 def initialize(theme_root:) @theme_root = theme_root end |
Instance Method Details
#ensure_can_start! ⇒ Object
Check the current state and ensure it’s safe to start Returns :ok, :stale_pid_cleaned, or raises an error
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/liquidbook/pid_manager.rb', line 48 def ensure_can_start! pid = read_pid return :ok if pid.nil? if process_alive?(pid) raise Error, "Server is already running (PID: #{pid}). Run `liquidbook stop` to stop it." end remove_pid :stale_pid_cleaned end |
#pid_file_path ⇒ Object
11 12 13 |
# File 'lib/liquidbook/pid_manager.rb', line 11 def pid_file_path File.join(@theme_root, ".liquid-preview", PID_FILE_NAME) end |
#process_alive?(pid) ⇒ Boolean
Check if a process with the given PID is alive
36 37 38 39 40 41 42 43 44 |
# File 'lib/liquidbook/pid_manager.rb', line 36 def process_alive?(pid) Process.kill(0, pid) true rescue Errno::ESRCH false rescue Errno::EPERM # Process exists but we don't have permission to signal it true end |
#read_pid ⇒ Object
Read the PID from the PID file, returns nil if not found
23 24 25 26 27 28 |
# File 'lib/liquidbook/pid_manager.rb', line 23 def read_pid return nil unless File.exist?(pid_file_path) pid = File.read(pid_file_path).strip.to_i pid.positive? ? pid : nil end |
#remove_pid ⇒ Object
Remove the PID file
31 32 33 |
# File 'lib/liquidbook/pid_manager.rb', line 31 def remove_pid File.delete(pid_file_path) if File.exist?(pid_file_path) end |
#stop! ⇒ Object
Stop the running server process Returns :stopped, :not_running, or :stale_pid_cleaned
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/liquidbook/pid_manager.rb', line 62 def stop! pid = read_pid if pid.nil? return :not_running end unless process_alive?(pid) remove_pid return :stale_pid_cleaned end Process.kill("TERM", pid) remove_pid :stopped rescue Errno::EPERM raise Error, "Permission denied: cannot stop process (PID: #{pid})." end |
#write_pid ⇒ Object
Write the current process PID to the PID file
16 17 18 19 20 |
# File 'lib/liquidbook/pid_manager.rb', line 16 def write_pid dir = File.dirname(pid_file_path) FileUtils.mkdir_p(dir) unless File.directory?(dir) File.write(pid_file_path, Process.pid.to_s) end |