1
0
Fork 0

Timestamp in UTC, abstract storage

This commit is contained in:
Gregory Eremin 2013-06-07 12:32:50 +04:00
parent 861c12aa32
commit c6f0f2480d
14 changed files with 84 additions and 32 deletions

View File

@ -29,10 +29,14 @@ require 'burden'
Burden.configure do |c|
c.storage = :active_record # or :mongoid, :mongo_mapper
c.ignored_tasks = [/^db:/, /environment/]
c.on_failure = ->(task_name, execution_time){
c.on_failure = ->(task_name, execution_time, timestamp){
Mail.new(to: me,
subject: "Task #{task_name} failed!",
body: "Current time: #{Time.now}\nExecution time: #{execution_time}s"
body: <<-MSG
Started at: #{timestamp}
Execution time: #{execution_time}s
Status: FAILED
MSG
).send
}
end

View File

@ -1,7 +1,7 @@
module BurdenWeb
class RunsController < ApplicationController
def index
@runs = Burden::Storage.run.where(name: params[:task_id]).order('created_at desc').limit(100)
@runs = Burden::Storage.run.history(params[:task_id])
end
end
end

View File

@ -13,7 +13,7 @@
<% @runs.each do |run| %>
<tr>
<td><span class="<%= task_label(run) %> radius label"><%= run.name %></span></td>
<td><%= l run.created_at, format: :short %> (<%= time_ago_in_words(run.created_at) %> ago)</td>
<td><%= l run.timestamp, format: :short %> (<%= time_ago_in_words(run.timestamp) %> ago)</td>
<td><%= run.execution_time.round(6) %>s</td>
</tr>
<% end %>

View File

@ -12,8 +12,8 @@
<% @tasks.each do |run| %>
<tr>
<td><span class="<%= task_label(run) %> radius label"><%= run.name %></span></td>
<% if run.created_at %>
<td><%= time_ago_in_words(run.created_at) %> ago</td>
<% if run.timestamp %>
<td><%= time_ago_in_words(run.timestamp) %> ago</td>
<td><%= run.execution_time.round(6) %>s</td>
<td><%= link_to 'Run', run_task_path(run.name, back: tasks_path), class: 'small radius button' %></td>
<td><%= link_to 'History', task_runs_path(run.name), class: 'secondary small radius button' %></td>

View File

@ -4,7 +4,7 @@ class CreateBurdenRuns < ActiveRecord::Migration
t.string :name
t.boolean :success
t.float :execution_time
t.timestamps
t.datetime :timestamp
end
end

View File

@ -11,14 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130607070108) do
ActiveRecord::Schema.define(:version => 20130607082646) do
create_table "burden_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
t.datetime "timestamp"
end
end

View File

@ -10,12 +10,12 @@ module Burden
@ignored_tasks = [/environment/]
end
def trigger_success_callback(name, execution_time)
on_success.send(name, execution_time) unless on_success.nil?
def trigger_success_callback(name, execution_time, timestamp)
on_success.send(name, execution_time, timestamp) unless on_success.nil?
end
def trigger_failure_callback(name, execution_time)
on_failure.send(name, execution_time) unless on_failure.nil?
def trigger_failure_callback(name, execution_time, timestamp)
on_failure.send(name, execution_time, timestamp) unless on_failure.nil?
end
module Helper

View File

@ -1,25 +1,41 @@
module Burden
class Statistics
attr_reader :name, :success, :execution_time
attr_reader :name, :success, :execution_time, :timestamp
def initialize(params)
@name = params[:name]
@success = params[:success]
@execution_time = params[:execution_time]
@timestamp = params[:timestamp]
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)
Storage.run.create(name: name, success: success, execution_time: execution_time)
begin
Storage.run.create(name: name, success: success, execution_time: execution_time, timestamp: timestamp)
rescue
log_to_stdout(:failed)
end
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)}"
log_to_stdout(:no_rails)
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)}"
end
end
end

View File

@ -0,0 +1,28 @@
module Burden
module Storage
module Abstract
class Run
attr_accessor :name, :success, :execution_time, :timestamp
def initialize(attributes = {})
@name = attributes[:name]
@success = attributes[:success]
@execution_time = attributes[:execution_time]
@timestamp = attributes[:timestamp]
end
def save
raise NotImplementedError.new('Method #save must be overwritten')
end
def summary
raise NotImplementedError.new('Method #summary must be overwritten')
end
def history(task_name)
raise NotImplementedError.new('Method #history must be overwritten')
end
end
end
end
end

View File

@ -3,9 +3,10 @@ module Burden
module ActiveRecord
class Run < ::ActiveRecord::Base
self.table_name = 'burden_runs'
attr_accessible :name, :success, :execution_time
attr_accessible :name, :success, :execution_time, :timestamp
scope :summary, ->{ order('created_at desc').group(:name) }
scope :summary, ->{ order('timestamp desc').group(:name) }
scope :history, ->(task_name){ where(name: task_name).order('timestamp desc').limit(100) }
end
end
end

View File

@ -8,8 +8,10 @@ module Burden
key :name, String
key :success, Boolean
key :execution_time, Float
key :timestamp, Time
scope :summary, ->{ sort(:created_at.desc).group_by(&:name) }
scope :summary, ->{ sort(:timestamp.desc).group_by(&:name) }
scope :history, ->(task_name){ where(name: task_name).sort(:timestamp.desc).group_by(&:name) }
end
end
end

View File

@ -8,8 +8,10 @@ module Burden
field :name, type: String
field :success, type: Boolean
field :execution_time, type: Float
field :timestamp, type: Time
scope :summary, ->{ sort(created_at: -1).group_by(&:name) }
scope :summary, ->{ sort(timestamp: -1).group_by(&:name) }
scope :history, ->(task_name){ where(name: task_name).sort(timestamp: -1).group_by(&:name) }
end
end
end

View File

@ -1,12 +1,12 @@
module Burden
class Wrapper
attr_reader :name, :description, :block, :success, :exception, :execution_time
attr_reader :name, :block, :success, :exception, :execution_time, :timestamp
def initialize(name, description, block)
def initialize(name, block)
@name = name
@description = description
@block = block
@success = true
@timestamp = Time.now.utc
end
def execute
@ -20,11 +20,11 @@ module Burden
save_statistics
unless success
Burden.config.trigger_failure_callback(name, execution_time)
Burden.config.trigger_failure_callback(name, execution_time, timestamp)
raise(exception)
end
Burden.config.trigger_success_callback(name, execution_time)
Burden.config.trigger_success_callback(name, execution_time, timestamp)
result
end
@ -52,7 +52,7 @@ module Burden
end
def save_statistics
Statistics.new(name: name, success: success, execution_time: execution_time).save
Statistics.new(name: name, success: success, execution_time: execution_time, timestamp: timestamp).save
end
end
end

View File

@ -4,7 +4,7 @@ class CreateBurdenRuns < ActiveRecord::Migration
t.string :name
t.boolean :success
t.float :execution_time
t.timestamps
t.datetime :timestamp
end
end