Class: Pipeline::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/pipeline/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_failure_mode=(mode) ⇒ Object



25
26
27
28
# File 'lib/pipeline/base.rb', line 25

def self.default_failure_mode=(mode)
  new_mode = [:pause, :cancel].include?(mode) ? mode : :pause
  self.failure_mode = new_mode
end

.define_stages(stages) ⇒ Object



21
22
23
# File 'lib/pipeline/base.rb', line 21

def self.define_stages(stages)
  self.defined_stages = stages.build_chain
end

Instance Method Details

#after_initializeObject



30
31
32
33
34
35
36
37
# File 'lib/pipeline/base.rb', line 30

def after_initialize
  if new_record?
    self[:status] = :not_started
    self.class.defined_stages.each do |stage_class|
      stages << stage_class.new(:pipeline => self)
    end
  end
end

#cancelObject

Raises:



62
63
64
65
# File 'lib/pipeline/base.rb', line 62

def cancel
  raise InvalidStatusError.new(status) unless ok_to_resume?
  self.status = :failed
end

#ok_to_resume?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/pipeline/base.rb', line 67

def ok_to_resume?
  [:not_started, :paused, :retry].include?(status)
end

#performObject

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pipeline/base.rb', line 39

def perform
  reload unless new_record?
  raise InvalidStatusError.new(status) unless ok_to_resume?
  begin
    _setup
    stages.each do |stage|
      stage.perform unless stage.completed?
    end
    self.status = :completed
  rescue IrrecoverableError
    self.status = :failed
  rescue RecoverableError => e
    if e.input_required?
      self.status = :paused
    else
      self.status = :retry
      raise e
    end
  rescue Exception
    self.status = (failure_mode == :cancel ? :failed : :paused)
  end
end