Class: Chronicle::ETL::JobLogger
- Inherits:
-
Object
- Object
- Chronicle::ETL::JobLogger
- Extended by:
- Forwardable
- Defined in:
- lib/chronicle/etl/job_logger.rb
Overview
Saves JobLogs to db and loads previous ones
Instance Attribute Summary collapse
-
#job_log ⇒ Object
Returns the value of attribute job_log.
Class Method Summary collapse
- .db_exists? ⇒ Boolean
- .db_filename ⇒ Object
- .initialize_db ⇒ Object
- .initialize_schema(db) ⇒ Object
-
.load_latest(_job_id) ⇒ Object
For a given
job_id
, return the last successful log. - .schema_exists?(db) ⇒ Boolean
- .with_db_connection ⇒ Object
Instance Method Summary collapse
-
#initialize(job) ⇒ JobLogger
constructor
Create a new JobLogger.
-
#save ⇒ Object
Save this JobLogger's JobLog to db.
- #summarize ⇒ Object
Constructor Details
Instance Attribute Details
#job_log ⇒ Object
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
30 31 32 |
# File 'lib/chronicle/etl/job_logger.rb', line 30 def self.db_exists? File.exist?(db_filename) end |
.db_filename ⇒ Object
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_db ⇒ Object
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
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_connection ⇒ Object
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
#save ⇒ Object
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 |
#summarize ⇒ Object
77 78 79 |
# File 'lib/chronicle/etl/job_logger.rb', line 77 def summarize @job_log.inspect end |