Class: Fractor::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/fractor/cli.rb

Overview

Main Fractor CLI

Instance Method Summary collapse

Instance Method Details

#execute(file) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/fractor/cli.rb', line 95

def execute(file)
  setup_logging

  unless File.exist?(file)
    warn "Error: File not found: #{file}"
    exit 1
  end

  begin
    workflow = load_workflow_class(file)
    input_data = parse_input_data

    instance = workflow.new

    puts "Running workflow: #{workflow.workflow_name}"
    puts "Mode: #{workflow.workflow_mode}"
    puts "Input: #{input_data.inspect}" if options[:verbose]

    start_time = Time.now
    result = instance.execute(input: input_data)
    elapsed = Time.now - start_time

    puts "\nWorkflow completed in #{elapsed.round(3)}s"

    if result.success?
      puts "Status: ✓ SUCCESS"
      puts "Result: #{result.result.inspect}" if result.result
    else
      puts "Status: ✗ FAILED"
      puts "Error: #{result.error}" if result.error
    end

    puts "Jobs completed: #{result.jobs_completed}" if result.jobs_completed
    puts "Jobs failed: #{result.jobs_failed}" if result.jobs_failed

    exit(result.success? ? 0 : 1)
  rescue StandardError => e
    warn "Error: #{e.class}: #{e.message}"
    warn e.backtrace.first(5) if options[:verbose]
    exit 1
  end
end

#supervisor(worker_class, *inputs) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/fractor/cli.rb', line 150

def supervisor(worker_class, *inputs)
  setup_logging

  begin
    # Load the worker class
    worker = load_worker_class(worker_class)

    # Parse input data if provided
    work_items = if options[:input]
                   parse_input_file(options[:input])
                 elsif inputs.any?
                   inputs.map { |input| Fractor::Work.new(input) }
                 else
                   warn "Error: No input data provided. Use --input FILE or provide INPUTS"
                   exit 1
                 end

    num_workers = options[:workers] || 4
    continuous_mode = options[:continuous] || false

    puts "Starting Fractor Supervisor..."
    puts "Worker: #{worker}"
    puts "Workers: #{num_workers}"
    puts "Mode: #{continuous_mode ? 'Continuous' : 'Batch'}"
    puts "Work items: #{work_items.size}"
    puts

    supervisor = Fractor::Supervisor.new(
      worker_pools: [{ worker_class: worker, num_workers: num_workers }],
      continuous_mode: continuous_mode,
    )

    # Add work items
    work_items.each { |item| supervisor.add_work_item(item) }

    # Run supervisor
    start_time = Time.now
    supervisor.run
    elapsed = Time.now - start_time

    results = supervisor.results

    puts
    puts "Completed in #{elapsed.round(3)}s"
    puts "Results: #{results.results.size} successful"
    puts "Errors: #{results.errors.size} failed"

    if options[:metrics] && defined?(Fractor::PerformanceMonitor)
      show_metrics(supervisor)
    end

    # Exit with error code if any failures
    exit(results.errors.empty? ? 0 : 1)
  rescue StandardError => e
    warn "Error: #{e.class}: #{e.message}"
    warn e.backtrace.first(5) if options[:verbose]
    exit 1
  end
end

#validate(file) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fractor/cli.rb', line 15

def validate(file)
  setup_logging

  unless File.exist?(file)
    warn "Error: File not found: #{file}"
    exit 1
  end

  begin
    # Load and validate the workflow
    workflow = load_workflow_class(file)

    puts "✓ Valid workflow: #{workflow.workflow_name}"
    puts "  Mode: #{workflow.workflow_mode}"
    puts "  Jobs: #{workflow.jobs.size}"

    # Validate each job
    workflow.jobs.each do |name, job|
      puts "  - #{name} (#{job.worker_class})"
      puts "    Input: #{job.input_type}" if job.input_type
      puts "    Output: #{job.output_type}" if job.output_type
      puts "    Needs: #{job.needs.join(', ')}" if job.needs.any?
    end
  rescue StandardError => e
    warn "Error: #{e.class}: #{e.message}"
    warn e.backtrace.first(5) if options[:verbose]
    exit 1
  end
end

#versionObject



211
212
213
# File 'lib/fractor/cli.rb', line 211

def version
  puts "Fractor #{Fractor::VERSION}"
end

#visualize(file) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fractor/cli.rb', line 52

def visualize(file)
  setup_logging

  unless File.exist?(file)
    warn "Error: File not found: #{file}"
    exit 1
  end

  begin
    workflow = load_workflow_class(file)
    visualizer = Fractor::Workflow::Visualizer.new(workflow)

    output = case options[:format].to_sym
             when :mermaid
               visualizer.to_mermaid
             when :dot
               visualizer.to_dot
             else
               visualizer.to_ascii
             end

    if options[:output]
      File.write(options[:output], output)
      puts "Visualization written to: #{options[:output]}"
    else
      puts output
    end
  rescue StandardError => e
    warn "Error: #{e.class}: #{e.message}"
    warn e.backtrace.first(5) if options[:verbose]
    exit 1
  end
end