Dirty logging to DB
This commit is contained in:
@@ -7,9 +7,5 @@ module Rake
|
||||
original_execute(args)
|
||||
end
|
||||
end
|
||||
|
||||
def stats
|
||||
RakeControl::Statistics.new(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+2
-8
@@ -4,18 +4,12 @@ require File.expand_path('../rake/task', __FILE__)
|
||||
|
||||
require 'rake_control/config'
|
||||
require 'rake_control/statistics'
|
||||
require 'rake_control/storage'
|
||||
require 'rake_control/version'
|
||||
require 'rake_control/wrapper'
|
||||
|
||||
module RakeControl
|
||||
def config
|
||||
@config ||= Config.new
|
||||
end
|
||||
|
||||
def configure
|
||||
yield config if block_given?
|
||||
config.apply
|
||||
end
|
||||
include Config::Helper
|
||||
|
||||
def wrap(name, description, &block)
|
||||
Wrapper.new(name, description, block).execute
|
||||
|
||||
@@ -1,27 +1,20 @@
|
||||
module RakeControl
|
||||
class Config
|
||||
attr_accessor :storage
|
||||
attr_accessor :storage, # Storage backend (ActiveRecord, Mongoid, MongoMapper)
|
||||
:ignored_tasks # Do not log this tasks (eg. environment task)
|
||||
|
||||
def initialize
|
||||
@storage = :active_record
|
||||
@ignored_tasks = [:environment]
|
||||
end
|
||||
|
||||
def apply
|
||||
setup_storage_model
|
||||
end
|
||||
module Helper
|
||||
def config
|
||||
@config ||= Config.new
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def setup_storage_model
|
||||
case storage
|
||||
when :active_record, :activerecord
|
||||
require 'rake_control/storage/active_record/rake_control_run'
|
||||
when :mongoid
|
||||
require 'rake_control/storage/mongoid/rake_control_run'
|
||||
when :mongo_mapper, :mongomapper
|
||||
require 'rake_control/storage/mongo_mapper/rake_control_run'
|
||||
else
|
||||
raise Exception.new("Unknown storage: #{storage}")
|
||||
def configure
|
||||
yield config if block_given?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,23 +1,25 @@
|
||||
module RakeControl
|
||||
class Statistics
|
||||
attr_reader :name
|
||||
attr_reader :name, :success, :execution_time
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
def initialize(params)
|
||||
@name = params[:name]
|
||||
@success = params[:success]
|
||||
@execution_time = params[:execution_time]
|
||||
end
|
||||
|
||||
def last
|
||||
relation.last
|
||||
end
|
||||
|
||||
def log
|
||||
relation.all
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def relation
|
||||
Run.where(name: name)
|
||||
def save
|
||||
if defined?(Rails) && Rails.respond_to?(:application) && Rails.application.present?
|
||||
conf = Rails.configuration.database_configuration[Rails.env]
|
||||
ActiveRecord::Base.establish_connection(conf)
|
||||
Storage.run.create(name: name, success: success, execution_time: execution_time)
|
||||
else
|
||||
puts
|
||||
puts "------------------------------------------------------------"
|
||||
puts "Rails environment is not loaded. Sending output to STDOUT"
|
||||
puts "Task #{name} #{success ? 'finished successfully' : 'failed'}"
|
||||
puts "Execution time: #{execution_time.round(4)}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
module RakeControl
|
||||
module Storage
|
||||
def run
|
||||
case RakeControl.config.storage
|
||||
when :active_record, :activerecord
|
||||
require 'rake_control/storage/active_record/run'
|
||||
RakeControl::Storage::ActiveRecord::Run
|
||||
when :mongoid
|
||||
require 'rake_control/storage/mongoid/run'
|
||||
RakeControl::Storage::Mongoid::Run
|
||||
when :mongo_mapper, :mongomapper
|
||||
require 'rake_control/storage/mongo_mapper/run'
|
||||
RakeControl::Storage::MongoMapper::Run
|
||||
else
|
||||
raise Exception.new("Unknown storage: #{storage}")
|
||||
end
|
||||
end
|
||||
|
||||
extend self
|
||||
end
|
||||
end
|
||||
@@ -1,5 +0,0 @@
|
||||
module RakeControl
|
||||
class Run < ActiveRecord::Base
|
||||
attr_accessible :name, :success, :execution_time
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
module RakeControl
|
||||
module Storage
|
||||
module ActiveRecord
|
||||
class Run < ::ActiveRecord::Base
|
||||
self.table_name = 'rake_control_runs'
|
||||
attr_accessible :name, :success, :execution_time
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,9 +0,0 @@
|
||||
module RakeControl
|
||||
class Run
|
||||
include MongoMapper::Document
|
||||
|
||||
key :name, String
|
||||
key :success, Boolean
|
||||
key :execution_time, Float
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
module RakeControl
|
||||
module Storage
|
||||
module MongoMapper
|
||||
class Run
|
||||
include ::MongoMapper::Document
|
||||
|
||||
key :name, String
|
||||
key :success, Boolean
|
||||
key :execution_time, Float
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,9 +0,0 @@
|
||||
module RakeControl
|
||||
class Run
|
||||
include Mongoid::Document
|
||||
|
||||
field :name, type: String
|
||||
field :success, type: Boolean
|
||||
field :execution_time, type: Float
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
module RakeControl
|
||||
module Storage
|
||||
module Mongoid
|
||||
class Run
|
||||
include ::Mongoid::Document
|
||||
|
||||
field :name, type: String
|
||||
field :success, type: Boolean
|
||||
field :execution_time, type: Float
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -10,6 +10,8 @@ module RakeControl
|
||||
end
|
||||
|
||||
def execute
|
||||
return block.call if ignored?
|
||||
|
||||
result = measure_time do
|
||||
intercept_exceptions do
|
||||
block.call
|
||||
@@ -39,10 +41,12 @@ module RakeControl
|
||||
end
|
||||
end
|
||||
|
||||
def ignored?
|
||||
RakeControl.config.ignored_tasks.include?(name.to_sym)
|
||||
end
|
||||
|
||||
def save_statistics
|
||||
Run.create(name: name, success: success, execution_time: execution_time) if defined?(Run)
|
||||
puts "Task #{name} #{success ? 'finished successfully' : 'failed'}"
|
||||
puts "Execution time: #{execution_time.round(4)}"
|
||||
Statistics.new(name: name, success: success, execution_time: execution_time).save
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user