<commit_before>require 'aws-sdk'
require 'rufus-scheduler'
require 'backupsss/tar'
require 'backupsss/backup'
require 'backupsss/backup_dir'
require 'backupsss/backup_bucket'
require 'backupsss/janitor'
require 'backupsss/version'
require 'backupsss/configuration'

# A utility for backing things up to S3.
module Backupsss
  class << self
    def config
      @config ||= Backupsss::Configuration.new
    end

    def call
      push_backup(*prep_for_backup)
      cleanup_local
      cleanup_remote
    end

    def prep_for_backup
      filename = "#{Time.now.to_i}.tar"
      backup   = Backupsss::Backup.new(
        {
          s3_bucket_prefix: config.s3_bucket_prefix,
          s3_bucket:        config.s3_bucket,
          filename:         filename
        }, Aws::S3::Client.new(region: config.aws_region)
      )

      [filename, backup]
    end

    def push_backup(filename, backup)
      puts 'Create and Upload Tar: Starting'
      backup.put_file(
        Backupsss::Tar.new(
          config.backup_src_dir,
          "#{config.backup_dest_dir}/#{filename}"
        ).make
      )
      puts 'Create and Upload Tar: Finished'
    end

    def cleanup_local
      local_janitor = Janitor.new(
        driver: BackupDir.new(dir: config.backup_dest_dir)
      )
      local_janitor.rm_garbage(local_janitor.sift_trash)
    end

    def cleanup_remote
      remote_janitor = Janitor.new(
        driver: BackupBucket.new(
          dir: "#{config.s3_bucket}/#{config.s3_bucket_prefix}",
          region: config.aws_region
        ),
        retention_count: config.remote_retention
      )
      remote_janitor.rm_garbage(remote_janitor.sift_trash)
    end

    def run
      scheduler = Rufus::Scheduler.new
      scheduler.cron(config.backup_freq, blocking: true) do
        begin
          call
        rescue => exc
          $stderr.puts "ERROR - backup failed: #{exc.message}"
          $stderr.puts exc.backtrace.join("\n\t")
        end
      end
      scheduler.join
    end
  end
end<commit_msg>feat(backupsss.rb): add runner with oneshot logic<commit_after>require 'aws-sdk'
require 'rufus-scheduler'
require 'backupsss/tar'
require 'backupsss/backup'
require 'backupsss/backup_dir'
require 'backupsss/backup_bucket'
require 'backupsss/janitor'
require 'backupsss/version'
require 'backupsss/configuration'

# A utility for backing things up to S3.
module Backupsss
  class Runner
    attr_accessor :config

    def initialize
      @config = Backupsss::Configuration.new
    end

    def run
      config.backup_freq ? run_scheduled : run_oneshot
    end

    private
    def call
      push_backup(*prep_for_backup)
      cleanup_local
      cleanup_remote
    end

    def prep_for_backup
      filename = "#{Time.now.to_i}.tar"
      backup   = Backupsss::Backup.new(
        {
          s3_bucket_prefix: config.s3_bucket_prefix,
          s3_bucket:        config.s3_bucket,
          filename:         filename
        }, Aws::S3::Client.new(region: config.aws_region)
      )

      [filename, backup]
    end

    def push_backup(filename, backup)
      puts 'Create and Upload Tar: Starting'
      backup.put_file(
        Backupsss::Tar.new(
          config.backup_src_dir,
          "#{config.backup_dest_dir}/#{filename}"
        ).make
      )
      puts 'Create and Upload Tar: Finished'
    end

    def cleanup_local
      local_janitor = Janitor.new(
        driver: BackupDir.new(dir: config.backup_dest_dir)
      )
      local_janitor.rm_garbage(local_janitor.sift_trash)
    end

    def cleanup_remote
      remote_janitor = Janitor.new(
        driver: BackupBucket.new(
          dir: "#{config.s3_bucket}/#{config.s3_bucket_prefix}",
          region: config.aws_region
        ),
        retention_count: config.remote_retention
      )
      remote_janitor.rm_garbage(remote_janitor.sift_trash)
    end


    def run_scheduled
      $stdout.puts "Schedule provided, running with #{config.backup_freq}"

      scheduler = Rufus::Scheduler.new
      scheduler.cron(config.backup_freq, blocking: true){ make_call }
      scheduler.join
    end

    def run_oneshot
      $stdout.puts "No Schedule provided, running one time task"

      make_call
    end

    def make_call
      begin
        call
      rescue => exc
        $stderr.puts "ERROR - backup failed: #{exc.message}"
        $stderr.puts exc.backtrace.join("\n\t")
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08981628487185303, 'fraction_numerical': 0.0036289408029031526, 'mean_word_length': 2.431906614785992, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '716458', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1417, 'n_words': 287}