Class: FlowCore::Task
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- FlowCore::Task
- Defined in:
- app/models/flow_core/task.rb
Instance Method Summary collapse
- #can_enable? ⇒ Boolean
- #can_finish? ⇒ Boolean
- #can_terminate? ⇒ Boolean
- #create_output_token ⇒ Object
- #enable ⇒ Object
- #enable! ⇒ Object
- #error(error) ⇒ Object (also: #error!)
- #errored? ⇒ Boolean
- #finish ⇒ Object
- #finish! ⇒ Object
- #rescue ⇒ Object
- #rescue! ⇒ Object
- #resume ⇒ Object
- #resume! ⇒ Object
- #suspend ⇒ Object
- #suspend! ⇒ Object
- #suspended? ⇒ Boolean
- #terminate(reason:) ⇒ Object
- #terminate!(reason:) ⇒ Object
Instance Method Details
#can_enable? ⇒ Boolean
53 54 55 56 57 58 59 60 61 62 |
# File 'app/models/flow_core/task.rb', line 53 def can_enable? return false unless created? if input_free_tokens.size == transition.input_arcs.size return true end # Note: It's impossible of it create by a token and needs another token (AND join) to enable? same_origin_tasks.enabled.any? end |
#can_finish? ⇒ Boolean
64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/flow_core/task.rb', line 64 def can_finish? return false unless enabled? return false if errored? || suspended? if executable executable.finished? else true end end |
#can_terminate? ⇒ Boolean
76 77 78 |
# File 'app/models/flow_core/task.rb', line 76 def can_terminate? created? || enabled? end |
#create_output_token ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'app/models/flow_core/task.rb', line 174 def create_output_token return if output_token_created with_transaction_returning_status do transition.create_output_tokens_for(self) update! output_token_created: true true end end |
#enable ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/models/flow_core/task.rb', line 80 def enable return false unless can_enable? status = with_transaction_returning_status do input_free_tokens.each(&:lock!) update! stage: :enabled, enabled_at: Time.zone.now transition.on_task_enable(self) workflow.on_instance_task_enable(self) true end try_auto_finish if status status end |
#enable! ⇒ Object
185 186 187 |
# File 'app/models/flow_core/task.rb', line 185 def enable! enable || raise(FlowCore::InvalidTransition, "Can't enable Task##{id}") end |
#error(error) ⇒ Object Also known as: error!
132 133 134 135 136 137 138 139 |
# File 'app/models/flow_core/task.rb', line 132 def error(error) update! errored_at: Time.zone.now, error_reason: error. instance.update! errored_at: Time.zone.now transition.on_task_errored(self, error) workflow.on_instance_task_errored(self, error) true end |
#errored? ⇒ Boolean
45 46 47 |
# File 'app/models/flow_core/task.rb', line 45 def errored? errored_at.present? end |
#finish ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/flow_core/task.rb', line 99 def finish return false unless can_finish? with_transaction_returning_status do # terminate other racing tasks instance.tasks.enabled.where(created_by_token: created_by_token).find_each do |task| task.terminate! reason: "Same origin task #{id} finished" end input_locked_tokens.each { |token| token.consume! by: self } update! stage: :finished, finished_at: Time.zone.now transition.on_task_finish(self) workflow.on_instance_task_finish(self) true end create_output_token end |
#finish! ⇒ Object
189 190 191 |
# File 'app/models/flow_core/task.rb', line 189 def finish! finish || raise(FlowCore::InvalidTransition, "Can't finish Task##{id}") end |
#rescue ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'app/models/flow_core/task.rb', line 141 def rescue return unless errored? with_transaction_returning_status do update! errored_at: nil, rescued_at: Time.zone.now instance.update! errored_at: nil, rescued_at: Time.zone.now transition.on_task_rescue(self) workflow.on_instance_task_rescue(self) true end end |
#rescue! ⇒ Object
199 200 201 |
# File 'app/models/flow_core/task.rb', line 199 def rescue! self.rescue || raise(FlowCore::InvalidTransition, "Can't rescue Task##{id}") end |
#resume ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'app/models/flow_core/task.rb', line 164 def resume with_transaction_returning_status do update! suspended_at: nil, resumed_at: Time.zone.now transition.on_task_resume(self) workflow.on_instance_task_resume(self) true end end |
#resume! ⇒ Object
207 208 209 |
# File 'app/models/flow_core/task.rb', line 207 def resume! resume || raise(FlowCore::InvalidTransition, "Can't resume Task##{id}") end |
#suspend ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'app/models/flow_core/task.rb', line 154 def suspend with_transaction_returning_status do update! suspended_at: Time.zone.now transition.on_task_suspend(self) workflow.on_instance_task_suspend(self) true end end |
#suspend! ⇒ Object
203 204 205 |
# File 'app/models/flow_core/task.rb', line 203 def suspend! suspend || raise(FlowCore::InvalidTransition, "Can't suspend Task##{id}") end |
#suspended? ⇒ Boolean
49 50 51 |
# File 'app/models/flow_core/task.rb', line 49 def suspended? suspended_at.present? end |
#terminate(reason:) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/models/flow_core/task.rb', line 120 def terminate(reason:) return false unless can_terminate? with_transaction_returning_status do update! stage: :terminated, terminated_at: Time.zone.now, terminate_reason: reason transition.on_task_terminate(self) workflow.on_instance_task_terminate(self) true end end |
#terminate!(reason:) ⇒ Object
193 194 195 |
# File 'app/models/flow_core/task.rb', line 193 def terminate!(reason:) terminate(reason: reason) || raise(FlowCore::InvalidTransition, "Can't terminate Task##{id}") end |