Skip to content

Commit 8889466

Browse files
committed
fix: replication process detection
1 parent 872ef21 commit 8889466

File tree

1 file changed

+65
-48
lines changed

1 file changed

+65
-48
lines changed

lib/litestream.rb

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -90,54 +90,7 @@ def systemctl_command
9090
end
9191

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

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

0 commit comments

Comments
 (0)