Class: CommandTower::Messaging::Execution::Claim

Inherits:
Object
  • Object
show all
Defined in:
app/services/command_tower/messaging/execution/claim.rb

Overview

Optimistic CAS: queued → executing. Returns true only when this worker wins.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delivery:) ⇒ Claim

Returns a new instance of Claim.



12
13
14
# File 'app/services/command_tower/messaging/execution/claim.rb', line 12

def initialize(delivery:)
  @delivery = delivery
end

Class Method Details

.call(delivery:) ⇒ Object



8
9
10
# File 'app/services/command_tower/messaging/execution/claim.rb', line 8

def self.call(delivery:)
  new(delivery:).call
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/command_tower/messaging/execution/claim.rb', line 16

def call
  now = Time.current
  updated = Messaging::ChannelDelivery.where(
    id: @delivery.id,
    status: Messaging::ChannelDelivery::STATUS_QUEUED,
  ).update_all(
    [
      "status = ?, execution_claimed_at = ?, execution_attempt_count = execution_attempt_count + 1, updated_at = ?",
      Messaging::ChannelDelivery::STATUS_EXECUTING,
      now,
      now,
    ],
  )

  updated == 1
end