Class: Snapsync::Cleanup

Inherits:
Object
  • Object
show all
Defined in:
lib/snapsync/cleanup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy) ⇒ Cleanup

Returns a new instance of Cleanup.



7
8
9
# File 'lib/snapsync/cleanup.rb', line 7

def initialize(policy)
    @policy = policy
end

Instance Attribute Details

#policyObject (readonly)

The underlying timeline policy object that we use to compute which snapshots to delete and which to keep



5
6
7
# File 'lib/snapsync/cleanup.rb', line 5

def policy
  @policy
end

Instance Method Details

#cleanup(target, dry_run: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/snapsync/cleanup.rb', line 11

def cleanup(target, dry_run: false)
    snapshots = target.each_snapshot.to_a
    filtered_snapshots = policy.filter_snapshots_to_sync(target, snapshots).to_set

    if filtered_snapshots.any? { |s| s.synchronization_point? }
        raise InvalidPolicy, "#{policy} returned a snapsync synchronization point in its results"
    end

    if filtered_snapshots.empty?
        raise InvalidPolicy, "#{policy} returned no snapshots"
    end

    last_sync_point = snapshots.
        sort_by(&:num).reverse.
        find { |s| s.synchronization_point_for?(target) }
    if !last_sync_point
        binding.pry
    end
    filtered_snapshots << last_sync_point
    filtered_snapshots = filtered_snapshots.to_set

    snapshots.sort_by(&:num).each do |s|
        if !filtered_snapshots.include?(s)
            target.delete(s, dry_run: dry_run)
        end
    end
end