Class: SolidObjects::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_objects/executor.rb,
sig/generated/lib/solid_objects/executor.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(activation:, message:) ⇒ Executor

Returns a new instance of Executor.

RBS:

  • (activation: Activation, message: Message) -> void

Parameters:



9
10
11
12
# File 'lib/solid_objects/executor.rb', line 9

def initialize(activation:, message:)
  @activation = activation
  @message = message
end

Instance Attribute Details

#activationObject (readonly)

Returns the value of attribute activation.

Returns:

  • (Object)


45
46
47
# File 'lib/solid_objects/executor.rb', line 45

def activation
  @activation
end

#messageObject (readonly)

Returns the value of attribute message.

Returns:

  • (Object)


45
46
47
# File 'lib/solid_objects/executor.rb', line 45

def message
  @message
end

Instance Method Details

#actorActor

RBS:

  • () -> Actor

Returns:



48
49
50
# File 'lib/solid_objects/executor.rb', line 48

def actor
  activation.actor
end

#callBoolean

RBS:

  • () -> bool

Returns:

  • (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
# File 'lib/solid_objects/executor.rb', line 15

def call
  state_before = actor.state.to_h
  observables_before = actor.observable_values
  message_context = MessageContext.new(
    id: message.id,
    attempt: message.attempt_count,
    enqueued_at: message.enqueued_at,
    idempotency_key: message.idempotency_key,
    request_id: message.request_id
  )

  SolidObjects.instrument(:"message.started", **instrumentation_payload)
  result = Context.with(actor:, message: message_context) do
    actor.invoke(message.message_name, message.arguments)
  end
  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 => error
  activation.restore_state(state_before) if state_before
  actor.discard_intents
  fail_message(error)
  false
end

#changed_observables(before, after) ⇒ Hash[String, untyped]

RBS:

  • (Hash[String, untyped], Hash[String, untyped]) -> Hash[String, untyped]

Parameters:

  • (Hash[String, untyped])
  • (Hash[String, untyped])

Returns:

  • (Hash[String, untyped])


62
63
64
65
66
# File 'lib/solid_objects/executor.rb', line 62

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.

RBS:

  • (untyped, Hash[String, untyped]) -> void

Parameters:

  • (Object)
  • (Hash[String, untyped])


69
70
71
72
73
74
75
76
77
78
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
# File 'lib/solid_objects/executor.rb', line 69

def complete(result, observable_changes)
  serialized_state = Serialization.dump(
    actor.state.to_h,
    max_bytes: SolidObjects.configuration.max_state_bytes
  )
  serialized_result = Serialization.dump(
    (message.message_kind == "ask") ? result : nil,
    max_bytes: SolidObjects.configuration.max_result_bytes
  )
  effect_intents = actor.drain_effect_intents
  reminder_intents = actor.drain_reminder_intents
  outbound_message_intents = actor.drain_outbound_message_intents
  enqueued_effects = []

  activation.lease.fenced_transaction do |instance|
    claimed_message = matching_claim!
    locked_message = Message.lock.find(message.id)
    instance.update!(
      state: serialized_state,
      state_version: actor.class.state_version,
      last_used_at: SolidObjects.database_adapter.database_now
    )
    locked_message.update!(
      result: serialized_result,
      error: nil,
      completed_at: SolidObjects.database_adapter.database_now
    )
    enqueued_effects.concat(enqueue_effects(locked_message, instance, effect_intents))
    schedule_reminders(instance, reminder_intents)
    enqueued_effects.concat(
      enqueue_actor_messages(locked_message, instance, outbound_message_intents)
    )
    enqueue_broadcasts(locked_message, instance, observable_changes)
    claimed_message.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.message_id,
      actor_type: message.actor_type,
      actor_id: message.actor_id
    )
  end
  SolidObjects.instrument(:"message.completed", **instrumentation_payload)
  SolidObjects.wake_up.signal
end

#create_dead_letter(locked_message, error_details, now) ⇒ DeadLetter

RBS:

  • (Message, Hash[String, untyped], Time) -> DeadLetter

Parameters:

  • (Message)
  • (Hash[String, untyped])
  • (Time)

Returns:



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/solid_objects/executor.rb', line 261

def create_dead_letter(locked_message, error_details, now)
  DeadLetter.create!(
    message: locked_message,
    instance: locked_message.instance,
    actor_type: locked_message.actor_type,
    actor_id: locked_message.actor_id,
    message_name: locked_message.message_name,
    arguments: locked_message.arguments,
    attempts: locked_message.attempt_count,
    exception_class: error_details.fetch("class"),
    exception_message: error_details.fetch("message"),
    backtrace: error_details.fetch("backtrace"),
    first_failed_at: locked_message.last_failed_at || now,
    last_failed_at: now
  )
