Class: TrackableTasks::Base

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(log_level = :notice) ⇒ Base

Initializes the task run and sets the start time

Parameters:

  • log_level (Symbol) (defaults to: :notice)

    The log level for the task, defaults to notice



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

Parameters:

  • log_level (Sybmol)

    Log level to check

Returns:

  • (Symbol)

    The given log level 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

#finallyObject

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

Parameters:

  • text (String)

    The text to add to the log

  • level (Symbol) (defaults to: :notice)

    The log level of the text



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

#runObject

The run method must be overridden for the task to work code for the actual task goes here

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/trackable_tasks/base.rb', line 70

def run
  raise NotImplementedError.new("The run method must be overridden")
end

#run_taskObject

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.message)
    @task_run.add_error_text(e.backtrace.inspect)
    @task_run.success = false
  end 

  finally

  @task_run.end_time = Time.now
  @task_run.save
end