1
0
Fork 0

Organizing storages

This commit is contained in:
Gregory Eremin 2013-06-12 12:45:14 +04:00
parent bba8bbcd07
commit 0dbdb232df
12 changed files with 88 additions and 41 deletions

View File

@ -1,3 +1,3 @@
module BurdenWeb module BurdenWeb
VERSION = '0.1.2' VERSION = '0.1.3'
end end

View File

@ -1,12 +1,15 @@
module Burden module Burden
class Config class Config
attr_accessor :storage, # Storage backend (ActiveRecord, Mongoid, MongoMapper) attr_accessor :storage, # Storage backend (ActiveRecord, Mongoid, MongoMapper)
:ignored_tasks, # Do not log this tasks (eg. environment task) :storage_config, # Config needed to initialize storage
:on_success, # Success callback (expected to be a Proc) :log_file, # Log file
:on_failure # Failure callback (expected to be a Proc) :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 def initialize
@storage = :active_record @storage = :active_record
@log_file = 'tmp/rake.log'
@ignored_tasks = [/environment/] @ignored_tasks = [/environment/]
end end

View File

@ -10,32 +10,27 @@ module Burden
end end
def save def save
if defined?(Rails) && Rails.respond_to?(:application) && Rails.application.present? # FIXME
# FIXME: Dirty stuff # if Burden.storage.ready?
conf = Rails.configuration.database_configuration[Rails.env] # Burden.storage.runs.create(name: name, success: success, execution_time: execution_time, timestamp: timestamp)
ActiveRecord::Base.establish_connection(conf) # end
begin File.open(Burden.config.log_file, 'a') do |f|
Burden.runs.create(name: name, success: success, execution_time: execution_time, timestamp: timestamp) f.write(log_message)
rescue f.close
log_to_stdout(:failed)
end
else
log_to_stdout(:no_rails)
end end
end end
def log_to_stdout(reason = :no_rails) private
puts
puts "------------------------------------------------------------" def log_message
case reason <<-TEXT
when :no_rails Timestamp: #{timestamp}
puts "Rails environment is not loaded. Sending output to STDOUT" Task: #{name}
when :failed Execution #{success ? 'finished successfully' : 'failed'}
puts "Failed to persist this run. Sending output to STDOUT" Execution time: #{execution_time.round(4)}
end
puts "Task #{name} #{success ? 'finished successfully' : 'failed'}" TEXT
puts "Execution time: #{execution_time.round(4)}"
end end
end end
end end

View File

@ -1,18 +1,18 @@
module Burden module Burden
module Storage module Storage
module Helper module Helper
def runs def storage
@storage_class ||= begin @storage ||= begin
case Burden.config.storage case Burden.config.storage
when :active_record, :activerecord when :active_record, :activerecord
require 'burden/storage/active_record/run' require 'burden/storage_backends/active_record_backend'
Burden::Storage::ActiveRecord::Run Burden::StorageBackends::ActiveRecordBackend.new(Burden.config.storage_config)
when :mongoid when :mongoid
require 'burden/storage/mongoid/run' require 'burden/storage_backends/mongoid_backend'
Burden::Storage::Mongoid::Run Burden::StorageBackends::MongoidBackend.new(Burden.config.storage_config)
when :mongo_mapper, :mongomapper when :mongo_mapper, :mongomapper
require 'burden/storage/mongo_mapper/run' require 'burden/storage_backends/mongo_mapper_backend'
Burden::Storage::MongoMapper::Run Burden::StorageBackends::MongoMapperBackend.new(Burden.config.storage_config)
else else
raise Exception.new("Unknown storage: #{storage}") raise Exception.new("Unknown storage: #{storage}")
end end

View File

@ -1,5 +1,5 @@
module Burden module Burden
module Storage module StorageBackends
module Abstract module Abstract
class Run class Run
attr_accessor :name, :success, :execution_time, :timestamp attr_accessor :name, :success, :execution_time, :timestamp

View File

@ -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

View File

@ -1,5 +1,5 @@
module Burden module Burden
module Storage module StorageBackends
module ActiveRecord module ActiveRecord
class Run < ::ActiveRecord::Base class Run < ::ActiveRecord::Base
self.table_name = 'burden_runs' self.table_name = 'burden_runs'

View File

@ -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

View File

@ -1,5 +1,5 @@
module Burden module Burden
module Storage module StorageBackends
module MongoMapper module MongoMapper
class Run class Run
include ::MongoMapper::Document include ::MongoMapper::Document

View File

@ -1,5 +1,5 @@
module Burden module Burden
module Storage module StorageBackends
module Mongoid module Mongoid
class Run class Run
include ::Mongoid::Document include ::Mongoid::Document

View File

@ -1,3 +1,3 @@
module Burden module Burden
VERSION = '0.1.2' VERSION = '0.1.3'
end end

View File

@ -17,7 +17,11 @@ module Burden
block.call block.call
end end
end end
save_statistics
begin
save_statistics
rescue => e
end
unless success unless success
Burden.config.trigger_failure_callback(name, execution_time, timestamp) Burden.config.trigger_failure_callback(name, execution_time, timestamp)