Class: Makit::TaskInfo

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • task_name (String, Symbol)

    Name of the task to track



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_tasksObject

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_dirString, Time (readonly)

Returns:

  • (String)

    Name of the task being tracked

  • (Time)

    Time when task execution started

  • (Time)

    Time when task execution ended

  • (String)

    Directory path for task artifacts



23
24
25
# File 'lib/makit/task_info.rb', line 23

def artifacts_dir
  @artifacts_dir
end

#end_timeString, Time (readonly)

Returns:

  • (String)

    Name of the task being tracked

  • (Time)

    Time when task execution started

  • (Time)

    Time when task execution ended

  • (String)

    Directory path for task artifacts



23
24
25
# File 'lib/makit/task_info.rb', line 23

def end_time
  @end_time
end

#start_timeString, Time (readonly)

Returns:

  • (String)

    Name of the task being tracked

  • (Time)

    Time when task execution started

  • (Time)

    Time when task execution ended

  • (String)

    Directory path for task artifacts



23
24
25
# File 'lib/makit/task_info.rb', line 23

def start_time
  @start_time
end

#task_nameString, Time (readonly)

Returns:

  • (String)

    Name of the task being tracked

  • (Time)

    Time when task execution started

  • (Time)

    Time when task execution ended

  • (String)

    Directory path for task artifacts



23
24
25
# File 'lib/makit/task_info.rb', line 23

def task_name
  @task_name
end

Class Method Details

.infer_task_nameString?

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.

Returns:

  • (String, nil)

    The inferred task name, or nil if unavailable



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_nameString

Get the name of the slowest completed task

Analyzes all completed tasks and returns the name of the one with the longest execution duration.

Returns:

  • (String)

    Name of the slowest task, or “unknown” if no tasks completed



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.

Examples:

Makit::TaskInfo.track(:generate) do
  # generate code
end

Parameters:

  • task_name (String, Symbol)

    Name of the task to track

Yields:

  • (TaskInfo)

    The task info instance for use within the block

Returns:

  • (Object)

    Result of the yielded block



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.

Yields:

  • (TaskInfo)

    The task info instance for use within the block

Returns:

  • (Object)

    Result of the yielded block



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_takennil

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.

Returns:

  • (nil)


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