Class: Taski::Execution::ParallelProgressDisplay

Inherits:
Object
  • Object
show all
Defined in:
lib/taski/execution/parallel_progress_display.rb

Defined Under Namespace

Classes: TaskProgress

Constant Summary collapse

SPINNER_FRAMES =
%w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏].freeze

Instance Method Summary collapse

Constructor Details

#initialize(output: $stdout) ⇒ ParallelProgressDisplay

Returns a new instance of ParallelProgressDisplay.



22
23
24
25
26
27
28
29
# File 'lib/taski/execution/parallel_progress_display.rb', line 22

def initialize(output: $stdout)
  @output = output
  @tasks = {}
  @monitor = Monitor.new
  @spinner_index = 0
  @renderer_thread = nil
  @running = false
end

Instance Method Details

#register_task(task_class) ⇒ Object

Parameters:

  • task_class (Class)

    The task class to register



32
33
34
35
36
# File 'lib/taski/execution/parallel_progress_display.rb', line 32

def register_task(task_class)
  @monitor.synchronize do
    @tasks[task_class] = TaskProgress.new
  end
end

#renderObject



76
77
78
79
80
81
82
83
# File 'lib/taski/execution/parallel_progress_display.rb', line 76

def render
  @monitor.synchronize do
    @tasks.each do |task_class, progress|
      line = format_task_line(task_class, progress)
      @output.puts line
    end
  end
end

#startObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/taski/execution/parallel_progress_display.rb', line 85

def start
  return if @running

  @running = true
  @renderer_thread = Thread.new do
    loop do
      break unless @running
      render_live
      sleep 0.1
    end
  end
end

#stopObject



98
99
100
101
102
103
104
# File 'lib/taski/execution/parallel_progress_display.rb', line 98

def stop
  return unless @running

  @running = false
  @renderer_thread&.join
  render_final
end

#task_registered?(task_class) ⇒ Boolean

Returns true if the task is registered.

Parameters:

  • task_class (Class)

    The task class to check

Returns:

  • (Boolean)

    true if the task is registered



40
41
42
43
44
# File 'lib/taski/execution/parallel_progress_display.rb', line 40

def task_registered?(task_class)
  @monitor.synchronize do
    @tasks.key?(task_class)
  end
end

#task_state(task_class) ⇒ Symbol

Returns The task state.

Parameters:

  • task_class (Class)

    The task class

Returns:

  • (Symbol)

    The task state



70
71
72
73
74
# File 'lib/taski/execution/parallel_progress_display.rb', line 70

def task_state(task_class)
  @monitor.synchronize do
    @tasks[task_class]&.state
  end
end

#update_task(task_class, state:, duration: nil, error: nil) ⇒ Object

Parameters:

  • task_class (Class)

    The task class to update

  • state (Symbol)

    The new state (:pending, :running, :completed, :failed)

  • duration (Float) (defaults to: nil)

    Duration in milliseconds (for completed tasks)

  • error (Exception) (defaults to: nil)

    Error object (for failed tasks)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/taski/execution/parallel_progress_display.rb', line 50

def update_task(task_class, state:, duration: nil, error: nil)
  @monitor.synchronize do
    progress = @tasks[task_class]
    return unless progress

    progress.state = state
    progress.duration = duration if duration
    progress.error = error if error

    case state
    when :running
      progress.start_time = Time.now
    when :completed, :failed
      progress.end_time = Time.now
    end
  end
end