Organizing storages
This commit is contained in:
parent
bba8bbcd07
commit
0dbdb232df
|
@ -1,3 +1,3 @@
|
|||
module BurdenWeb
|
||||
VERSION = '0.1.2'
|
||||
VERSION = '0.1.3'
|
||||
end
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
module Burden
|
||||
class Config
|
||||
attr_accessor :storage, # Storage backend (ActiveRecord, Mongoid, MongoMapper)
|
||||
:storage_config, # Config needed to initialize storage
|
||||
:log_file, # Log file
|
||||
:ignored_tasks, # Do not log this tasks (eg. environment task)
|
||||
:on_success, # Success callback (expected to be a Proc)
|
||||
:on_failure # Failure callback (expected to be a Proc)
|
||||
|
||||
def initialize
|
||||
@storage = :active_record
|
||||
@log_file = 'tmp/rake.log'
|
||||
@ignored_tasks = [/environment/]
|
||||
end
|
||||
|
||||
|
|
|
@ -10,32 +10,27 @@ module Burden
|
|||
end
|
||||
|
||||
def save
|
||||
if defined?(Rails) && Rails.respond_to?(:application) && Rails.application.present?
|
||||
# FIXME: Dirty stuff
|
||||
conf = Rails.configuration.database_configuration[Rails.env]
|
||||
ActiveRecord::Base.establish_connection(conf)
|
||||
# FIXME
|
||||
# if Burden.storage.ready?
|
||||
# Burden.storage.runs.create(name: name, success: success, execution_time: execution_time, timestamp: timestamp)
|
||||
# end
|
||||
|
||||
begin
|
||||
Burden.runs.create(name: name, success: success, execution_time: execution_time, timestamp: timestamp)
|
||||
rescue
|
||||
log_to_stdout(:failed)
|
||||
end
|
||||
else
|
||||
log_to_stdout(:no_rails)
|
||||
File.open(Burden.config.log_file, 'a') do |f|
|
||||
f.write(log_message)
|
||||
f.close
|
||||
end
|
||||
end
|
||||
|
||||
def log_to_stdout(reason = :no_rails)
|
||||
puts
|
||||
puts "------------------------------------------------------------"
|
||||
case reason
|
||||
when :no_rails
|
||||
puts "Rails environment is not loaded. Sending output to STDOUT"
|
||||
when :failed
|
||||
puts "Failed to persist this run. Sending output to STDOUT"
|
||||
end
|
||||
puts "Task #{name} #{success ? 'finished successfully' : 'failed'}"
|
||||
puts "Execution time: #{execution_time.round(4)}"
|
||||
private
|
||||
|
||||
def log_message
|
||||
<<-TEXT
|
||||
Timestamp: #{timestamp}
|
||||
Task: #{name}
|
||||
Execution #{success ? 'finished successfully' : 'failed'}
|
||||
Execution time: #{execution_time.round(4)}
|
||||
|
||||
TEXT
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
module Burden
|
||||
module Storage
|
||||
module Helper
|
||||
def runs
|
||||
@storage_class ||= begin
|
||||
def storage
|
||||
@storage ||= begin
|
||||
case Burden.config.storage
|
||||
when :active_record, :activerecord
|
||||
require 'burden/storage/active_record/run'
|
||||
Burden::Storage::ActiveRecord::Run
|
||||
require 'burden/storage_backends/active_record_backend'
|
||||
Burden::StorageBackends::ActiveRecordBackend.new(Burden.config.storage_config)
|
||||
when :mongoid
|
||||
require 'burden/storage/mongoid/run'
|
||||
Burden::Storage::Mongoid::Run
|
||||
require 'burden/storage_backends/mongoid_backend'
|
||||
Burden::StorageBackends::MongoidBackend.new(Burden.config.storage_config)
|
||||
when :mongo_mapper, :mongomapper
|
||||
require 'burden/storage/mongo_mapper/run'
|
||||
Burden::Storage::MongoMapper::Run
|
||||
require 'burden/storage_backends/mongo_mapper_backend'
|
||||
Burden::StorageBackends::MongoMapperBackend.new(Burden.config.storage_config)
|
||||
else
|
||||
raise Exception.new("Unknown storage: #{storage}")
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module Burden
|
||||
module Storage
|
||||
module StorageBackends
|
||||
module Abstract
|
||||
class Run
|
||||
attr_accessor :name, :success, :execution_time, :timestamp
|
|
@ -0,0 +1,18 @@
|
|||
require 'burden/storage_backends/abstract/run'
|
||||
|
||||
module Burden
|
||||
module StorageBackends
|
||||
class AbstractBackend
|
||||
def initialize(config)
|
||||
end
|
||||
|
||||
def ready?
|
||||
raise NotImplementedError.new('Method #ready? must be overwritten')
|
||||
end
|
||||
|
||||
def runs
|
||||
raise NotImplementedError.new('Method #runs must be overwritten')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
module Burden
|
||||
module Storage
|
||||
module StorageBackends
|
||||
module ActiveRecord
|
||||
class Run < ::ActiveRecord::Base
|
||||
self.table_name = 'burden_runs'
|
|
@ -0,0 +1,27 @@
|
|||
require 'active_record'
|
||||
require 'burden/storage_backends/active_record/run'
|
||||
|
||||
module Burden
|
||||
module StorageBackends
|
||||
class ActiveRecordBackend
|
||||
attr_reader :connection_success
|
||||
|
||||
def initialize(config)
|
||||
@connection_success = true
|
||||
begin
|
||||
ActiveRecord::Base.establish_connection(config)
|
||||
rescue => e
|
||||
@connection_success = false
|
||||
end
|
||||
end
|
||||
|
||||
def ready?
|
||||
@connection_success
|
||||
end
|
||||
|
||||
def runs
|
||||
Burden::StorageBackends::ActiveRecord::Run
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
module Burden
|
||||
module Storage
|
||||
module StorageBackends
|
||||
module MongoMapper
|
||||
class Run
|
||||
include ::MongoMapper::Document
|
|
@ -1,5 +1,5 @@
|
|||
module Burden
|
||||
module Storage
|
||||
module StorageBackends
|
||||
module Mongoid
|
||||
class Run
|
||||
include ::Mongoid::Document
|
|
@ -1,3 +1,3 @@
|
|||
module Burden
|
||||
VERSION = '0.1.2'
|
||||
VERSION = '0.1.3'
|
||||
end
|
||||
|
|
|
@ -17,7 +17,11 @@ module Burden
|
|||
block.call
|
||||
end
|
||||
end
|
||||
|
||||
begin
|
||||
save_statistics
|
||||
rescue => e
|
||||
end
|
||||
|
||||
unless success
|
||||
Burden.config.trigger_failure_callback(name, execution_time, timestamp)
|
||||
|
|
Loading…
Reference in New Issue