Class: Makit::TaskInfo
- Inherits:
-
Object
- Object
- Makit::TaskInfo
- Defined in:
- lib/makit/task_info.rb
Overview
Task execution time tracking and reporting
This class provides functionality to track the execution time of tasks, store timing information, and generate performance reports. It’s designed to help identify performance bottlenecks in build processes.
Class Attribute Summary collapse
-
.completed_tasks ⇒ Object
Returns the value of attribute completed_tasks.
Instance Attribute Summary collapse
- #artifacts_dir ⇒ String, Time readonly
- #end_time ⇒ String, Time readonly
- #start_time ⇒ String, Time readonly
- #task_name ⇒ String, Time readonly
Class Method Summary collapse
-
.infer_task_name ⇒ String?
Attempt to infer the current task name.
-
.slowest_task_name ⇒ String
Get the name of the slowest completed task.
-
.track(task_name) {|TaskInfo| ... } ⇒ Object
Track execution time of a task with a block.
-
.track_inferred {|TaskInfo| ... } ⇒ Object
Track execution time with inferred task name.
Instance Method Summary collapse
-
#initialize(task_name) ⇒ TaskInfo
constructor
Initialize a new task tracking instance.
-
#report_and_store_time_taken ⇒ nil
Report and store the task execution time.
Constructor Details
#initialize(task_name) ⇒ TaskInfo
Initialize a new task tracking instance
Sets up timing tracking for a named task, creates an artifacts directory path, and records the start time.
80 81 82 83 84 85 |
# File 'lib/makit/task_info.rb', line 80 def initialize(task_name) @task_name = task_name @artifacts_dir = File.join("artifacts", task_name.to_s.gsub(":", "_")) @start_time = Time.now # at_exit { report_and_store_time_taken } end |
Class Attribute Details
.completed_tasks ⇒ Object
Returns the value of attribute completed_tasks.
15 16 17 |
# File 'lib/makit/task_info.rb', line 15 def completed_tasks @completed_tasks end |
Instance Attribute Details
#artifacts_dir ⇒ String, Time (readonly)
23 24 25 |
# File 'lib/makit/task_info.rb', line 23 def artifacts_dir @artifacts_dir end |
#end_time ⇒ String, Time (readonly)
23 24 25 |
# File 'lib/makit/task_info.rb', line 23 def end_time @end_time end |
#start_time ⇒ String, Time (readonly)
23 24 25 |
# File 'lib/makit/task_info.rb', line 23 def start_time @start_time end |
#task_name ⇒ String, Time (readonly)
23 24 25 |
# File 'lib/makit/task_info.rb', line 23 def task_name @task_name end |
Class Method Details
.infer_task_name ⇒ String?
Attempt to infer the current task name
This method would extract the current Rake task name from the application context, but is currently not implemented.
126 127 128 |
# File 'lib/makit/task_info.rb', line 126 def self.infer_task_name # Rake.application.current_scope.join(":") rescue "unknown" end |
.slowest_task_name ⇒ String
Get the name of the slowest completed task
Analyzes all completed tasks and returns the name of the one with the longest execution duration.
31 32 33 34 35 |
# File 'lib/makit/task_info.rb', line 31 def self.slowest_task_name return "unknown" if @completed_tasks.empty? @completed_tasks.max_by { |_task_name, task_info| task_info[:duration] }[0] end |
.track(task_name) {|TaskInfo| ... } ⇒ Object
Track execution time of a task with a block
Creates a new TaskInfo instance, displays the task name, executes the provided block, and ensures timing is reported even if an exception occurs.
51 52 53 54 55 56 57 58 |
# File 'lib/makit/task_info.rb', line 51 def self.track(task_name) task = new(task_name) Makit::Logging.default_logger.task_start(task_name) #Makit::SHOW.task(task_name) yield(task) ensure task&.report_and_store_time_taken end |
.track_inferred {|TaskInfo| ... } ⇒ Object
Track execution time with inferred task name
Similar to track() but attempts to infer the task name automatically. Currently returns nil as task name inference is not implemented.
67 68 69 70 71 72 |
# File 'lib/makit/task_info.rb', line 67 def self.track_inferred(&block) task_name = infer_task_name Makit::Logging.default_logger.task_start(task_name) #Makit::SHOW.task(task_name) track(task_name, &block) end |
Instance Method Details
#report_and_store_time_taken ⇒ nil
Report and store the task execution time
Calculates the duration, displays a human-readable completion message, and stores the timing information in the class-level completed_tasks hash.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/makit/task_info.rb', line 93 def report_and_store_time_taken @end_time = Time.now duration = @end_time - @start_time # leverate the Makit::Humanize.get_humanized_duration method humanized_duration = Makit::Humanize.get_humanized_duration(duration) puts " #{@task_name} completed in #{humanized_duration}".colorize(:grey) # puts "[TaskGuard] Task '#{@task_name}' completed in #{duration}." # Add to class-level storage self.class.completed_tasks[@task_name] = { start_time: @start_time, end_time: @end_time, duration: duration, } end |