Class: BiblioTech::Backups::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/bibliotech/backups/scheduler.rb

Direct Known Subclasses

CalendarScheduler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, frequency, limit) ⇒ Scheduler

Returns a new instance of Scheduler.



8
9
10
11
12
# File 'lib/bibliotech/backups/scheduler.rb', line 8

def initialize(name, frequency, limit)
  @name = name
  @frequency, @limit = frequency, limit
  @limit = nil if limit == "all"
end

Instance Attribute Details

#frequencyObject

Returns the value of attribute frequency.



6
7
8
# File 'lib/bibliotech/backups/scheduler.rb', line 6

def frequency
  @frequency
end

#limitObject

Returns the value of attribute limit.



6
7
8
# File 'lib/bibliotech/backups/scheduler.rb', line 6

def limit
  @limit
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/bibliotech/backups/scheduler.rb', line 6

def name
  @name
end

Instance Method Details

#compute_earliest_time(file_list) ⇒ Object

The earliest possible time to keep a file in.



23
24
25
26
27
28
29
30
# File 'lib/bibliotech/backups/scheduler.rb', line 23

def compute_earliest_time(file_list)
  limit_time = Time.at(0)
  return limit_time if file_list.empty?
  unless limit.nil?
    limit_time = latest_time(file_list) - limit * freq_seconds
  end
  [limit_time, file_list.last.timestamp - range].max
end

#freq_secondsObject



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

def freq_seconds
  frequency * 60
end

#latest_time(file_list) ⇒ Object

The latest possible time to keep a file in.



33
34
35
36
# File 'lib/bibliotech/backups/scheduler.rb', line 33

def latest_time(file_list)
  return Time.at(0) if file_list.empty?
  file_list.first.timestamp
end

#mark(original_file_list) ⇒ Object

Working from the latest time backwards, mark the closest file to the appropriate frequencies as keepable



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/bibliotech/backups/scheduler.rb', line 48

def mark(original_file_list)
  file_list = original_file_list.sort_by{|record| -record.timestamp.to_i} #sort from newest to oldest

  time = latest_time(file_list)
  earliest_time = compute_earliest_time(file_list)
  oldest_file = file_list.last

  while time > earliest_time do
    file_list.delete_if do |record|
      record.timestamp > time
    end

    if file_list.empty?
      oldest_file.in_schedule(name)
      break
    end

    closest = file_list.first

    if (time - closest.timestamp) < freq_seconds
      closest.in_schedule(name)
    end
    time = step_back(time)
  end
  return original_file_list
end

#rangeObject



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

def range
  freq_seconds / 2
end

#step_back(time) ⇒ Object



38
39
40
# File 'lib/bibliotech/backups/scheduler.rb', line 38

def step_back(time)
  time - freq_seconds
end

#step_forward(time) ⇒ Object



42
43
44
# File 'lib/bibliotech/backups/scheduler.rb', line 42

def step_forward(time)
  time + freq_seconds
end