From c6f0f2480db183cea0898df6f502224260f80e3f Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Fri, 7 Jun 2013 12:32:50 +0400 Subject: [PATCH] Timestamp in UTC, abstract storage --- README.md | 8 +++-- .../controllers/burden_web/runs_controller.rb | 2 +- .../app/views/burden_web/runs/index.html.erb | 2 +- .../app/views/burden_web/tasks/index.html.erb | 4 +-- ...b => 20130607082646_create_burden_runs.rb} | 2 +- burden_web/spec/dummy/db/schema.rb | 5 ++-- lib/burden/config.rb | 8 ++--- lib/burden/statistics.rb | 30 ++++++++++++++----- lib/burden/storage/abstract/run.rb | 28 +++++++++++++++++ lib/burden/storage/active_record/run.rb | 5 ++-- lib/burden/storage/mongo_mapper/run.rb | 4 ++- lib/burden/storage/mongoid/run.rb | 4 ++- lib/burden/wrapper.rb | 12 ++++---- .../templates/burden_runs_migration.rb | 2 +- 14 files changed, 84 insertions(+), 32 deletions(-) rename burden_web/spec/dummy/db/migrate/{20130607070108_create_burden_runs.rb => 20130607082646_create_burden_runs.rb} (89%) create mode 100644 lib/burden/storage/abstract/run.rb diff --git a/README.md b/README.md index 2ba5a9d..1d9f253 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/burden_web/app/controllers/burden_web/runs_controller.rb b/burden_web/app/controllers/burden_web/runs_controller.rb index 997491d..7bf0d1c 100644 --- a/burden_web/app/controllers/burden_web/runs_controller.rb +++ b/burden_web/app/controllers/burden_web/runs_controller.rb @@ -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 diff --git a/burden_web/app/views/burden_web/runs/index.html.erb b/burden_web/app/views/burden_web/runs/index.html.erb index 3b500b0..d1e6f8f 100644 --- a/burden_web/app/views/burden_web/runs/index.html.erb +++ b/burden_web/app/views/burden_web/runs/index.html.erb @@ -13,7 +13,7 @@ <% @runs.each do |run| %> <%= run.name %> - <%= l run.created_at, format: :short %> (<%= time_ago_in_words(run.created_at) %> ago) + <%= l run.timestamp, format: :short %> (<%= time_ago_in_words(run.timestamp) %> ago) <%= run.execution_time.round(6) %>s <% end %> diff --git a/burden_web/app/views/burden_web/tasks/index.html.erb b/burden_web/app/views/burden_web/tasks/index.html.erb index 09666c7..88b6e66 100644 --- a/burden_web/app/views/burden_web/tasks/index.html.erb +++ b/burden_web/app/views/burden_web/tasks/index.html.erb @@ -12,8 +12,8 @@ <% @tasks.each do |run| %> <%= run.name %> - <% if run.created_at %> - <%= time_ago_in_words(run.created_at) %> ago + <% if run.timestamp %> + <%= time_ago_in_words(run.timestamp) %> ago <%= run.execution_time.round(6) %>s <%= link_to 'Run', run_task_path(run.name, back: tasks_path), class: 'small radius button' %> <%= link_to 'History', task_runs_path(run.name), class: 'secondary small radius button' %> diff --git a/burden_web/spec/dummy/db/migrate/20130607070108_create_burden_runs.rb b/burden_web/spec/dummy/db/migrate/20130607082646_create_burden_runs.rb similarity index 89% rename from burden_web/spec/dummy/db/migrate/20130607070108_create_burden_runs.rb rename to burden_web/spec/dummy/db/migrate/20130607082646_create_burden_runs.rb index 9ed964d..5aef166 100644 --- a/burden_web/spec/dummy/db/migrate/20130607070108_create_burden_runs.rb +++ b/burden_web/spec/dummy/db/migrate/20130607082646_create_burden_runs.rb @@ -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 diff --git a/burden_web/spec/dummy/db/schema.rb b/burden_web/spec/dummy/db/schema.rb index 7d0ac3b..96797b2 100644 --- a/burden_web/spec/dummy/db/schema.rb +++ b/burden_web/spec/dummy/db/schema.rb @@ -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 diff --git a/lib/burden/config.rb b/lib/burden/config.rb index da586a8..a67f182 100644 --- a/lib/burden/config.rb +++ b/lib/burden/config.rb @@ -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 diff --git a/lib/burden/statistics.rb b/lib/burden/statistics.rb index a169dc5..82408f3 100644 --- a/lib/burden/statistics.rb +++ b/lib/burden/statistics.rb @@ -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 diff --git a/lib/burden/storage/abstract/run.rb b/lib/burden/storage/abstract/run.rb new file mode 100644 index 0000000..03cc7fc --- /dev/null +++ b/lib/burden/storage/abstract/run.rb @@ -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 diff --git a/lib/burden/storage/active_record/run.rb b/lib/burden/storage/active_record/run.rb index a6d29b9..4ae756c 100644 --- a/lib/burden/storage/active_record/run.rb +++ b/lib/burden/storage/active_record/run.rb @@ -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 diff --git a/lib/burden/storage/mongo_mapper/run.rb b/lib/burden/storage/mongo_mapper/run.rb index 5c491a6..688d635 100644 --- a/lib/burden/storage/mongo_mapper/run.rb +++ b/lib/burden/storage/mongo_mapper/run.rb @@ -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 diff --git a/lib/burden/storage/mongoid/run.rb b/lib/burden/storage/mongoid/run.rb index 517d035..f9bb290 100644 --- a/lib/burden/storage/mongoid/run.rb +++ b/lib/burden/storage/mongoid/run.rb @@ -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 diff --git a/lib/burden/wrapper.rb b/lib/burden/wrapper.rb index 4a1cc1b..60c0281 100644 --- a/lib/burden/wrapper.rb +++ b/lib/burden/wrapper.rb @@ -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 diff --git a/lib/rails/generators/burden/install/templates/burden_runs_migration.rb b/lib/rails/generators/burden/install/templates/burden_runs_migration.rb index 9ed964d..5aef166 100644 --- a/lib/rails/generators/burden/install/templates/burden_runs_migration.rb +++ b/lib/rails/generators/burden/install/templates/burden_runs_migration.rb @@ -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