Module: PgDrive::Dump

Defined in:
lib/pg_drive/dump.rb

Class Method Summary collapse

Class Method Details

.backup_timeout_secondsObject



42
43
44
# File 'lib/pg_drive/dump.rb', line 42

def backup_timeout_seconds
  DEFAULT_BACKUP_TIMEOUT_SECONDS
end

.callObject



4
5
6
7
8
9
10
# File 'lib/pg_drive/dump.rb', line 4

def call
  stdin, out_and_error, wait_thr = exec_pg_dump
  read_with_timeout(out_and_error, wait_thr)
ensure
  stdin&.close
  out_and_error&.close
end

.db_confObject



38
39
40
# File 'lib/pg_drive/dump.rb', line 38

def db_conf
  @db_conf ||= Rails.configuration.database_configuration[Rails.env]
end

.exec_pg_dumpObject



26
27
28
29
30
31
32
# File 'lib/pg_drive/dump.rb', line 26

def exec_pg_dump
  Open3.popen2e(
    pg_env,
    BACKUP_CMD,
    pgroup: true
  )
end

.pg_envObject



34
35
36
# File 'lib/pg_drive/dump.rb', line 34

def pg_env
  PG_ENV_MAP.map { |k, v| [k, db_conf[v].to_s] }.to_h
end

.read_with_timeout(input, wait_thr) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pg_drive/dump.rb', line 12

def read_with_timeout(input, wait_thr)
  result = Timeout.timeout(backup_timeout_seconds) { input.read }

  unless wait_thr.value.success? || result.blank?
    raise BackupFailed, "Exit status: #{wait_thr.value.exitstatus}: #{result}"
  end
  result
rescue Timeout::Error
  if (kill_pid = wait_thr[:pid])
    Process.kill(9, Process.getpgid(kill_pid))
  end
  raise BackupFailed, "Timeout error for backup_command #{kill_pid}"
end