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
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/flow_core/instance.rb', line 67 def activate return false unless can_activate? with_transaction_returning_status do update! stage: :activated, activated_at: Time.zone.now workflow.on_instance_activate(self) tokens.create! place: workflow.start_place true end end |
#activate! ⇒ Object
112 113 114 |
# File 'app/models/flow_core/instance.rb', line 112 def activate! activate || raise(FlowCore::InvalidTransition, "Can't activate Instance##{id}") end |
#can_activate? ⇒ Boolean
44 45 46 |
# File 'app/models/flow_core/instance.rb', line 44 def can_activate? created? end |
#can_cancel? ⇒ Boolean
40 41 42 |
# File 'app/models/flow_core/instance.rb', line 40 def can_cancel? created? end |
#can_finish? ⇒ Boolean
48 49 50 |
# File 'app/models/flow_core/instance.rb', line 48 def can_finish? activated? end |
#can_terminate? ⇒ Boolean
52 53 54 |
# File 'app/models/flow_core/instance.rb', line 52 def can_terminate? true end |
#cancel ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/flow_core/instance.rb', line 56 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
108 109 110 |
# File 'app/models/flow_core/instance.rb', line 108 def cancel! cancel || raise(FlowCore::InvalidTransition, "Can't cancel Instance##{id}") end |
#errored? ⇒ Boolean
36 37 38 |
# File 'app/models/flow_core/instance.rb', line 36 def errored? tasks.where.not(errored_at: nil).exists? end |
#finish ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/flow_core/instance.rb', line 80 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
116 117 118 |
# File 'app/models/flow_core/instance.rb', line 116 def finish! finish || raise(FlowCore::InvalidTransition, "Can't finish Instance##{id}") end |
#terminate(reason:) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/flow_core/instance.rb', line 96 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
120 121 122 |
# File 'app/models/flow_core/instance.rb', line 120 def terminate!(reason:) terminate(reason: reason) || raise(FlowCore::InvalidTransition, "Can't terminate Instance##{id}") end |