Class: SolidObjects::Executor
- Inherits:
-
Object
- Object
- SolidObjects::Executor
- Defined in:
- lib/solid_objects/executor.rb,
sig/generated/lib/solid_objects/executor.rbs
Instance Attribute Summary collapse
-
#activation ⇒ Object
readonly
Returns the value of attribute activation.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Instance Method Summary collapse
- #actor ⇒ Actor
- #call ⇒ Boolean
- #changed_observables(before, after) ⇒ Hash[String, untyped]
- #complete(result, observable_changes) ⇒ void
- #create_dead_letter(locked_message, error_details, now) ⇒ DeadLetter
- #enqueue_actor_messages(locked_message, instance, intents) ⇒ Array[Effect]
- #enqueue_broadcasts(locked_message, instance, observable_changes) ⇒ void
- #enqueue_effects(locked_message, instance, intents) ⇒ Array[Effect]
- #ensure_application_database_is_shared! ⇒ void
- #ensure_query_did_not_mutate_state!(state_before) ⇒ void
- #execute_commit_action(intent, handler, context) ⇒ Object
- #execute_commit_actions(intents) ⇒ void
- #fail_message(error) ⇒ void
-
#initialize(activation:, message:) ⇒ Executor
constructor
A new instance of Executor.
- #instrumentation_payload ⇒ Hash[Symbol, untyped]
- #invoke_actor(message_context) ⇒ Object
- #matching_claim! ⇒ ClaimedMessage
- #reject_message(rejection) ⇒ void
- #schedule_reminders(instance, intents) ⇒ void
- #serialized_error(error) ⇒ Hash[String, untyped]
Constructor Details
#initialize(activation:, message:) ⇒ Executor
Returns a new instance of Executor.
9 10 11 12 |
# File 'lib/solid_objects/executor.rb', line 9 def initialize(activation:, message:) @activation = activation @message = end |
Instance Attribute Details
#activation ⇒ Object (readonly)
Returns the value of attribute activation.
48 49 50 |
# File 'lib/solid_objects/executor.rb', line 48 def activation @activation end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
48 49 50 |
# File 'lib/solid_objects/executor.rb', line 48 def @message end |
Instance Method Details
#actor ⇒ Actor
51 52 53 |
# File 'lib/solid_objects/executor.rb', line 51 def actor activation.actor end |
#call ⇒ Boolean
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/solid_objects/executor.rb', line 15 def call state_before = actor.state.to_h observables_before = actor.observable_values = MessageContext.new( id: .id, attempt: .attempt_count, enqueued_at: .enqueued_at, idempotency_key: .idempotency_key, request_id: .request_id ) SolidObjects.instrument(:"message.started", **instrumentation_payload) result = invoke_actor() ensure_query_did_not_mutate_state!(state_before) observable_changes = changed_observables(observables_before, actor.observable_values) complete(result, observable_changes) true rescue LostActivation raise rescue Rejected => rejection activation.restore_state(state_before) if state_before actor.discard_intents (rejection) false rescue => error activation.restore_state(state_before) if state_before actor.discard_intents (error) false end |
#changed_observables(before, after) ⇒ Hash[String, untyped]
72 73 74 75 76 |
# File 'lib/solid_objects/executor.rb', line 72 def changed_observables(before, after) after.each_with_object({}) do |(name, value), changes| changes[name] = value unless before[name] == value end end |
#complete(result, observable_changes) ⇒ void
This method returns an undefined value.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/solid_objects/executor.rb', line 79 def complete(result, observable_changes) serialized_state = Serialization.dump( actor.state.to_h, max_bytes: SolidObjects.configuration.max_state_bytes ) serialized_result = Serialization.dump( (. == "sync") ? result : nil, max_bytes: SolidObjects.configuration.max_result_bytes ) effect_intents = actor.drain_effect_intents commit_action_intents = actor.drain_commit_action_intents reminder_intents = actor.drain_reminder_intents = actor. enqueued_effects = [] activation.lease.fenced_transaction do |instance| = matching_claim! = Message.lock.find(.id) execute_commit_actions(commit_action_intents) instance.update!( state: serialized_state, state_version: actor.class.state_version, last_used_at: SolidObjects.database_adapter.database_now ) .update!( result: serialized_result, error: nil, completed_at: SolidObjects.database_adapter.database_now ) enqueued_effects.concat(enqueue_effects(, instance, effect_intents)) schedule_reminders(instance, reminder_intents) enqueued_effects.concat( (, instance, ) ) enqueue_broadcasts(, instance, observable_changes) .destroy! end observable_changes.each_key do |observable_name| SolidObjects.instrument( :"broadcast.enqueued", **instrumentation_payload, observable_name: ) end enqueued_effects.each do |effect| SolidObjects.instrument( :"effect.enqueued", effect_id: effect.effect_id, effect_name: effect.name, message_id: effect., actor_type: .actor_type, actor_id: .actor_id ) end SolidObjects.instrument(:"message.completed", **instrumentation_payload) SolidObjects.wake_up.signal end |
#create_dead_letter(locked_message, error_details, now) ⇒ DeadLetter
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/solid_objects/executor.rb', line 361 def create_dead_letter(, error_details, now) DeadLetter.create!( message: , instance: .instance, actor_type: .actor_type, actor_id: .actor_id, message_name: ., arguments: .arguments, attempts: .attempt_count, exception_class: error_details.fetch("class"), exception_message: error_details.fetch("message"), backtrace: error_details.fetch("backtrace"), first_failed_at: .last_failed_at || now, last_failed_at: now ) end |
#enqueue_actor_messages(locked_message, instance, intents) ⇒ Array[Effect]
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/solid_objects/executor.rb', line 229 def (, instance, intents) intents.map do |intent| Effect.create!( message: , instance:, effect_id: SecureRandom.uuid, name: EffectExecutor::ACTOR_MESSAGE_EFFECT, arguments: { "actor_type" => intent.actor_type, "actor_id" => intent.actor_id, "message_name" => intent., "arguments" => intent.arguments, "available_at" => intent.available_at&.iso8601(6), "idempotency_key" => intent.idempotency_key }, status: "pending", max_attempts: SolidObjects.configuration.max_attempts, available_at: SolidObjects.database_adapter.database_now ) end end |
#enqueue_broadcasts(locked_message, instance, observable_changes) ⇒ void
This method returns an undefined value.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/solid_objects/executor.rb', line 252 def enqueue_broadcasts(, instance, observable_changes) observable_changes.each do |observable_name, value| Broadcast.create!( message: , instance:, broadcast_id: SecureRandom.uuid, observable_name:, value:, state_version: actor.class.state_version, activation_generation: activation.lease.generation, status: "pending", available_at: SolidObjects.database_adapter.database_now ) end end |
#enqueue_effects(locked_message, instance, intents) ⇒ Array[Effect]
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/solid_objects/executor.rb', line 191 def enqueue_effects(, instance, intents) intents.map do |intent| Effect.create!( message: , instance:, effect_id: SecureRandom.uuid, name: intent.name, arguments: intent.arguments, success_message_name: intent., failure_message_name: intent., status: "pending", max_attempts: SolidObjects.configuration.max_attempts, available_at: SolidObjects.database_adapter.database_now ) end end |
#ensure_application_database_is_shared! ⇒ void
This method returns an undefined value.
180 181 182 183 184 185 186 187 188 |
# File 'lib/solid_objects/executor.rb', line 180 def ensure_application_database_is_shared! return if SolidObjects::Record.connection_pool.equal?(ActiveRecord::Base.connection_pool) raise CommitActionUnavailable.new( actor_type: .actor_type, actor_id: .actor_id, message_name: . ) end |
#ensure_query_did_not_mutate_state!(state_before) ⇒ void
This method returns an undefined value.
63 64 65 66 67 68 69 |
# File 'lib/solid_objects/executor.rb', line 63 def ensure_query_did_not_mutate_state!(state_before) return unless . == "sync" return unless actor.class.definition.queries.key?(..to_sym) return if actor.state.to_h == state_before raise InvalidActor, "query #{..inspect} mutated actor state" end |
#execute_commit_action(intent, handler, context) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/solid_objects/executor.rb', line 156 def execute_commit_action(intent, handler, context) payload = { commit_action_name: intent.name, message_id: .id, request_id: .request_id, actor_type: .actor_type, actor_id: .actor_id, activation_generation: activation.lease.generation } SolidObjects.instrument(:"commit_action.started", **payload) handler.call(intent.arguments, context).tap do SolidObjects.instrument(:"commit_action.completed", **payload) end rescue => error SolidObjects.instrument( :"commit_action.failed", **payload, error_class: error.class.name, error_message: error. ) raise end |
#execute_commit_actions(intents) ⇒ void
This method returns an undefined value.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/solid_objects/executor.rb', line 139 def execute_commit_actions(intents) ensure_application_database_is_shared! if intents.any? context = CommitActionContext.new( message_id: .id, request_id: .request_id, actor_type: .actor_type, actor_id: .actor_id, activation_generation: activation.lease.generation ) intents.each do |intent| handler = SolidObjects.commit_action_registry.fetch(intent.name) execute_commit_action(intent, handler, context) end end |
#fail_message(error) ⇒ void
This method returns an undefined value.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/solid_objects/executor.rb', line 269 def (error) error_details = serialized_error(error) dead = false activation.lease.fenced_transaction do = matching_claim! = Message.lock.find(.id) now = SolidObjects.database_adapter.database_now .update!(error: error_details, last_failed_at: now) .destroy! if error.is_a?(NonRetryableError) || .attempt_count >= .max_attempts create_dead_letter(, error_details, now) dead = true else ReadyMessage.create!( message: , instance: .instance, sequence: .sequence, available_at: now + SolidObjects.configuration.retry_delay.call(.attempt_count) ) end end SolidObjects.instrument( :"message.failed", **instrumentation_payload, error_class: error.class.name, dead: ) SolidObjects.wake_up.signal end |
#instrumentation_payload ⇒ Hash[Symbol, untyped]
379 380 381 382 383 384 385 386 387 388 |
# File 'lib/solid_objects/executor.rb', line 379 def instrumentation_payload { message_id: .id, actor_type: .actor_type, actor_id: .actor_id, sequence: .sequence, attempt: .attempt_count, request_id: .request_id } end |
#invoke_actor(message_context) ⇒ Object
56 57 58 59 60 |
# File 'lib/solid_objects/executor.rb', line 56 def invoke_actor() Context.with(actor:, message: ) do actor.invoke(., .arguments) end end |
#matching_claim! ⇒ ClaimedMessage
338 339 340 341 342 343 344 345 346 347 |
# File 'lib/solid_objects/executor.rb', line 338 def matching_claim! ClaimedMessage.lock.find_by!( message_id: .id, process_id: activation.lease.owner_id, activation_token: activation.lease.activation_token, activation_generation: activation.lease.generation ) rescue ActiveRecord::RecordNotFound raise LostActivation, "message claim changed" end |
#reject_message(rejection) ⇒ void
This method returns an undefined value.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/solid_objects/executor.rb', line 304 def (rejection) rejection_data = Serialization.dump( { "code" => rejection.code, "message" => rejection..to_s.byteslice(0, 8_192), "details" => rejection.details }, max_bytes: SolidObjects.configuration.max_result_bytes ) activation.lease.fenced_transaction do |instance| = matching_claim! = Message.lock.find(.id) now = SolidObjects.database_adapter.database_now instance.update!(last_used_at: now) .update!( result: nil, rejection: rejection_data, error: nil, rejected_at: now, completed_at: now ) .destroy! end SolidObjects.instrument( :"message.rejected", **instrumentation_payload, code: rejection.code ) SolidObjects.wake_up.signal end |
#schedule_reminders(instance, intents) ⇒ void
This method returns an undefined value.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/solid_objects/executor.rb', line 209 def schedule_reminders(instance, intents) intents.each do |intent| reminder = Reminder.find_or_initialize_by(instance:, name: intent.name) reminder.assign_attributes( actor_type: instance.actor_type, actor_id: instance.actor_id, message_name: intent.name, arguments: intent.arguments, next_run_at: intent.at, interval_seconds: intent.interval_seconds, missed_policy: intent.missed_policy, status: "scheduled", claimed_by: nil, claimed_at: nil ) reminder.save! end end |
#serialized_error(error) ⇒ Hash[String, untyped]
350 351 352 353 354 355 356 357 358 |
# File 'lib/solid_objects/executor.rb', line 350 def serialized_error(error) Serialization.dump( { "class" => error.class.name, "message" => error..to_s.byteslice(0, 8_192), "backtrace" => Array(error.backtrace).first(50) } ) end |