Skip to content

Commit c11fe23

Browse files
Merge pull request #63 from hschne/25-05-process-status
Fix replication process detection
2 parents 716a03c + 33cfa36 commit c11fe23

File tree

1 file changed

+66
-48
lines changed

1 file changed

+66
-48
lines changed

lib/litestream.rb

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -95,54 +95,7 @@ def config_path
9595
end
9696

9797
def replicate_process
98-
info = {}
99-
if !`which systemctl`.empty?
100-
systemctl_status = `#{Litestream.systemctl_command}`.chomp
101-
# ["● litestream.service - Litestream",
102-
# " Loaded: loaded (/lib/systemd/system/litestream.service; enabled; vendor preset: enabled)",
103-
# " Active: active (running) since Tue 2023-07-25 13:49:43 UTC; 8 months 24 days ago",
104-
# " Main PID: 1179656 (litestream)",
105-
# " Tasks: 9 (limit: 1115)",
106-
# " Memory: 22.9M",
107-
# " CPU: 10h 49.843s",
108-
# " CGroup: /system.slice/litestream.service",
109-
# " └─1179656 /usr/bin/litestream replicate",
110-
# "",
111-
# "Warning: some journal files were not opened due to insufficient permissions."]
112-
systemctl_status.split("\n").each do |line|
113-
line.strip!
114-
if line.start_with?("Main PID:")
115-
_key, value = line.split(":")
116-
pid, _name = value.strip.split(" ")
117-
info[:pid] = pid
118-
elsif line.start_with?("Active:")
119-
value, _ago = line.split(";")
120-
status, timestamp = value.split(" since ")
121-
info[:started] = DateTime.strptime(timestamp.strip, "%a %Y-%m-%d %H:%M:%S %Z")
122-
status_match = status.match(%r{\((?<status>.*)\)})
123-
info[:status] = status_match ? status_match[:status] : nil
124-
end
125-
end
126-
else
127-
litestream_replicate_ps = `ps -ax | grep litestream | grep replicate`.chomp
128-
litestream_replicate_ps.split("\n").each do |line|
129-
next unless line.include?("litestream replicate")
130-
pid, * = line.split(" ")
131-
info[:pid] = pid
132-
state, _, lstart = `ps -o "state,lstart" #{pid}`.chomp.split("\n").last.partition(/\s+/)
133-
134-
info[:status] = case state[0]
135-
when "I" then "idle"
136-
when "R" then "running"
137-
when "S" then "sleeping"
138-
when "T" then "stopped"
139-
when "U" then "uninterruptible"
140-
when "Z" then "zombie"
141-
end
142-
info[:started] = DateTime.strptime(lstart.strip, "%a %b %d %H:%M:%S %Y")
143-
end
144-
end
145-
info
98+
systemctl_info || process_info || {}
14699
end
147100

148101
def databases
@@ -162,6 +115,71 @@ def databases
162115
end
163116
end
164117
end
118+
119+
private
120+
121+
def systemctl_info
122+
return if `which systemctl`.empty?
123+
124+
systemctl_output = `#{Litestream.systemctl_command}`
125+
systemctl_exit_code = $?.exitstatus
126+
return unless systemctl_exit_code.zero?
127+
128+
# ["● litestream.service - Litestream",
129+
# " Loaded: loaded (/lib/systemd/system/litestream.service; enabled; vendor preset: enabled)",
130+
# " Active: active (running) since Tue 2023-07-25 13:49:43 UTC; 8 months 24 days ago",
131+
# " Main PID: 1179656 (litestream)",
132+
# " Tasks: 9 (limit: 1115)",
133+
# " Memory: 22.9M",
134+
# " CPU: 10h 49.843s",
135+
# " CGroup: /system.slice/litestream.service",
136+
# " └─1179656 /usr/bin/litestream replicate",
137+
# "",
138+
# "Warning: some journal files were not opened due to insufficient permissions."]
139+
140+
info = {}
141+
systemctl_output.chomp.split("\n").each do |line|
142+
line.strip!
143+
if line.start_with?("Main PID:")
144+
_key, value = line.split(":")
145+
pid, _name = value.strip.split(" ")
146+
info[:pid] = pid
147+
elsif line.start_with?("Active:")
148+
value, _ago = line.split(";")
149+
status, timestamp = value.split(" since ")
150+
info[:started] = DateTime.strptime(timestamp.strip, "%a %Y-%m-%d %H:%M:%S %Z")
151+
status_match = status.match(%r{\((?<status>.*)\)})
152+
info[:status] = status_match ? status_match[:status] : nil
153+
end
154+
end
155+
info
156+
end
157+
158+
def process_info
159+
litestream_replicate_ps = `ps -ax | grep litestream | grep replicate`
160+
exit_code = $?.exitstatus
161+
return unless exit_code.zero?
162+
163+
info = {}
164+
litestream_replicate_ps.chomp.split("\n").each do |line|
165+
next unless line.include?("litestream replicate")
166+
167+
pid, * = line.split(" ")
168+
info[:pid] = pid
169+
state, _, lstart = `ps -o "state,lstart" #{pid}`.chomp.split("\n").last.partition(/\s+/)
170+
171+
info[:status] = case state[0]
172+
when "I" then "idle"
173+
when "R" then "running"
174+
when "S" then "sleeping"
175+
when "T" then "stopped"
176+
when "U" then "uninterruptible"
177+
when "Z" then "zombie"
178+
end
179+
info[:started] = DateTime.strptime(lstart.strip, "%a %b %d %H:%M:%S %Y")
180+
end
181+
info
182+
end
165183
end
166184
end
167185

0 commit comments

Comments
 (0)