Class: BiblioTech::Backups::Pruner

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/bibliotech/backups/pruner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

log, log_level, logger, logger=, null_logger

Constructor Details

#initialize(config) ⇒ Pruner

Returns a new instance of Pruner.



9
10
11
# File 'lib/bibliotech/backups/pruner.rb', line 9

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/bibliotech/backups/pruner.rb', line 12

def config
  @config
end

Instance Method Details

#backup_needed?(time) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bibliotech/backups/pruner.rb', line 30

def backup_needed?(time)
  most_recent = most_recent()
  log.info{ "backup_needed?: most_recent is #{most_recent.path rescue "<nil>"}" }
  if most_recent.nil?
    log.warn{ "backup_needed?: no backups yet -> YES" }
    return true
  end

  difference = time - most_recent.timestamp
  freq_seconds = frequency * 60
  if difference >= freq_seconds
    log.warn{ "backup_needed?: check time: #{time.inspect} most_recent timestamp: #{most_recent.timestamp.inspect}" }
    log.warn{ "backup_needed?: delta: #{difference} frequency: #{freq_seconds} -> YES" }
    return true
  else
    log.info{ "backup_needed?: check time: #{time.inspect} most_recent timestamp: #{most_recent.timestamp.inspect}" }
    log.info{ "backup_needed?: delta: #{difference} frequency: #{freq_seconds} -> NO" }
    return false
  end
end

#delete(path) ⇒ Object



93
94
95
# File 'lib/bibliotech/backups/pruner.rb', line 93

def delete(path)
  File.unlink(path)
end

#filename_for(time) ⇒ Object



67
68
69
# File 'lib/bibliotech/backups/pruner.rb', line 67

def filename_for(time)
  PruneList.filename_for(name, time)
end

#frequencyObject



26
27
28
# File 'lib/bibliotech/backups/pruner.rb', line 26

def frequency
  @frequency ||= config.backup_frequency
end

#goObject



88
89
90
91
# File 'lib/bibliotech/backups/pruner.rb', line 88

def go
  return if schedules.empty?
  pruneable.each {|record| delete(record.path) }
end

#listObject



51
52
53
# File 'lib/bibliotech/backups/pruner.rb', line 51

def list
  @list ||= PruneList.new(path, name).list
end

#mark_listObject



55
56
57
58
59
# File 'lib/bibliotech/backups/pruner.rb', line 55

def mark_list
  schedules.each do |schedule|
    schedule.mark(list)
  end
end

#most_recentObject



61
62
63
64
65
# File 'lib/bibliotech/backups/pruner.rb', line 61

def most_recent
  list.max_by do |record|
    record.timestamp
  end
end

#nameObject



18
19
20
# File 'lib/bibliotech/backups/pruner.rb', line 18

def name
  @name ||= config.backup_name
end

#pathObject



14
15
16
# File 'lib/bibliotech/backups/pruner.rb', line 14

def path
  @path ||= config.backup_path
end

#pruneableObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bibliotech/backups/pruner.rb', line 71

def pruneable
  mark_list
  if list.empty?
    log.warn{ "No backup files in #{path} / #{name} !" }
  end
  list.each do |record|
    if record.keep
      log.info{ "#{record.path} #{record.timestamp} kept: #{record.scheduled_by.inspect}" }
    else
      log.warn{ "#{record.path} #{record.timestamp} discarding" }
    end
  end
  list.select do |record|
    !record.keep?
  end
end

#schedulesObject



22
23
24
# File 'lib/bibliotech/backups/pruner.rb', line 22

def schedules
  @schedules ||= config.prune_schedules
end