trackable_tasks 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/CHANGELOG +7 -0
  4. data/Gemfile +22 -0
  5. data/Gemfile.lock +159 -0
  6. data/LICENSE.txt +20 -0
  7. data/README.rdoc +77 -0
  8. data/Rakefile +90 -0
  9. data/VERSION +1 -0
  10. data/app/models/trackable_tasks/task_run.rb +39 -0
  11. data/config/routes.rb +0 -0
  12. data/lib/generators/trackable_tasks/install_generator.rb +43 -0
  13. data/lib/generators/trackable_tasks/templates/migrations/install_migration.rb.erb +16 -0
  14. data/lib/generators/trackable_tasks/templates/trackable_tasks.rake +48 -0
  15. data/lib/trackable_tasks/base.rb +87 -0
  16. data/lib/trackable_tasks/config.rb +0 -0
  17. data/lib/trackable_tasks/engine.rb +7 -0
  18. data/lib/trackable_tasks/railtie.rb +11 -0
  19. data/lib/trackable_tasks.rb +5 -0
  20. data/spec/dummy/Rakefile +7 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  22. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  23. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/spec/dummy/config/application.rb +45 -0
  25. data/spec/dummy/config/boot.rb +10 -0
  26. data/spec/dummy/config/database.yml +22 -0
  27. data/spec/dummy/config/environment.rb +5 -0
  28. data/spec/dummy/config/environments/development.rb +26 -0
  29. data/spec/dummy/config/environments/production.rb +49 -0
  30. data/spec/dummy/config/environments/test.rb +35 -0
  31. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  32. data/spec/dummy/config/initializers/inflections.rb +10 -0
  33. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  34. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  35. data/spec/dummy/config/initializers/session_store.rb +8 -0
  36. data/spec/dummy/config/locales/en.yml +5 -0
  37. data/spec/dummy/config/routes.rb +58 -0
  38. data/spec/dummy/config.ru +4 -0
  39. data/spec/dummy/db/migrate/20110826183240_create_trackable_task_tables.rb +16 -0
  40. data/spec/dummy/db/schema.rb +24 -0
  41. data/spec/dummy/features/step_definitions/trackable_tasks_steps.rb +0 -0
  42. data/spec/dummy/features/support/env.rb +15 -0
  43. data/spec/dummy/lib/tasks/trackable_tasks.rake +48 -0
  44. data/spec/dummy/lib/trackable_tasks/my_task.rb +8 -0
  45. data/spec/dummy/public/404.html +26 -0
  46. data/spec/dummy/public/422.html +26 -0
  47. data/spec/dummy/public/500.html +26 -0
  48. data/spec/dummy/public/favicon.ico +0 -0
  49. data/spec/dummy/public/javascripts/application.js +2 -0
  50. data/spec/dummy/public/javascripts/controls.js +965 -0
  51. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  52. data/spec/dummy/public/javascripts/effects.js +1123 -0
  53. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  54. data/spec/dummy/public/javascripts/rails.js +191 -0
  55. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  56. data/spec/dummy/script/rails +6 -0
  57. data/spec/integration/navigation_spec.rb +9 -0
  58. data/spec/models/task_run_spec.rb +100 -0
  59. data/spec/rake_task_spec.rb +29 -0
  60. data/spec/spec_helper.rb +37 -0
  61. data/spec/support/sample_tasks.rb +26 -0
  62. data/spec/trackable_task_base_spec.rb +168 -0
  63. data/spec/trackable_tasks_spec.rb +7 -0
  64. data/trackable_tasks.gemspec +145 -0
  65. metadata +285 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG ADDED
