Skip to content

Commit

Permalink
Merge pull request #39 from rossta/feat/custom-systemctl-command
Browse files Browse the repository at this point in the history
Support configuration of custom systemctl status command
  • Loading branch information
fractaledmind authored Oct 22, 2024
2 parents af0863b + 22f5f7a commit 9b6588c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/litestream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def initialize
end
end

mattr_writer :username, :password, :queue, :replica_bucket, :replica_key_id, :replica_access_key
mattr_writer :username, :password, :queue, :replica_bucket, :replica_key_id, :replica_access_key, :systemctl_command

class << self
def verify!(database_path)
Expand Down Expand Up @@ -85,10 +85,14 @@ def replica_access_key
@@replica_access_key || configuration.replica_access_key
end

def systemctl_command
@@systemctl_command || "systemctl status litestream"
end

def replicate_process
info = {}
if !`which systemctl`.empty?
systemctl_status = `systemctl status litestream`.chomp
systemctl_status = `#{Litestream.systemctl_command}`.chomp
# ["● 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",
Expand Down
27 changes: 27 additions & 0 deletions test/test_litestream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
require "test_helper"

class TestLitestream < Minitest::Test
def teardown
Litestream.systemctl_command = nil
end

def test_that_it_has_a_version_number
refute_nil ::Litestream::VERSION
end
Expand All @@ -28,6 +32,29 @@ def test_replicate_process_systemd
end
end

def test_replicate_process_systemd_custom_command
stubbed_status = ["● myapp-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.systemctl_command = "systemctl --user status myapp-litestream.service"

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",
Expand Down

0 comments on commit 9b6588c

Please sign in to comment.