litebackup 1.3.0-aarch64-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,185 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Litebackup
6
+ # backups.once: one foreground litebackup backup job, as the CLI runs it.
7
+ module Foreground
8
+ # How many of the job's file diagnostics a failed backup's message lists.
9
+ MAXIMUM_FILE_DIAGNOSTICS = 20
10
+ # How long a job has to exit after SIGTERM before it gets SIGKILL.
11
+ STOP_GRACE_SECONDS = 15.0
12
+ # How long a job's error output may stay open after the job exited, in a
13
+ # process it started, before reading stops.
14
+ DRAIN_SECONDS = 1.0
15
+ # How often the wait for a job's exit checks it.
16
+ POLL_SECONDS = 0.05
17
+
18
+ class << self
19
+ # How long a stopped job has before SIGKILL, and how long past its
20
+ # timeout a job runs before it is killed; tests shorten them.
21
+ attr_accessor :stop_grace_seconds, :deadline_grace_seconds
22
+ end
23
+ self.stop_grace_seconds = STOP_GRACE_SECONDS
24
+ self.deadline_grace_seconds = Wire::DEADLINE_GRACE_SECONDS
25
+
26
+ module_function
27
+
28
+ # The litebackup command line of one foreground job.
29
+ def backup_command(binary, source, session:, name:, prefix:, durability:, timeout:)
30
+ raise Litebackup.invalid_argument("backup needs a path") unless Wire.supplied?(source)
31
+ raise Litebackup.invalid_argument("choose a name or a prefix") if !name.nil? && !prefix.nil?
32
+ raise Litebackup.invalid_argument("expected an exact name") if !name.nil? && (name.empty? || name.end_with?("/"))
33
+
34
+ Wire.check_durability(durability)
35
+ command = [binary, "backup", "--stdout-format", "json", "--durability", durability]
36
+ command += ["--session", session] if Wire.supplied?(session)
37
+ timeout_seconds = Wire.seconds(timeout, "the backup timeout")
38
+ command += ["--timeout", "#{(timeout_seconds * 1000).round}ms"] unless timeout_seconds.nil?
39
+ command += ["--", Wire.absolute_path(source)]
40
+ command << name unless name.nil?
41
+ command << (prefix.end_with?("/") ? prefix : "#{prefix}/") if Wire.supplied?(prefix)
42
+ command
43
+ end
44
+
45
+ # The job's structured error on this line of its error output, or nil for a log line.
46
+ def reported_failure(line)
47
+ parsed = JSON.parse(line)
48
+ parsed.is_a?(Hash) && parsed["code"].is_a?(String) && parsed["message"].is_a?(String) ? parsed : nil
49
+ rescue JSON::ParserError
50
+ nil
51
+ end
52
+
53
+ # The job's final structured report, followed by each file diagnostic it
54
+ # printed before it, or its text. Each file diagnostic names the file, and
55
+ # the machine that writes it for a busy one.
56
+ def backup_failure(stderr, exit_status)
57
+ final_report = nil
58
+ diagnostics = []
59
+ stderr.strip.each_line(chomp: true) do |line|
60
+ failure = reported_failure(line)
61
+ next if failure.nil?
62
+
63
+ final_report = failure
64
+ # A file reported twice is listed once, so the limit counts distinct files.
65
+ if diagnostics.length < MAXIMUM_FILE_DIAGNOSTICS && !diagnostics.include?(failure["message"])
66
+ diagnostics << failure["message"]
67
+ end
68
+ end
69
+ if final_report.nil?
70
+ text = stderr.strip
71
+ return Error.new("backup_failed",
72
+ text.empty? ? "foreground backup failed with exit status #{exit_status}" : text)
73
+ end
74
+
75
+ message = final_report["message"]
76
+ listed = [message]
77
+ diagnostics.each do |diagnostic|
78
+ next if listed.include?(diagnostic)
79
+
80
+ listed << diagnostic
81
+ message += "\n#{diagnostic}"
82
+ end
83
+ Error.new(final_report["code"], message)
84
+ end
85
+
86
+ # binary_not_found when the binary is missing, launch_failed when it
87
+ # cannot run. ENOENT while the file is there means its script interpreter
88
+ # is missing: the file cannot run.
89
+ def start_failure(binary, error)
90
+ not_found = error.is_a?(Errno::ENOENT) || error.is_a?(Errno::ENOTDIR)
91
+ code = not_found && Binary.missing_path?(binary) ? ErrorCode::BINARY_NOT_FOUND : ErrorCode::LAUNCH_FAILED
92
+ Error.new(code, Binary.start_failure_message(binary, error))
93
+ end
94
+
95
+ # A running job: its operating-system process and the thread reading its error output.
96
+ class Job
97
+ attr_reader :pid
98
+
99
+ def initialize(pid, error_output)
100
+ @pid = pid
101
+ @error_output = error_output
102
+ @text = +""
103
+ @reader = Thread.new { read_error_output }
104
+ @waiter = Process.detach(pid)
105
+ end
106
+
107
+ def exited?
108
+ !@waiter.alive?
109
+ end
110
+
111
+ def exit_status
112
+ @waiter.value
113
+ end
114
+
115
+ def signal(signal_name)
116
+ Process.kill(signal_name, pid)
117
+ rescue Errno::ESRCH
118
+ nil
119
+ end
120
+
121
+ # Stops a running job: SIGTERM, then SIGKILL when it has not exited within STOP_GRACE_SECONDS.
122
+ def stop
123
+ signal(:TERM)
124
+ return unless @waiter.join(Foreground.stop_grace_seconds).nil?
125
+
126
+ signal(:KILL)
127
+ @waiter.join
128
+ end
129
+
130
+ # Waits for the job to exit, killing it once deadline_seconds pass.
131
+ # Returns its error output and whether the deadline ended it. Reading
132
+ # stops DRAIN_SECONDS after the job exited even if a process it started
133
+ # keeps the output open.
134
+ def await(deadline_seconds)
135
+ timed_out = false
136
+ if @waiter.join(deadline_seconds).nil?
137
+ timed_out = true
138
+ signal(:KILL)
139
+ @waiter.join
140
+ end
141
+ if @reader.join(DRAIN_SECONDS).nil?
142
+ @error_output.close
143
+ @reader.join
144
+ end
145
+ [@text.dup.force_encoding(Encoding::UTF_8).scrub, timed_out]
146
+ end
147
+
148
+ private
149
+
150
+ def read_error_output
151
+ loop { @text << @error_output.readpartial(65_536) }
152
+ rescue IOError
153
+ nil
154
+ ensure
155
+ @error_output.close unless @error_output.closed?
156
+ end
157
+ end
158
+
159
+ # Starts a job, with its standard output discarded and its error output read.
160
+ def start_job(command, environment)
161
+ error_reader, error_writer = IO.pipe
162
+ begin
163
+ pid = Process.spawn(Launch.exact_environment(environment), *command, in: File::NULL, out: File::NULL,
164
+ err: error_writer)
165
+ rescue SystemCallError => e
166
+ error_reader.close
167
+ raise start_failure(command.first, e)
168
+ ensure
169
+ error_writer.close
170
+ end
171
+ Job.new(pid, error_reader)
172
+ end
173
+
174
+ # How long to wait for a job: its timeout plus the grace that lets the job
175
+ # report its own timeout first.
176
+ def job_deadline(timeout)
177
+ timeout_seconds = Wire.seconds(timeout, "the backup timeout")
178
+ timeout_seconds.nil? ? nil : timeout_seconds + Foreground.deadline_grace_seconds
179
+ end
180
+
181
+ def timed_out_backup
182
+ Error.new(ErrorCode::TIMEOUT, "the foreground backup did not finish within its timeout")
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Code generated from client-openapi.yaml. DO NOT EDIT.
4
+
5
+ module Litebackup
6
+ # The socket API spec's error codes; errors.rb's ErrorCode takes them.
7
+ module SpecErrorCodes
8
+ BUSY = "busy"
9
+ CANCELLED = "cancelled"
10
+ CHANGED = "changed"
11
+ CLIENT_UNSUPPORTED = "client_unsupported"
12
+ CONFLICT = "conflict"
13
+ CURSOR_EXPIRED = "cursor_expired"
14
+ DRAIN_REQUIRED = "drain_required"
15
+ FILE_EXISTS = "file_exists"
16
+ FORBIDDEN = "forbidden"
17
+ INTEGRITY = "integrity"
18
+ INTERNAL = "internal"
19
+ INVALID_ARGUMENT = "invalid_argument"
20
+ LAUNCH_FAILED = "launch_failed"
21
+ LIMIT_EXCEEDED = "limit_exceeded"
22
+ LOCK_LOST = "lock_lost"
23
+ LOCK_UNAVAILABLE = "lock_unavailable"
24
+ METHOD_NOT_ALLOWED = "method_not_allowed"
25
+ NOT_CONFIGURED = "not_configured"
26
+ NOT_DURABLE = "not_durable"
27
+ NOT_FOUND = "not_found"
28
+ PLAN_LIMIT = "plan_limit"
29
+ RULE_DATABASE = "rule_database"
30
+ RULE_EXISTS = "rule_exists"
31
+ SDK_UNSUPPORTED = "sdk_unsupported"
32
+ STILL_OPEN = "still_open"
33
+ TIMEOUT = "timeout"
34
+ UNAUTHENTICATED = "unauthenticated"
35
+ UNAVAILABLE = "unavailable"
36
+ WRONG_SERVER = "wrong_server"
37
+ end
38
+ end