Sha256: de7cf46e3d277563d19e11aa25c4d2ab33b7ee3acc5fed16c446e41d010be941

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'sequel'
require 'forwardable'

require 'pry'

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

      def_delegators :@job_log, :start, :finish, :log_transformation

      # 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

      # 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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronicle-etl-0.2.4 lib/chronicle/etl/job_logger.rb