Class: Devformance::RunOrchestrator

Inherits:
Object
  • Object
show all
Defined in:
lib/devformance/run_orchestrator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(framework: nil) ⇒ RunOrchestrator



7
8
9
# File 'lib/devformance/run_orchestrator.rb', line 7

def initialize(framework: nil)
  @framework = framework || detect_framework
end

Class Method Details

.call(framework: nil) ⇒ Object



3
4
5
# File 'lib/devformance/run_orchestrator.rb', line 3

def self.call(framework: nil)
  new(framework: framework).call
end

Instance Method Details

#callObject



11
12
13
14
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
44
45
46
47
48
# File 'lib/devformance/run_orchestrator.rb', line 11

def call
  file_paths = @framework.discover_files
  if file_paths.empty?
    return { error: "No test files found matching #{@framework.file_pattern}" }
  end

  tagged = file_paths.select { |f| File.read(f).match?(/devformance/i) }
  file_paths = tagged if tagged.any?

  run = ::Devformance::Run.create_for_files(file_paths)

  file_metas = file_paths.map do |path|
    file_key = ::Devformance::FileResult.file_key_for(path)
    ::Devformance::FileResult.create!(
      run_id:     run.run_id,
      file_key:   file_key,
      file_path:  path,
      status:     :pending
    )
    { file_key: file_key, file_path: path, display_name: path.sub(Rails.root.to_s + "/", "") }
  end

  ActionCable.server.broadcast(
    "devformance:run:#{run.run_id}",
    { type: "run_started", run_id: run.run_id, files: file_metas, framework: @framework.name }
  )

  file_metas.each do |meta|
    ::Devformance::FileRunnerJob.perform_later(
      run_id:    run.run_id,
      file_path: meta[:file_path],
      file_key:  meta[:file_key],
      framework: @framework.name
    )
  end

  { run_id: run.run_id, file_count: file_metas.size, files: file_metas, framework: @framework.name }
end