@@ -0,0 +1,7 @@
1
+ == 0.0.1
2
+
3
+ * Initial release
4
+ * TrackableTasks::Base class that determines how data is logged and stored
5
+ * Includes tests that show how to instantiate the Base class to use a trackable task
6
+ * TaskRun model that stores logging data associated with a trackable task
7
+ * Set of rake tasks to instantiate and run the trackable tasks
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "3.0.7"
4
+ gem "capybara", ">= 0.4.0"
5
+ gem "sqlite3"
6
+
7
+ group :development do
8
+ gem 'ruby-debug19'
9
+ gem "rspec", "~> 2.6.0"
10
+ gem "rspec-rails", "~>2.6.1"
11
+ gem "yard", "~> 0.6.0"
12
+ gem "cucumber", ">= 0"
13
+ gem "cucumber-rails"
14
+ gem "database_cleaner"
15
+ gem "bundler", "~> 1.0.0"
16
+ gem "jeweler", "~> 1.6.3"
17
+ gem "rcov", ">= 0"
18
+ gem "json"
19
+
20
+ # hudson ci
21
+ gem "ci_reporter"
22
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,159 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.7)
6
+ actionpack (= 3.0.7)
7
+ mail (~> 2.2.15)
8
+ actionpack (3.0.7)
9
+ activemodel (= 3.0.7)
10
+ activesupport (= 3.0.7)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.5.0)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.14)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.7)
19
+ activesupport (= 3.0.7)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.5.0)
22
+ activerecord (3.0.7)
23
+ activemodel (= 3.0.7)
24
+ activesupport (= 3.0.7)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.7)
28
+ activemodel (= 3.0.7)
29
+ activesupport (= 3.0.7)
30
+ activesupport (3.0.7)
31
+ archive-tar-minitar (0.5.2)
32
+ arel (2.0.10)
33
+ builder (2.1.2)
34
+ capybara (1.0.1)
35
+ mime-types (>= 1.16)
36
+ nokogiri (>= 1.3.3)
37
+ rack (>= 1.0.0)
38
+ rack-test (>= 0.5.4)
39
+ selenium-webdriver (~> 2.0)
40
+ xpath (~> 0.1.4)
41
+ childprocess (0.2.1)
42
+ ffi (~> 1.0.6)
43
+ ci_reporter (1.6.5)
44
+ builder (>= 2.1.2)
45
+ columnize (0.3.4)
46
+ cucumber (1.0.2)
47
+ builder (>= 2.1.2)
48
+ diff-lcs (>= 1.1.2)
49
+ gherkin (~> 2.4.5)
50
+ json (>= 1.4.6)
51
+ term-ansicolor (>= 1.0.5)
52
+ cucumber-rails (1.0.2)
53
+ capybara (>= 1.0.0)
54
+ cucumber (~> 1.0.0)
55
+ nokogiri (>= 1.4.6)
56
+ database_cleaner (0.6.7)
57
+ diff-lcs (1.1.2)
58
+ erubis (2.6.6)
59
+ abstract (>= 1.0.0)
60
+ ffi (1.0.9)
61
+ gherkin (2.4.16)
62
+ json (>= 1.4.6)
63
+ git (1.2.5)
64
+ i18n (0.5.0)
65
+ jeweler (1.6.4)
66
+ bundler (~> 1.0)
67
+ git (>= 1.2.5)
68
+ rake
69
+ json (1.5.3)
70
+ json_pure (1.5.3)
71
+ linecache19 (0.5.12)
72
+ ruby_core_source (>= 0.1.4)
73
+ mail (2.2.19)
74
+ activesupport (>= 2.3.6)
75
+ i18n (>= 0.4.0)
76
+ mime-types (~> 1.16)
77
+ treetop (~> 1.4.8)
78
+ mime-types (1.16)
79
+ nokogiri (1.5.0)
80
+ polyglot (0.3.2)
81
+ rack (1.2.3)
82
+ rack-mount (0.6.14)
83
+ rack (>= 1.0.0)
84
+ rack-test (0.5.7)
85
+ rack (>= 1.0)
86
+ rails (3.0.7)
87
+ actionmailer (= 3.0.7)
88
+ actionpack (= 3.0.7)
89
+ activerecord (= 3.0.7)
90
+ activeresource (= 3.0.7)
91
+ activesupport (= 3.0.7)
92
+ bundler (~> 1.0)
93
+ railties (= 3.0.7)
94
+ railties (3.0.7)
95
+ actionpack (= 3.0.7)
96
+ activesupport (= 3.0.7)
97
+ rake (>= 0.8.7)
98
+ thor (~> 0.14.4)
99
+ rake (0.9.2)
100
+ rcov (0.9.10)
101
+ rspec (2.6.0)
102
+ rspec-core (~> 2.6.0)
103
+ rspec-expectations (~> 2.6.0)
104
+ rspec-mocks (~> 2.6.0)
105
+ rspec-core (2.6.4)
106
+ rspec-expectations (2.6.0)
107
+ diff-lcs (~> 1.1.2)
108
+ rspec-mocks (2.6.0)
109
+ rspec-rails (2.6.1)
110
+ actionpack (~> 3.0)
111
+ activesupport (~> 3.0)
112
+ railties (~> 3.0)
113
+ rspec (~> 2.6.0)
114
+ ruby-debug-base19 (0.11.25)
115
+ columnize (>= 0.3.1)
116
+ linecache19 (>= 0.5.11)
117
+ ruby_core_source (>= 0.1.4)
118
+ ruby-debug19 (0.11.6)
119
+ columnize (>= 0.3.1)
120
+ linecache19 (>= 0.5.11)
121
+ ruby-debug-base19 (>= 0.11.19)
122
+ ruby_core_source (0.1.5)
123
+ archive-tar-minitar (>= 0.5.2)
124
+ rubyzip (0.9.4)
125
+ selenium-webdriver (2.5.0)
126
+ childprocess (>= 0.2.1)
127
+ ffi (>= 1.0.7)
128
+ json_pure
129
+ rubyzip
130
+ sqlite3 (1.3.4)
131
+ term-ansicolor (1.0.6)
132
+ thor (0.14.6)
133
+ treetop (1.4.10)
134
+ polyglot
135
+ polyglot (>= 0.3.1)
136
+ tzinfo (0.3.29)
137
+ xpath (0.1.4)
138
+ nokogiri (~> 1.3)
139
+ yard (0.6.8)
140
+
141
+ PLATFORMS
142
+ ruby
143
+
144
+ DEPENDENCIES
145
+ bundler (~> 1.0.0)
146
+ capybara (>= 0.4.0)
147
+ ci_reporter
148
+ cucumber
149
+ cucumber-rails
150
+ database_cleaner
151
+ jeweler (~> 1.6.3)
152
+ json
153
+ rails (= 3.0.7)
154
+ rcov
155
+ rspec (~> 2.6.0)
156
+ rspec-rails (~> 2.6.1)
157
+ ruby-debug19
158
+ sqlite3
159
+ yard (~> 0.6.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jeremiah Hemphill
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,77 @@
1
+ = trackable_tasks
2
+
3
+ A wrapper for rake tasks that include logging and stats.
4
+
5
+ = Installation
6
+
7
+ Add the gem to your gem file.
8
+
9
+ gem "trackable_tasks"
10
+
11
+ Run the install generator and migrate.
12
+
13
+ rails generate trackable_tasks:install
14
+ rake db:migrate
15
+
16
+ The generator also adds lib/tasks/trackable_tasks.rake. There should be a new rake task called trackable_task:run.
17
+
18
+ = How to use
19
+
20
+ Create a trackable task. Override the run method and put the code that needs to be tracked inside.
21
+
22
+ class MyTask < AbstractTrackableTask
23
+ def run
24
+ log "hello"
25
+ end
26
+ end
27
+
28
+ This creates a new dynamic rake task. Run the trackable task using either rake task. The RAISL_ENV can also be specified here.
29
+
30
+ rake trackable_task:my_task
31
+ rake trackable_task:run task_num=my_task
32
+
33
+ The results can be found by looking in:
34
+
35
+ TrackableTasks::TaskRun.last
36
+
37
+ The log_text and error_text methods store logging and error information. If an exception is generated by the run method, it's message is stored to the error_text, and success is set to false.
38
+
39
+ = Logging
40
+
41
+ Log levels can be set when running with a rake task
42
+
43
+ rake trackable_task:my_task log_level=notice
44
+ rake trackable_task:run task_num=my_task log_level=error
45
+
46
+ Setting the log level here determines the highest level of logs that will be saved by the task. The three log levels are 'error', 'notice', and 'debug'.
47
+
48
+ - Error: only store error messages; task_run.log_text should not be used
49
+ - Notice: store error messages and notice messages; this is the default level
50
+ - Debug: store all messages
51
+
52
+ Inside the script, the log function is used to store data to the task_run logs.
53
+
54
+ log("this is an error log", :error)
55
+ log("this is a notice log")
56
+ log("this is a debug log", :debug)
57
+
58
+ The error level logs write to task_run.log_text. Notice and debug level logs write to task_run.log_text.
59
+
60
+ == Future Work
61
+
62
+ * Adding a basic controller and views to show the trackable task runs.
63
+
64
+ == Contributing to trackable_tasks
65
+
66
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
67
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
68
+ * Fork the project
69
+ * Start a feature/bugfix branch
70
+ * Commit and push until you are happy with your contribution
71
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
72
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
73
+
74
+ == Copyright
75
+
76
+ Copyright (c) 2011 Jeremiah Hemphill. See LICENSE.txt for
77
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,90 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "trackable_tasks"
18
+ gem.homepage = "http://github.com/jeremiahishere/trackable_tasks"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Adds tracking to rake tasks}
21
+ gem.description = %Q{Adds tracking to rake tasks including error capturing and logging.}
22
+ gem.email = "jeremiah@cloudspace.com"
23
+ gem.authors = ["Jeremiah Hemphill"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ require 'cucumber/rake/task'
40
+ Cucumber::Rake::Task.new(:features)
41
+
42
+ task :default => :spec
43
+
44
+ require 'yard'
45
+ YARD::Rake::YardocTask.new
46
+
47
+ # hudson ci
48
+ require 'ci/reporter/rake/rspec'
49
+ namespace :hudson do
50
+ def report_path
51
+ "spec/reports/"
52
+ end
53
+
54
+ task :report_setup do
55
+ rm_rf report_path
56
+ mkdir_p report_path
57
+ end
58
+
59
+ # hudson cucumber rake task
60
+ Cucumber::Rake::Task.new(:cucumber, "Run cucumber with hudson output") do |t|
61
+ t.cucumber_opts = %{spec/dummy/features --format junit --out #{report_path}}
62
+ end
63
+ end
64
+
65
+ #general cucumber rake tasks
66
+ require 'cucumber/rake/task'
67
+ namespace :cucumber do
68
+ Cucumber::Rake::Task.new(:all, 'run features that should pass') do |t|
69
+ t.cucumber_opts = "spec/dummy/features --format progress"
70
+ end
71
+ Cucumber::Rake::Task.new(:no_js, 'run features that should pass') do |t|
72
+ t.cucumber_opts = "spec/dummy/features --format progress --tags ~@javascript"
73
+ end
74
+
75
+ task :setup_js_with_vnc4server do
76
+ puts "Cucumber test with vnc4server"
77
+ ENV['DISPLAY'] = ":99"
78
+ %x{vncserver :99 2>/dev/null >/dev/null &}
79
+ %x{DISPLAY=:99 firefox 2>/dev/null >/dev/null &}
80
+ end
81
+
82
+ task :kill_js do
83
+ puts "Killing vnc, xvfb, and ff processes"
84
+ %x{killall Xvnc4}
85
+ %x{killall Xvfb}
86
+ %x{killall firefox}
87
+ end
88
+ end
89
+ #task :cucumber => ['cucumber:setup_js_with_vnc4server', 'cucumber:all', 'cucumber:kill_js']
90
+ task :cucumber => ['cucumber:all']
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,39 @@
1
+ module TrackableTasks
2
+ # Stores information about a single execution of a trackable task
3
+ # Records errors and logs, and gives information about how long the execution took
4
+ class TaskRun < ActiveRecord::Base
5
+ set_table_name "trackable_tasks_task_runs"
6
+
7
+ validates_presence_of :start_time, :task_type
8
+ validates_inclusion_of :success, :in => [true, false]
9
+ validate :end_time_after_start_time
10
+
11
+ # adds an error if the end time is after the start time
12
+ # does not add an error if the end time is not set
13
+ def end_time_after_start_time
14
+ if self.end_time && self.end_time < self.start_time
15
+ self.errors.add(:end_time, "The end time must be after the start time")
16
+ end
17
+ end
18
+
19
+ # Appends the input text onto the log text
20
+ #
21
+ # I am aware that these two methods are duplicates nd we could merge them
22
+ # I am not convinced that the code would be any more readable or maintainable
23
+ # It does cut down the number of tests though
24
+ #
25
+ # @param [String] text The text to append to the log text
26
+ def add_log_text(text)
27
+ self.log_text = "" if self.log_text.nil?
28
+ self.log_text += text + "\n"
29
+ end
30
+
31
+ # Appends the input text onto the error text
32
+ #
33
+ # @param [String] text The text to append to the error text
34
+ def add_error_text(text)
35
+ self.error_text = "" if self.error_text.nil?
36
+ self.error_text += text + "\n"
37
+ end
38
+ end
39
+ end
data/config/routes.rb ADDED
File without changes
@@ -0,0 +1,43 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'active_record'
4
+ require 'rails/generators/active_record'
5
+
6
+ module TrackableTasks
7
+ module Generators
8
+ # Generator for copying the base migration and the rakefile to the including project
9
+ #
10
+ # @author Jeremiah Hemphill
11
+ class InstallGenerator < Rails::Generators::Base
12
+ include Rails::Generators::Migration
13
+
14
+ source_root File.expand_path(File.join(File.dirname(__FILE__), "templates"))
15
+
16
+ desc <<DESC
17
+ Description:
18
+ Copies over migrations and rakefile for the trackable tasks system.
19
+ DESC
20
+ # Implement the required interface for Rails::Generators::Migration.
21
+ # thanks Kaminari gem for this bit of code
22
+ def self.next_migration_number(dirname) #:nodoc:
23
+ next_migration_number = current_migration_number(dirname) + 1
24
+ if ActiveRecord::Base.timestamped_migrations
25
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
26
+ else
27
+ "%.3d" % next_migration_number
28
+ end
29
+ end
30
+
31
+ # Creates the rake file
32
+ def copy_rake_file
33
+ copy_file "trackable_tasks.rake", "lib/tasks/trackable_tasks.rake"
34
+ end
35
+
36
+ # Creates the migration
37
+ def copy_migration
38
+ migration_template "migrations/install_migration.rb.erb", "db/migrate/create_trackable_task_tables.rb"
39
+ end
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,16 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :trackable_tasks_task_runs do |t|
4
+ t.string :task_type
5
+ t.datetime :start_time
6
+ t.datetime :end_time
7
+ t.text :error_text
8
+ t.text :log_text
9
+ t.boolean :success, :default => false
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :task_runs
15
+ end
16
+ end
@@ -0,0 +1,48 @@
1
+ include Rake::DSL
2
+
3
+ # Careful when editing this file. It may be overwritten when the gem is updated.
4
+ namespace :trackable_task do
5
+
6
+ # loads the trackable tasks from /lib/trackable_tasks
7
+ # this may be moved to a config file at some point
8
+ trackable_task_list = []
9
+ Dir.foreach("#{::Rails.root.to_s}/lib/trackable_tasks/") do |file_name|
10
+ next if file_name == "." || file_name == ".."
11
+ file_name['.rb'] = ''
12
+ trackable_task_list.push(file_name.to_sym)
13
+ end
14
+
15
+ # turns the array of trackable tasks into individual rake tasks
16
+ trackable_task_list.each do |task_name|
17
+ desc "Trackable task for #{task_name.to_s.camelize}. Optionally set the log_level ENV variable to debug, notice, and error."
18
+ task task_name => :environment do |t|
19
+ log_level = ENV["log_level"]
20
+ run_trackable_task(task_name.to_s, log_level)
21
+ end
22
+ end
23
+
24
+ # single rake task that can run any trackable task
25
+ desc "Runs a trackable task by passing in the task as an argument"
26
+ task :run => :environment do |t|
27
+ task_name = ENV["task_name"]
28
+ log_level = ENV["log_level"]
29
+ run_trackable_task(task_name, log_level)
30
+ end
31
+
32
+ # Runs the trackable task by constantizing the task name, initializing, and calling run task
33
+ #
34
+ # @param [String] The name of the task class
35
+ # @param [String] The log level for the task
36
+ def run_trackable_task(task_name, log_level)
37
+ #puts "I am running #{task_name} at log level #{log_level}"
38
+ require "#{::Rails.root.to_s}/lib/trackable_tasks/#{task_name}.rb"
39
+ # if the class exists
40
+ task = task_name.camelize.constantize.new(log_level)
41
+ # if the class has a superclass of trackable task base
42
+ if task.class.superclass == TrackableTasks::Base
43
+ task.run_task
44
+ else
45
+ puts "The task #{task_name} was not an instance of TrackableTasks::Base."
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,87 @@
1
+ require 'ruby-debug'
2
+
3
+ module TrackableTasks
4
+ # Abstract trackable task to be overriden when creating a trackable task.
5
+ #
6
+ # Usage:
7
+ #
8
+ # Create a class
9
+ # MyGenericTask < TrackableTasks:Base
10
+ #
11
+ # Override the run method
12
+ # def run
13
+ # puts "Running my own code"
14
+ # end
15
+ #
16
+ # Instantiate and call run_task
17
+ #
18
+ # my_generic_task = MyGenericTask.new
19
+ # my_generic_task.run_task
20
+ #
21
+ # Bask in the glory of your saved logging information
22
+ #
23
+ # puts TrackableTasks.TaskRun.last.log_text
24
+ class Base
25
+
26
+ # Initializes the task run and sets the start time
27
+ #
28
+ # @param [Symbol] log_level The log level for the task, defaults to notice
29
+ def initialize(log_level = :notice)
30
+ @task_run = TrackableTasks::TaskRun.create(:start_time => Time.now, :task_type => self.class.name, :success => true)
31
+
32
+ @log_levels = [:debug, :notice, :error]
33
+ @log_level = allowable_log_level(log_level)
34
+ end
35
+
36
+ # Checks if the log level is an allowable level, then returns it or notice if it is not allowable
37
+ #
38
+ # @param [Sybmol] log_level Log level to check
39
+ # @return [Symbol] The given log level or notice if it is not allowable
40
+ def allowable_log_level(log_level)
41
+ log_level = "" if log_level.nil?
42
+ log_level = log_level.to_sym unless log_level.is_a?(Symbol)
43
+ if !@log_levels.include?(log_level)
44
+ log_level = :notice
45
+ end
46
+ return log_level
47
+ end
48
+
49
+ # this calls task with error catching
50
+ # if an error is caught, sets success to false and records the error
51
+ # either way, sets an end time and saves the task run information
52
+ def run_task
53
+ begin
54
+ run
55
+ rescue Exception => e
56
+ @task_run.add_error_text(e.class.name + ": " + e.message)
57
+ @task_run.success = false
58
+ end
59
+ @task_run.end_time = Time.now
60
+ @task_run.save
61
+ end
62
+
63
+ # The run method must be overridden for the task to work
64
+ # code for the actual task goes here
65
+ def run
66
+ raise NotImplementedError.new("The run method must be overridden")
67
+ end
68
+
69
+ # Adds test to the log for the task run
70
+ # Depending on the @log_level, save or don't save the text
71
+ # If the level is :error, save to the error log
72
+ #
73
+ # @param [String] text The text to add to the log
74
+ # @param [Symbol] level The log level of the text
75
+ def log(text, level = :notice)
76
+ level = allowable_log_level(level)
77
+
78
+ if level == :debug && @log_level == :debug
79
+ @task_run.add_log_text(text)
80
+ elsif level == :notice && (@log_level == :debug || @log_level == :notice)
81
+ @task_run.add_log_text(text)
82
+ elsif level == :error
83
+ @task_run.add_error_text(text)
84
+ end
85
+ end
86
+ end
87
+ end
File without changes
@@ -0,0 +1,7 @@
1
+ require "rails"
2
+
3
+ module TrackableTasks
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
7
+
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+
3
+ require 'trackable_tasks/config'
4
+
5
+ module TrackableTasks
6
+ class Railtie < ::Rails::Railtie
7
+ initializer 'trackable_tasks' do |app|
8
+ # add mixins here
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # make the TrackableTasks::Base available inside the project
2
+ require File.join(%w{trackable_tasks base})
3
+
4
+ require "trackable_tasks/engine"
5
+ #require "trackable_tasks/railtie"
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end