1
0
Fork 0

Dirty logging to DB

This commit is contained in:
Gregory Eremin 2013-06-06 18:15:38 +04:00
parent f7964df0b6
commit 802fe508ea
18 changed files with 140 additions and 74 deletions

View File

@ -7,9 +7,5 @@ module Rake
original_execute(args)
end
end
def stats
RakeControl::Statistics.new(name)
end
end
end

View File

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

View File

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

View File

@ -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
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
def log
relation.all
end
private
def relation
Run.where(name: name)
end
end
end

View File

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

View File

@ -1,5 +0,0 @@
module RakeControl
class Run < ActiveRecord::Base
attr_accessible :name, :success, :execution_time
end
end

View File

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

View File

@ -1,9 +0,0 @@
module RakeControl
class Run
include MongoMapper::Document
key :name, String
key :success, Boolean
key :execution_time, Float
end
end

View File

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

View File

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

View File

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

View File

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

View File

@ -5,3 +5,4 @@ spec/dummy/db/*.sqlite3
spec/dummy/log/*.log
spec/dummy/tmp/
spec/dummy/.sass-cache
vendor

View File

@ -3,4 +3,4 @@ source 'http://rubygems.org'
gemspec
gem 'jquery-rails'
gem 'rake_control', :path => '..'
gem 'rake_control', path: '..'

View File

@ -15,9 +15,9 @@ Gem::Specification.new do |s|
s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
s.add_dependency "rails", "~> 3.2.13"
# s.add_dependency "jquery-rails"
s.add_dependency 'rails', '>= 3.2'
s.add_development_dependency 'rake_control'
s.add_development_dependency "sqlite3"
s.add_development_dependency "rake_control"
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'awesome_print'
end

View File

@ -0,0 +1,4 @@
RakeControl.configure do |c|
c.storage = :active_record
c.ignored_tasks = [:environment]
end

View File

@ -0,0 +1,14 @@
class CreateRakeControlRuns < ActiveRecord::Migration
def up
create_table :rake_control_runs do |t|
t.string :name
t.boolean :success
t.float :execution_time
t.timestamps
end
end
def down
drop_table :rake_control_runs
end
end

View File

@ -0,0 +1,24 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130606131532) do
create_table "rake_control_runs", :force => true do |t|
t.string "name"
t.boolean "success"
t.float "execution_time"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end