Class: Chronicle::ETL::JobLogger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/chronicle/etl/job_logger.rb

Overview

Saves JobLogs to db and loads previous ones

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job) ⇒ JobLogger

Create a new JobLogger



61
62
63
64
65
# File 'lib/chronicle/etl/job_logger.rb', line 61

def initialize(job)
  @job_log = JobLog.new do |job_log|
    job_log.job = job
  end
end

Instance Attribute Details

#job_logObject

Returns the value of attribute job_log.



12
13
14
# File 'lib/chronicle/etl/job_logger.rb', line 12

def job_log
  @job_log
end

Class Method Details

.db_exists?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/chronicle/etl/job_logger.rb', line 30

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

.db_filenameObject



38
39
40
41
# File 'lib/chronicle/etl/job_logger.rb', line 38

def self.db_filename
  base = Pathname.new(XDG::Data.new.home)
  base.join('job_log.db')
end

.initialize_dbObject



43
44
45
# File 'lib/chronicle/etl/job_logger.rb', line 43

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

.initialize_schema(db) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chronicle/etl/job_logger.rb', line 47

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

.load_latest(_job_id) ⇒ Object

For a given job_id, return the last successful log



15
16
17
18
19
20
# File 'lib/chronicle/etl/job_logger.rb', line 15

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

.schema_exists?(db) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/chronicle/etl/job_logger.rb', line 34

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

.with_db_connectionObject



22
23
24
25
26
27
28
# File 'lib/chronicle/etl/job_logger.rb', line 22

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

Instance Method Details

#saveObject

Save this JobLogger's JobLog to db



68
69
70
71
72
73
74
75
# File 'lib/chronicle/etl/job_logger.rb', line 68

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

#summarizeObject



77
78
79
# File 'lib/chronicle/etl/job_logger.rb', line 77

def summarize
  @job_log.inspect
end