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
64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/flow_core/instance.rb', line 64 def activate return false unless can_activate? with_transaction_returning_status do update! stage: :activated, activated_at: Time.zone.now tokens.create! place: workflow.start_place true end end |
#activate! ⇒ Object
106 107 108 |
# File 'app/models/flow_core/instance.rb', line 106 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 |
# 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 true end end |
#cancel! ⇒ Object
102 103 104 |
# File 'app/models/flow_core/instance.rb', line 102 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
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/flow_core/instance.rb', line 76 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!) true end end |
#finish! ⇒ Object
110 111 112 |
# File 'app/models/flow_core/instance.rb', line 110 def finish! finish || raise(FlowCore::InvalidTransition, "Can't finish Instance##{id}") end |
#terminate(reason:) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/flow_core/instance.rb', line 91 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 true end end |
#terminate!(reason:) ⇒ Object
114 115 116 |
# File 'app/models/flow_core/instance.rb', line 114 def terminate!(reason:) terminate(reason: reason) || raise(FlowCore::InvalidTransition, "Can't terminate Instance##{id}") end |