Sha256: dbe1566ca0fc7f076f7385371c3cf7299b13975c908aee5e2e75e715d6f595a7

Contents?: true

Size: 1.99 KB

Versions: 7

Compression:

Stored size: 1.99 KB

Contents

require 'sequel'
require 'forwardable'

module Chronicle
  module ETL
    # Saves JobLogs to db and loads previous ones
    class JobLogger
      extend Forwardable

      def_delegators :@job_log, :start, :finish, :error, :log_transformation, :duration, :success
      attr_accessor :job_log

      # For a given `job_id`, return the last successful log
      def self.load_latest(job_id)
        with_db_connection do |db|
          attrs = db[:job_logs].reverse_order(:finished_at).where(success: true).first
          JobLog.build_from_serialized(attrs) if attrs
        end
      end

      def self.with_db_connection
        initialize_db unless db_exists?
        Sequel.connect("sqlite://#{db_filename}") do |db|
          initialize_schema(db) unless schema_exists?(db)
          yield db
        end
      end

      def self.db_exists?
        File.exists?(db_filename)
      end

      def self.schema_exists?(db)
        return db.tables.include? :job_logs
      end

      def self.db_filename
        data = Runcom::Data.new "chronicle/etl/job_log.db"
        filename = data.all[0].to_s
      end

      def self.initialize_db
        FileUtils.mkdir_p(File.dirname(db_filename))
      end

      def self.initialize_schema db
        db.create_table :job_logs do
          primary_key :id
          String :job_id, null: false
          String :last_id
          Time :highest_timestamp
          Integer :num_records_processed
          boolean :success, default: false
          Time :started_at
          Time :finished_at
        end
      end

      # Create a new JobLogger
      def initialize(job)
        @job_log = JobLog.new do |job_log|
          job_log.job = job
        end
      end

      # Save this JobLogger's JobLog to db
      def save
        return unless @job_log.save_log?

        JobLogger.with_db_connection do |db|
          dataset = db[:job_logs]
          dataset.insert(@job_log.serialize)
        end
      end

      def summarize
        @job_log.inspect
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
chronicle-etl-0.4.4 lib/chronicle/etl/job_logger.rb
chronicle-etl-0.4.3 lib/chronicle/etl/job_logger.rb
chronicle-etl-0.4.2 lib/chronicle/etl/job_logger.rb
chronicle-etl-0.4.1 lib/chronicle/etl/job_logger.rb
chronicle-etl-0.4.0 lib/chronicle/etl/job_logger.rb
chronicle-etl-0.3.1 lib/chronicle/etl/job_logger.rb
chronicle-etl-0.3.0 lib/chronicle/etl/job_logger.rb