Class: Legion::Extensions::MindGrowth::Helpers::BuildPipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/mind_growth/helpers/build_pipeline.rb

Constant Summary collapse

STAGES =
%i[scaffold implement test validate register complete failed].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proposal) ⇒ BuildPipeline

Returns a new instance of BuildPipeline.



12
13
14
15
16
17
18
19
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 12

def initialize(proposal)
  @proposal     = proposal
  @stage        = :scaffold
  @errors       = []
  @artifacts    = {}
  @started_at   = Time.now.utc
  @completed_at = nil
end

Instance Attribute Details

#artifactsObject (readonly)

Returns the value of attribute artifacts.



10
11
12
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 10

def artifacts
  @artifacts
end

#completed_atObject (readonly)

Returns the value of attribute completed_at.



10
11
12
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 10

def completed_at
  @completed_at
end

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 10

def errors
  @errors
end

#proposalObject (readonly)

Returns the value of attribute proposal.



10
11
12
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 10

def proposal
  @proposal
end

#stageObject (readonly)

Returns the value of attribute stage.



10
11
12
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 10

def stage
  @stage
end

#started_atObject (readonly)

Returns the value of attribute started_at.



10
11
12
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 10

def started_at
  @started_at
end

Instance Method Details

#advance!(result) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 21

def advance!(result)
  return if complete? || failed?

  if timed_out?
    @errors << { stage: @stage, error: 'build timeout exceeded', at: Time.now.utc }
    @stage = :failed
    return
  end

  if result[:success]
    @artifacts[@stage] = result
    next_idx      = STAGES.index(@stage) + 1
    @stage        = STAGES[next_idx] || :complete
    @completed_at = Time.now.utc if @stage == :complete
  else
    @errors << { stage: @stage, error: result[:error], at: Time.now.utc }
    @stage = :failed if @errors.size >= Helpers::Constants::MAX_FIX_ATTEMPTS
  end
end

#complete?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 41

def complete?
  @stage == :complete
end

#duration_msObject



65
66
67
68
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 65

def duration_ms
  end_time = @completed_at || Time.now.utc
  ((end_time - @started_at) * 1000).round
end

#failed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 45

def failed?
  @stage == :failed
end

#timed_out?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 49

def timed_out?
  duration_ms >= Helpers::Constants::BUILD_TIMEOUT_MS
end

#to_hObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/legion/extensions/mind_growth/helpers/build_pipeline.rb', line 53

def to_h
  {
    proposal_id:  @proposal.id,
    stage:        @stage,
    errors:       @errors,
    artifacts:    @artifacts,
    started_at:   @started_at,
    completed_at: @completed_at,
    duration_ms:  duration_ms
  }
end