class Snapsync::Cleanup

Attributes

policy[R]

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

Public Class Methods

new(policy) click to toggle source
# File lib/snapsync/cleanup.rb, line 7
def initialize(policy)
    @policy = policy
end

Public Instance Methods

cleanup(target, dry_run: false) click to toggle source
# File lib/snapsync/cleanup.rb, line 11
def cleanup(target, dry_run: false)
    snapshots = target.each_snapshot.to_a
    filtered_snapshots = policy.filter_snapshots(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) }
    filtered_snapshots << last_sync_point
    filtered_snapshots = filtered_snapshots.to_set

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