Class: ConcurrentPipeline::Pipeline::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent_pipeline/pipeline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipeline:, pool:) ⇒ Wrapper

Returns a new instance of Wrapper.



33
34
35
36
# File 'lib/concurrent_pipeline/pipeline.rb', line 33

def initialize(pipeline:, pool:)
  @pipeline = pipeline
  @pool = pool
end

Instance Attribute Details

#pipelineObject (readonly)

Returns the value of attribute pipeline.



32
33
34
# File 'lib/concurrent_pipeline/pipeline.rb', line 32

def pipeline
  @pipeline
end

#poolObject (readonly)

Returns the value of attribute pool.



32
33
34
# File 'lib/concurrent_pipeline/pipeline.rb', line 32

def pool
  @pool
end

Instance Method Details

#changesetObject



135
136
137
# File 'lib/concurrent_pipeline/pipeline.rb', line 135

def changeset
  pipeline.changeset
end

#create_pipeline_stepsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/concurrent_pipeline/pipeline.rb', line 85

def create_pipeline_steps
  sequence = (
    if pipeline.respond_to?(:steps)
      pipeline.steps
    else
      [:perform]
    end
  )

  sequence.each_with_index do |sub_seq, i|
    Array(sub_seq).each do |step_name|
      changeset.create(
        PipelineStep,
        pipeline_id: id,
        name: step_name,
        sequence: i
      )
    end
  end
end

#done?Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
# File 'lib/concurrent_pipeline/pipeline.rb', line 123

def done?
  if pipeline.respond_to?(:done?)
    pipeline.done?
  else
    !pipeline_steps.empty? && pipeline_steps.all?(&:completed_at)
  end
end

#idObject



38
39
40
41
42
43
44
45
46
# File 'lib/concurrent_pipeline/pipeline.rb', line 38

def id
  pipeline_id = (
    if pipeline.class.target_type
      pipeline.target.id
    end
  )

  [pipeline.class.name, pipeline_id].compact.join("__")
end

#performObject



48
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
76
77
78
79
# File 'lib/concurrent_pipeline/pipeline.rb', line 48

def perform
  if pipeline_steps.empty?
    create_pipeline_steps
  else
    pipeline_steps
      .reject(&:completed_at)
      .group_by(&:sequence)
      .values
      .first
      .map { |step|
        wrapper = self
        -> () do
          begin
            wrapper.pipeline.public_send(step.name)
            wrapper.changeset.update(
              step,
              completed_at: Time.now.iso8601,
              result: :success
            )
          rescue => e
            wrapper.changeset.update(
              step,
              completed_at: Time.now.iso8601,
              result: :failure,
              error: {class: e.class, message: e.message, backtrace: e.backtrace}
            )
          end
        end
      }
    .then { pool.process(_1) }
  end
end

#pipeline_stepsObject



106
107
108
109
110
111
112
113
# File 'lib/concurrent_pipeline/pipeline.rb', line 106

def pipeline_steps
  @pipeline_steps ||= (
    store
      .all(PipelineStep)
      .select { _1.pipeline_id == id }
      .sort_by(&:sequence)
  )
end

#ready?Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
# File 'lib/concurrent_pipeline/pipeline.rb', line 115

def ready?
  if pipeline.respond_to?(:ready?)
    pipeline.ready?
  else
    true
  end
end

#should_perform?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/concurrent_pipeline/pipeline.rb', line 81

def should_perform?
  ready? && !done?
end

#storeObject



131
132
133
# File 'lib/concurrent_pipeline/pipeline.rb', line 131

def store
  pipeline.store
end

#stream(type, payload) ⇒ Object



139
140
141
# File 'lib/concurrent_pipeline/pipeline.rb', line 139

def stream(type, payload)
  pipeline.stream.push(type, payload)
end