Class: FlowCore::Instance
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- FlowCore::Instance
- 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 terminate_reason created_at updated_at ].freeze
Instance Method Summary collapse
- #activate ⇒ Object
- #activate! ⇒ Object
- #can_activate? ⇒ Boolean
- #can_cancel? ⇒ Boolean
- #can_finish? ⇒ Boolean
- #can_terminate? ⇒ Boolean
- #cancel ⇒ Object
- #cancel! ⇒ Object
- #errored? ⇒ Boolean
- #finish ⇒ Object
- #finish! ⇒ Object
- #terminate(reason:) ⇒ Object
- #terminate!(reason:) ⇒ Object
Instance Method Details
#activate ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/flow_core/instance.rb', line 65 def activate return false unless can_activate? with_transaction_returning_status do tokens.create! place: workflow.start_place update! stage: :activated, activated_at: Time.zone.now workflow.on_instance_activate(self) true end end |
#activate! ⇒ Object
109 110 111 |
# File 'app/models/flow_core/instance.rb', line 109 def activate! activate || raise(FlowCore::InvalidTransition, "Can't activate Instance##{id}") end |
#can_activate? ⇒ Boolean
42 43 44 |
# File 'app/models/flow_core/instance.rb', line 42 def can_activate? created? end |
#can_cancel? ⇒ Boolean
38 39 40 |
# File 'app/models/flow_core/instance.rb', line 38 def can_cancel? created? end |
#can_finish? ⇒ Boolean
46 47 48 |
# File 'app/models/flow_core/instance.rb', line 46 def can_finish? activated? end |
#can_terminate? ⇒ Boolean
50 51 52 |
# File 'app/models/flow_core/instance.rb', line 50 def can_terminate? true end |
#cancel ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/flow_core/instance.rb', line 54 def cancel return false unless can_cancel? with_transaction_returning_status do update! stage: :canceled, canceled_at: Time.zone.now workflow.on_instance_cancel(self) true end end |
#cancel! ⇒ Object
105 106 107 |
# File 'app/models/flow_core/instance.rb', line 105 def cancel! cancel || raise(FlowCore::InvalidTransition, "Can't cancel Instance##{id}") end |
#errored? ⇒ Boolean
34 35 36 |
# File 'app/models/flow_core/instance.rb', line 34 def errored? tasks.where.not(errored_at: nil).exists? end |
#finish ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/flow_core/instance.rb', line 77 def finish return false unless can_finish? with_transaction_returning_status 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!) workflow.on_instance_finish(self) true end end |
#finish! ⇒ Object
113 114 115 |
# File 'app/models/flow_core/instance.rb', line 113 def finish! finish || raise(FlowCore::InvalidTransition, "Can't finish Instance##{id}") end |
#terminate(reason:) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/flow_core/instance.rb', line 93 def terminate(reason:) return unless can_terminate? with_transaction_returning_status do tasks.enabled.each { |task| task.terminate! reason: "Instance terminated" } update! stage: :terminated, terminated_at: Time.zone.now, terminate_reason: reason workflow.on_instance_terminate(self) true end end |
#terminate!(reason:) ⇒ Object
117 118 119 |
# File 'app/models/flow_core/instance.rb', line 117 def terminate!(reason:) terminate(reason: reason) || raise(FlowCore::InvalidTransition, "Can't terminate Instance##{id}") end |