Class: TrackableTasks::Base
- Inherits:
-
Object
- Object
- TrackableTasks::Base
- Defined in:
- lib/trackable_tasks/base.rb
Overview
Abstract trackable task to be overriden when creating a trackable task.
Usage:
Create a class MyGenericTask < TrackableTasks:Base
Override the run method def run
puts "Running my own code"
end
Instantiate and call run_task
my_generic_task = MyGenericTask.new my_generic_task.run_task
Bask in the glory of your saved logging information
puts TrackableTasks.TaskRun.last.log_text
Instance Method Summary collapse
-
#allowable_log_level(log_level) ⇒ Symbol
Checks if the log level is an allowable level, then returns it or notice if it is not allowable.
-
#finally ⇒ Object
The finally method can be optionally overridden for code that must run after run is completed This could be for things like temp file cleanup.
-
#initialize(log_level = :notice) ⇒ Base
constructor
Initializes the task run and sets the start time.
-
#log(text, level = :notice) ⇒ Object
Adds test to the log for the task run Depending on the @log_level, save or don’t save the text If the level is :error, save to the error log.
-
#run ⇒ Object
The run method must be overridden for the task to work code for the actual task goes here.
-
#run_task ⇒ Object
this calls task with error catching if an error is caught, sets success to false and records the error either way, sets an end time and saves the task run information.
Constructor Details
#initialize(log_level = :notice) ⇒ Base
Initializes the task run and sets the start time
27 28 29 30 31 32 |
# File 'lib/trackable_tasks/base.rb', line 27 def initialize(log_level = :notice) @task_run = TrackableTasks::TaskRun.create(:start_time => Time.now, :task_type => self.class.name, :success => true) @log_levels = [:debug, :notice, :error] @log_level = allowable_log_level(log_level) end |
Instance Method Details
#allowable_log_level(log_level) ⇒ Symbol
Checks if the log level is an allowable level, then returns it or notice if it is not allowable
38 39 40 41 42 43 44 45 |
# File 'lib/trackable_tasks/base.rb', line 38 def allowable_log_level(log_level) log_level = "" if log_level.nil? log_level = log_level.to_sym unless log_level.is_a?(Symbol) if !@log_levels.include?(log_level) log_level = :notice end return log_level end |
#finally ⇒ Object
The finally method can be optionally overridden for code that must run after run is completed This could be for things like temp file cleanup
Note that errors are not caught and logged here
78 79 |
# File 'lib/trackable_tasks/base.rb', line 78 def finally end |
#log(text, level = :notice) ⇒ Object
Adds test to the log for the task run Depending on the @log_level, save or don’t save the text If the level is :error, save to the error log
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/trackable_tasks/base.rb', line 87 def log(text, level = :notice) level = allowable_log_level(level) if level == :debug && @log_level == :debug @task_run.add_log_text(text) elsif level == :notice && (@log_level == :debug || @log_level == :notice) @task_run.add_log_text(text) elsif level == :error @task_run.add_error_text(text) end end |
#run ⇒ Object
The run method must be overridden for the task to work code for the actual task goes here
70 71 72 |
# File 'lib/trackable_tasks/base.rb', line 70 def run raise NotImplementedError.new("The run method must be overridden") end |
#run_task ⇒ Object
this calls task with error catching if an error is caught, sets success to false and records the error either way, sets an end time and saves the task run information
After the run completes, whether or not the run succeeds, run finally If finally has an error, don’t catch it
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/trackable_tasks/base.rb', line 53 def run_task begin run rescue Exception => e @task_run.add_error_text(e.class.name + ": " + e.) @task_run.add_error_text(e.backtrace.inspect) @task_run.success = false end finally @task_run.end_time = Time.now @task_run.save end |