Module: LitestreamRails

Defined in:
lib/litestream_rails.rb,
lib/litestream_rails/engine.rb,
lib/litestream_rails/version.rb,
app/controllers/litestream_rails/processes_controller.rb,
app/controllers/litestream_rails/application_controller.rb,
app/controllers/litestream_rails/restorations_controller.rb

Defined Under Namespace

Classes: ApplicationController, Engine, ProcessesController, RestorationsController

Constant Summary collapse

VERSION =
"0.4.1"

Class Method Summary collapse

Class Method Details

.databasesObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/litestream_rails.rb', line 75

def databases
  databases = Litestream::Commands.databases

  databases.each do |db|
    generations = Litestream::Commands.generations(db["path"])
    snapshots = Litestream::Commands.snapshots(db["path"])
    db["path"] = db["path"].gsub(Rails.root.to_s, "[ROOT]")

    db["generations"] = generations.map do |generation|
      id = generation["generation"]
      replica = generation["name"]
      generation["snapshots"] = snapshots.select { |snapshot| snapshot["generation"] == id && snapshot["replica"] == replica }
        .map { |s| s.slice("index", "size", "created") }
      generation.slice("generation", "name", "lag", "start", "end", "snapshots")
    end
  end
end

.passwordObject

use method instead of attr_accessor to ensure this works if variable set after LitestreamRails is loaded



20
21
22
# File 'lib/litestream_rails.rb', line 20

def password
  @password ||= ENV["LITESTREAMRAILS_PASSWORD"] || @@password
end

.replicate_processObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/litestream_rails.rb', line 24

def replicate_process
  info = {}
  if !`which systemctl`.empty?
    systemctl_status = `systemctl status litestream`.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",
    #  "   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."]
    systemctl_status.split("\n").each do |line|
      line.strip!
      if line.start_with?("Main PID:")
        _key, value = line.split(":")
        pid, _name = value.strip.split(" ")
        info[:pid] = pid
      elsif line.start_with?("Active:")
        _key, value = line.split(":")
        value, _ago = value.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
      end
    end
  else
    litestream_replicate_ps = `ps -a | grep litestream | grep replicate`.chomp
    litestream_replicate_ps.split("\n").each do |line|
      next unless line.include?("litestream replicate")
      pid, * = line.split(" ")
      info[:pid] = pid
      state, _, lstart = `ps -o "state,lstart" #{pid}`.chomp.split("\n").last.partition(/\s+/)

      info[:status] = case state[0]
      when "I" then "idle"
      when "R" then "running"
      when "S" then "sleeping"
      when "T" then "stopped"
      when "U" then "uninterruptible"
      when "Z" then "zombie"
      end
      info[:started] = DateTime.strptime(lstart.strip, "%a %b %d %H:%M:%S %Y")
    end
  end
  info
end

.usernameObject

use method instead of attr_accessor to ensure this works if variable set after LitestreamRails is loaded



14
15
16
# File 'lib/litestream_rails.rb', line 14

def username
  @username ||= ENV["LITESTREAMRAILS_USERNAME"] || @@username
end