Class: FlowCore::Instance

Inherits:
ApplicationRecord show all
Defined in:
app/models/flow_core/instance.rb

Constant Summary collapse

FORBIDDEN_ATTRIBUTES =
%i[
  workflow_id stage activated_at finished_at canceled_at terminated_at terminated_reason
  errored_at rescued_at suspended_at resumed_at created_at updated_at
].freeze

Instance Method Summary collapse

Instance Method Details

#activeObject



50
51
52
53
54
55
56
57
58
59
# File 'app/models/flow_core/instance.rb', line 50

def active
  return false unless can_active?

  transaction do
    tokens.create! place: workflow.start_place
    update! stage: :activated, activated_at: Time.zone.now
  end

  true
end

#active!Object



76
77
78
# File 'app/models/flow_core/instance.rb', line 76

def active!
  active || raise(FlowCore::InvalidTransition, "Can't active Instance##{id}")
end

#can_active?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/flow_core/instance.rb', line 42

def can_active?
  created?
end

#can_finish?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/flow_core/instance.rb', line 46

def can_finish?
  activated?
end

#error!Object



84
85
86
87
88
# File 'app/models/flow_core/instance.rb', line 84

def error!
  return if errored?

  update! errored_at: Time.zone.now
end

#errored?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/flow_core/instance.rb', line 34

def errored?
  errored_at.present?
end

#finishObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/flow_core/instance.rb', line 61

def finish
  return false unless can_finish?

  transaction do
    update! stage: :finished, finished_at: Time.zone.now

    tasks.where(stage: %i[created enabled]).find_each do |task|
      task.terminate! reason: "Instance finished"
    end
    tokens.where(stage: %i[free locked]).find_each(&:terminate!)
  end

  true
end

#finish!Object



80
81
82
# File 'app/models/flow_core/instance.rb', line 80

def finish!
  finish || raise(FlowCore::InvalidTransition, "Can't finish Instance##{id}")
end

#rescue!Object



90
91
92
93
94
95
# File 'app/models/flow_core/instance.rb', line 90

def rescue!
  return unless errored?
  return unless tasks.errored.any?

  update! errored_at: nil, rescued_at: Time.zone.now
end

#resume!Object



106
107
108
109
110
111
112
113
# File 'app/models/flow_core/instance.rb', line 106

def resume!
  return unless suspended?

  transaction do
    tasks.enabled.each(&:resume!)
    update! suspended_at: nil, resumed_at: Time.zone.now
  end
end

#suspend!Object



97
98
99
100
101
102
103
104
# File 'app/models/flow_core/instance.rb', line 97

def suspend!
  return if suspended?

  transaction do
    tasks.enabled.each(&:suspend!)
    update! suspended_at: Time.zone.now
  end
end

#suspended?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/flow_core/instance.rb', line 38

def suspended?
  suspended_at.present?
end