Class: Specbandit::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/specbandit/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: Specbandit.configuration.key, batch_size: Specbandit.configuration.batch_size, adapter: nil, key_rerun: Specbandit.configuration.key_rerun, key_rerun_ttl: Specbandit.configuration.key_rerun_ttl, rerun: Specbandit.configuration.rerun, verbose: Specbandit.configuration.verbose, queue: nil, output: $stdout, rspec_opts: nil) ⇒ Worker

Returns a new instance of Worker.



10
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
# File 'lib/specbandit/worker.rb', line 10

def initialize(
  key: Specbandit.configuration.key,
  batch_size: Specbandit.configuration.batch_size,
  adapter: nil,
  key_rerun: Specbandit.configuration.key_rerun,
  key_rerun_ttl: Specbandit.configuration.key_rerun_ttl,
  rerun: Specbandit.configuration.rerun,
  verbose: Specbandit.configuration.verbose,
  queue: nil,
  output: $stdout,
  # Legacy parameter for backward compatibility.
  # When adapter is not provided, rspec_opts is used to build an RspecAdapter.
  rspec_opts: nil
)
  @key = key
  @batch_size = batch_size
  @key_rerun = key_rerun
  @key_rerun_ttl = key_rerun_ttl
  @rerun = rerun
  @verbose = verbose
  @queue = queue || RedisQueue.new
  @output = output
  @batch_results = []
  @accumulated_examples = []
  @accumulated_summary = { duration: 0.0, example_count: 0, failure_count: 0, pending_count: 0,
                           errors_outside_of_examples_count: 0 }

  # Support both new adapter-based and legacy rspec_opts-based construction.
  # If no adapter is provided, fall back to RspecAdapter for backward compatibility.
  @adapter = adapter || RspecAdapter.new(
    rspec_opts: rspec_opts || Specbandit.configuration.rspec_opts,
    verbose: verbose,
    output: output
  )
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/specbandit/worker.rb', line 8

def adapter
  @adapter
end

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



8
9
10
# File 'lib/specbandit/worker.rb', line 8

def batch_size
  @batch_size
end

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/specbandit/worker.rb', line 8

def key
  @key
end

#key_rerunObject (readonly)

Returns the value of attribute key_rerun.



8
9
10
# File 'lib/specbandit/worker.rb', line 8

def key_rerun
  @key_rerun
end

#key_rerun_ttlObject (readonly)

Returns the value of attribute key_rerun_ttl.



8
9
10
# File 'lib/specbandit/worker.rb', line 8

def key_rerun_ttl
  @key_rerun_ttl
end

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/specbandit/worker.rb', line 8

def output
  @output
end

#queueObject (readonly)

Returns the value of attribute queue.



8
9
10
# File 'lib/specbandit/worker.rb', line 8

def queue
  @queue
end

#rerunObject (readonly)

Returns the value of attribute rerun.



8
9
10
# File 'lib/specbandit/worker.rb', line 8

def rerun
  @rerun
end

#verboseObject (readonly)

Returns the value of attribute verbose.



8
9
10
# File 'lib/specbandit/worker.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#runObject

Main entry point. Detects the operating mode and dispatches accordingly.

Returns 0 if all batches passed (or nothing to do), 1 if any batch failed.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/specbandit/worker.rb', line 49

def run
  adapter.setup

  exit_code = if key_rerun
                rerun_files = queue.read_all(key_rerun)
                if rerun_files.any?
                  run_replay(rerun_files)
                elsif rerun
                  output.puts "[specbandit] ERROR: --rerun flag is set but rerun key '#{key_rerun}' is empty."
                  output.puts '[specbandit] The rerun key may have expired (TTL) or Redis was flushed.'
                  output.puts '[specbandit] Cannot replay — failing to prevent silent false pass.'
                  1
                else
                  run_steal(record: true)
                end
              else
                run_steal(record: false)
              end

  print_summary if @batch_results.any?
  merge_json_results
  write_github_step_summary if ENV['GITHUB_STEP_SUMMARY']

  exit_code
ensure
  adapter.teardown
end