Sha256: 6745890d69c38bf1ba9b3b27412f2b71ec95bd84b463ee6f51b1c9ca1b5ddb09

Contents?: true

Size: 1.82 KB

Versions: 27

Compression:

Stored size: 1.82 KB

Contents

# encoding: utf-8

module Backup
  module Storage
    module Cycler
      class Error < Backup::Error; end

      private

      # Adds the current package being stored to the YAML cycle data file
      # and will remove any old package file(s) when the storage limit
      # set by #keep is exceeded.
      def cycle!
        Logger.info 'Cycling Started...'

        packages = yaml_load.unshift(package)
        excess = packages.count - keep.to_i

        if excess > 0
          packages.pop(excess).each do |pkg|
            begin
              remove!(pkg) unless pkg.no_cycle
            rescue => err
              Logger.warn Error.wrap(err, <<-EOS)
                There was a problem removing the following package:
                Trigger: #{pkg.trigger} :: Dated: #{pkg.time}
                Package included the following #{ pkg.filenames.count } file(s):
                #{ pkg.filenames.join("\n") }
              EOS
            end
          end
        end

        yaml_save(packages)
      end

      # Returns path to the YAML data file.
      def yaml_file
        @yaml_file ||= begin
          filename = self.class.to_s.split('::').last
          filename << "-#{ storage_id }" if storage_id
          File.join(Config.data_path, package.trigger, "#{ filename }.yml")
        end
      end

      # Returns stored Package objects, sorted by #time descending (oldest last).
      def yaml_load
        if File.exist?(yaml_file) && !File.zero?(yaml_file)
          YAML.load_file(yaml_file).sort_by!(&:time).reverse!
        else
          []
        end
      end

      # Stores the given package objects to the YAML data file.
      def yaml_save(packages)
        FileUtils.mkdir_p(File.dirname(yaml_file))
        File.open(yaml_file, 'w') do |file|
          file.write(packages.to_yaml)
        end
      end

    end
  end
end

Version data entries

27 entries across 27 versions & 5 rubygems

Version Path
backup-4.2.0 lib/backup/storage/cycler.rb
backup-4.1.12 lib/backup/storage/cycler.rb
backup-4.1.11 lib/backup/storage/cycler.rb
backup-ssh-4.1.10 lib/backup/storage/cycler.rb
backup-4.1.10 lib/backup/storage/cycler.rb
backup-4.1.9 lib/backup/storage/cycler.rb
backup-4.1.8 lib/backup/storage/cycler.rb
backup-4.1.7 lib/backup/storage/cycler.rb
backup-4.1.6 lib/backup/storage/cycler.rb
backup-4.1.5 lib/backup/storage/cycler.rb
backup-4.1.4 lib/backup/storage/cycler.rb
backup-4.1.3 lib/backup/storage/cycler.rb
venet-backup-4.1.3 lib/backup/storage/cycler.rb
backup-4.1.2 lib/backup/storage/cycler.rb
backup-4.1.1 lib/backup/storage/cycler.rb
backup-4.1.0 lib/backup/storage/cycler.rb
backup-4.0.7 lib/backup/storage/cycler.rb
backup-4.0.6 lib/backup/storage/cycler.rb
backup-4.0.5 lib/backup/storage/cycler.rb
backup-4.0.4 lib/backup/storage/cycler.rb