Skip to content

Commit

Permalink
Merge pull request #32 from rossta/fix/litestream-replicate-process
Browse files Browse the repository at this point in the history
Fix Litestream.replicate_process for systemd
  • Loading branch information
fractaledmind authored Aug 19, 2024
2 parents 8c965a3 + 79835fb commit 49c594a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/litestream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ def replicate_process
pid, _name = value.strip.split(" ")
info[:pid] = pid
elsif line.start_with?("Active:")
_key, value = line.split(":")
value, _ago = value.split(";")
value, _ago = line.split(";")
status, timestamp = value.split(" since ")
info[:started] = DateTime.strptime(timestamp.strip, "%Y-%m-%d %H:%M:%S %Z")
info[:status] = status.split("(").first.strip
info[:started] = DateTime.strptime(timestamp.strip, "%a %Y-%m-%d %H:%M:%S %Z")
status_match = status.match(%r{\((?<status>.*)\)})
info[:status] = status_match ? status_match[:status] : nil
end
end
else
Expand Down
52 changes: 52 additions & 0 deletions test/test_litestream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,56 @@ class TestLitestream < Minitest::Test
def test_that_it_has_a_version_number
refute_nil ::Litestream::VERSION
end

def test_replicate_process_systemd
stubbed_status = ["● litestream.service - Litestream",
" Loaded: loaded (/lib/systemd/system/litestream.service; enabled; vendor preset: enabled)",
" Active: active (running) since Tue 2023-07-25 13:49:43 UTC; 8 months 24 days ago",
" Main PID: 1179656 (litestream)",
" Tasks: 9 (limit: 1115)",
" Memory: 22.9M",
" CPU: 10h 49.843s",
" CGroup: /system.slice/litestream.service",
" └─1179656 /usr/bin/litestream replicate",
"",
"Warning: some journal files were not opened due to insufficient permissions."].join("\n")
Litestream.stub :`, stubbed_status do
info = Litestream.replicate_process

assert_equal info[:status], "running"
assert_equal info[:pid], "1179656"
assert_equal info[:started].class, DateTime
end
end

def test_replicate_process_ps
stubbed_ps_list = [
"40358 ttys008 0:01.11 ruby --yjit bin/rails litestream:replicate",
"40364 ttys008 0:00.07 /path/to/litestream-ruby/exe/architecture/litestream replicate --config /path/to/app/config/litestream.yml"
].join("\n")

stubbed_ps_status = [
"STAT STARTED",
"S+ Mon Jul 1 11:10:58 2024"
].join("\n")

stubbed_backticks = proc do |arg|
case arg
when "ps -a | grep litestream | grep replicate"
stubbed_ps_list
when %(ps -o "state,lstart" 40364)
stubbed_ps_status
else
""
end
end

Litestream.stub :`, stubbed_backticks do
info = Litestream.replicate_process

assert_equal info[:status], "sleeping"
assert_equal info[:pid], "40364"
assert_equal info[:started].class, DateTime
end
end
end

0 comments on commit 49c594a

Please sign in to comment.