class Expire::PurgeService

Purge expired backups

Attributes

options[R]
path[R]

Public Class Methods

call(path, options) click to toggle source
# File lib/expire/purge_service.rb, line 6
def self.call(path, options)
  new(path, options).call
end
new(path, options) click to toggle source
# File lib/expire/purge_service.rb, line 10
def initialize(path, options)
  @options = options
  @path    = path
end

Public Instance Methods

call() click to toggle source
# File lib/expire/purge_service.rb, line 17
def call
  check_preconditions
  purge_expired_backups
rescue StandardError => e
  report.error(e.message)
  raise
end

Private Instance Methods

annotated_backup_list() click to toggle source
# File lib/expire/purge_service.rb, line 27
def annotated_backup_list
  @annotated_backup_list ||= rules.apply(backup_list, DateTime.now)
end
backup_list() click to toggle source
# File lib/expire/purge_service.rb, line 31
def backup_list
  @backup_list ||= GenerateBackupListService.call(path)
end
check_preconditions() click to toggle source
# File lib/expire/purge_service.rb, line 35
def check_preconditions
  raise NoBackupsError, "Can't find any backups" unless backup_list.any?
  raise NoRulesError, 'Will not purge without rules' unless rules.any?
  raise AllBackupsExpiredError, 'Will not delete all backups' if annotated_backup_list.keep_count < 1
end
merge_rules() click to toggle source
# File lib/expire/purge_service.rb, line 55
def merge_rules
  rules_file = options[:rules_file]
  file_rules = rules_file ? Rules.from_yaml(rules_file) : Rules.new

  option_rules = Rules.from_options(options.transform_keys(&:to_sym))
  file_rules.merge(option_rules)
end
purge_expired_backups() click to toggle source
# File lib/expire/purge_service.rb, line 63
def purge_expired_backups
  annotated_backup_list.sort.each do |backup|
    if backup.expired?
      report.before_purge(backup)
      purge_pathname(backup.pathname)
      report.after_purge(backup)
    else
      report.on_keep(backup)
    end
  end
end
purge_pathname(pathname) click to toggle source
# File lib/expire/purge_service.rb, line 75
def purge_pathname(pathname)
  return if options[:simulate]

  purge_command = options[:purge_command]

  if purge_command
    system("#{purge_command} #{pathname}")
  else
    pathname.unlink
  end
end
report() click to toggle source
# File lib/expire/purge_service.rb, line 41
def report
  @report ||= report_class.new
end
report_class() click to toggle source
# File lib/expire/purge_service.rb, line 45
def report_class
  wanted_format = options[:format]

  return ReportNull unless wanted_format
  return ReportNull if wanted_format == 'none'

  class_name = "::Expire::Report#{wanted_format.titleize}"
  class_name.safe_constantize or raise ArgumentError, "unknown format \"#{wanted_format}\""
end
rules() click to toggle source
# File lib/expire/purge_service.rb, line 87
def rules
  @rules ||= merge_rules
end