end

#enqueue_actor_messages(locked_message, instance, intents) ⇒ Array[Effect]

RBS:

  • (Message, Instance, Array[Actor::OutboundMessageIntent]) -> Array[Effect]

Parameters:

Returns:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/solid_objects/executor.rb', line 165

def enqueue_actor_messages(locked_message, instance, intents)
  intents.map do |intent|
    Effect.create!(
      message: locked_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.message_name,
        "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.

RBS:

  • (Message, Instance, Hash[String, untyped]) -> void

Parameters:



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/solid_objects/executor.rb', line 188

def enqueue_broadcasts(locked_message, instance, observable_changes)
  observable_changes.each do |observable_name, value|
    Broadcast.create!(
      message: locked_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]

RBS:

  • (Message, Instance, Array[Actor::EffectIntent]) -> Array[Effect]

Parameters:

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/solid_objects/executor.rb', line 127

def enqueue_effects(locked_message, instance, intents)
  intents.map do |intent|
    Effect.create!(
      message: locked_message,
      instance:,
      effect_id: SecureRandom.uuid,
      name: intent.name,
      arguments: intent.arguments,
      success_message_name: intent.success_message_name,
      failure_message_name: intent.failure_message_name,
      status: "pending",
      max_attempts: SolidObjects.configuration.max_attempts,
      available_at: SolidObjects.database_adapter.database_now
    )
  end
end

#ensure_query_did_not_mutate_state!(state_before) ⇒ void

This method returns an undefined value.

RBS:

  • (Hash[String, untyped]) -> void

Parameters:

  • (Hash[String, untyped])


53
54
55
56
57
58
59
# File 'lib/solid_objects/executor.rb', line 53

def ensure_query_did_not_mutate_state!(state_before)
  return unless message.message_kind == "ask"
  return unless actor.class.definition.queries.key?(message.message_name.to_sym)
  return if actor.state.to_h == state_before

  raise InvalidActor, "query #{message.message_name.inspect} mutated actor state"
end

#fail_message(error) ⇒ void

This method returns an undefined value.

RBS:

  • (Exception) -> void

Parameters:

  • (Exception)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/solid_objects/executor.rb', line 205

def fail_message(error)
  error_details = serialized_error(error)
  dead = false

  activation.lease.fenced_transaction do
    claimed_message = matching_claim!
    locked_message = Message.lock.find(message.id)
    now = SolidObjects.database_adapter.database_now
    locked_message.update!(error: error_details, last_failed_at: now)
    claimed_message.destroy!

    if locked_message.attempt_count >= locked_message.max_attempts
      create_dead_letter(locked_message, error_details, now)
      dead = true
    else
      ReadyMessage.create!(
        message: locked_message,
        instance: locked_message.instance,
        sequence: locked_message.sequence,
        available_at: now + SolidObjects.configuration.retry_delay.call(locked_message.attempt_count)
      )
    end
  end

  SolidObjects.instrument(
    :"message.failed",
    **instrumentation_payload,
    error_class: error.class.name,
    dead:
  )
  SolidObjects.wake_up.signal
end

#instrumentation_payloadHash[Symbol, untyped]

RBS:

  • () -> Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


279
280
281
282
283
284
285
286
287
288
# File 'lib/solid_objects/executor.rb', line 279

def instrumentation_payload
  {
    message_id: message.id,
    actor_type: message.actor_type,
    actor_id: message.actor_id,
    sequence: message.sequence,
    attempt: message.attempt_count,
    request_id: message.request_id
  }
end

#matching_claim!ClaimedMessage

RBS:

  • () -> ClaimedMessage

Returns:



239
240
241
242
243
244
245
246
247
# File 'lib/solid_objects/executor.rb', line 239

def matching_claim!
  ClaimedMessage.lock.find_by!(
    message_id: message.id,
    process_id: activation.lease.owner_id,
    activation_generation: activation.lease.generation
  )
rescue ActiveRecord::RecordNotFound
  raise LostActivation, "message claim changed"
end

#schedule_reminders(instance, intents) ⇒ void

This method returns an undefined value.

RBS:

  • (Instance, Array[Actor::ReminderIntent]) -> void

Parameters:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/solid_objects/executor.rb', line 145

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]

RBS:

  • (Exception) -> Hash[String, untyped]

Parameters:

  • (Exception)

Returns:

  • (Hash[String, untyped])


250
251
252
253
254
255
256
257
258
# File 'lib/solid_objects/executor.rb', line 250

def serialized_error(error)
  Serialization.dump(
    {
      "class" => error.class.name,
      "message" => error.message.to_s.byteslice(0, 8_192),
      "backtrace" => Array(error.backtrace).first(50)
    }
  )
end