diff --git a/burden_web/lib/burden_web/version.rb b/burden_web/lib/burden_web/version.rb index dc37116..4dbc664 100644 --- a/burden_web/lib/burden_web/version.rb +++ b/burden_web/lib/burden_web/version.rb @@ -1,3 +1,3 @@ module BurdenWeb - VERSION = '0.1.2' + VERSION = '0.1.3' end diff --git a/lib/burden/config.rb b/lib/burden/config.rb index a67f182..12cdb27 100644 --- a/lib/burden/config.rb +++ b/lib/burden/config.rb @@ -1,12 +1,15 @@ module Burden class Config - attr_accessor :storage, # Storage backend (ActiveRecord, Mongoid, MongoMapper) - :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) + 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 diff --git a/lib/burden/statistics.rb b/lib/burden/statistics.rb index 25d7961..0aa52ae 100644 --- a/lib/burden/statistics.rb +++ b/lib/burden/statistics.rb @@ -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 diff --git a/lib/burden/storage.rb b/lib/burden/storage.rb index 2ed9203..55e0d7b 100644 --- a/lib/burden/storage.rb +++ b/lib/burden/storage.rb @@ -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 diff --git a/lib/burden/storage/abstract/run.rb b/lib/burden/storage_backends/abstract/run.rb similarity index 96% rename from lib/burden/storage/abstract/run.rb rename to lib/burden/storage_backends/abstract/run.rb index 03cc7fc..6fe9542 100644 --- a/lib/burden/storage/abstract/run.rb +++ b/lib/burden/storage_backends/abstract/run.rb @@ -1,5 +1,5 @@ module Burden - module Storage + module StorageBackends module Abstract class Run attr_accessor :name, :success, :execution_time, :timestamp diff --git a/lib/burden/storage_backends/abstract_backend.rb b/lib/burden/storage_backends/abstract_backend.rb new file mode 100644 index 0000000..c5f63bf --- /dev/null +++ b/lib/burden/storage_backends/abstract_backend.rb @@ -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 diff --git a/lib/burden/storage/active_record/run.rb b/lib/burden/storage_backends/active_record/run.rb similarity index 93% rename from lib/burden/storage/active_record/run.rb rename to lib/burden/storage_backends/active_record/run.rb index 4ae756c..9aca3dc 100644 --- a/lib/burden/storage/active_record/run.rb +++ b/lib/burden/storage_backends/active_record/run.rb @@ -1,5 +1,5 @@ module Burden - module Storage + module StorageBackends module ActiveRecord class Run < ::ActiveRecord::Base self.table_name = 'burden_runs' diff --git a/lib/burden/storage_backends/active_record_backend.rb b/lib/burden/storage_backends/active_record_backend.rb new file mode 100644 index 0000000..0c08b07 --- /dev/null +++ b/lib/burden/storage_backends/active_record_backend.rb @@ -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 diff --git a/lib/burden/storage/mongo_mapper/run.rb b/lib/burden/storage_backends/mongo_mapper/run.rb similarity index 94% rename from lib/burden/storage/mongo_mapper/run.rb rename to lib/burden/storage_backends/mongo_mapper/run.rb index 688d635..04b70ec 100644 --- a/lib/burden/storage/mongo_mapper/run.rb +++ b/lib/burden/storage_backends/mongo_mapper/run.rb @@ -1,5 +1,5 @@ module Burden - module Storage + module StorageBackends module MongoMapper class Run include ::MongoMapper::Document diff --git a/lib/burden/storage/mongoid/run.rb b/lib/burden/storage_backends/mongoid/run.rb similarity index 95% rename from lib/burden/storage/mongoid/run.rb rename to lib/burden/storage_backends/mongoid/run.rb index f9bb290..45a69f7 100644 --- a/lib/burden/storage/mongoid/run.rb +++ b/lib/burden/storage_backends/mongoid/run.rb @@ -1,5 +1,5 @@ module Burden - module Storage + module StorageBackends module Mongoid class Run include ::Mongoid::Document diff --git a/lib/burden/version.rb b/lib/burden/version.rb index f34a1fc..f3214f8 100644 --- a/lib/burden/version.rb +++ b/lib/burden/version.rb @@ -1,3 +1,3 @@ module Burden - VERSION = '0.1.2' + VERSION = '0.1.3' end diff --git a/lib/burden/wrapper.rb b/lib/burden/wrapper.rb index 471e687..34dbcbf 100644 --- a/lib/burden/wrapper.rb +++ b/lib/burden/wrapper.rb @@ -17,7 +17,11 @@ module Burden block.call end end - save_statistics + + begin + save_statistics + rescue => e + end unless success Burden.config.trigger_failure_callback(name, execution_time, timestamp)