Class: Rubyists::Dapr::Client::Lock

Inherits:
Object
  • Object
show all
Includes:
Rubyists::Dapr::Client, SemanticLogger::Loggable
Defined in:
lib/dapr/client/lock.rb

Overview

Handles publishing messages to Dapr pub/sub topics

Constant Summary collapse

LockRequest =

The proto class for the TryLock request message

::Dapr::Proto::Runtime::V1::TryLockRequest
UnlockRequest =

The proto class for the Unlock request message

::Dapr::Proto::Runtime::V1::UnlockRequest
DEFAULT_STORE_NAME =
'locker'

Constants included from Rubyists::Dapr::Client

DAPR_PORT, DAPR_STUB, DAPR_URI, Runtime

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rubyists::Dapr::Client

client, #client, singleton, #singleton

Constructor Details

#initialize(store_name, resource_id) ⇒ Lock

Initialize the Lock

Parameters:

  • store_name (String)

    The name of the Dapr lock store component to use

  • resource_id (String)

    The unique ID of the resource to lock



38
39
40
41
# File 'lib/dapr/client/lock.rb', line 38

def initialize(store_name, resource_id)
  @store_name = store_name
  @resource_id = resource_id
end

Instance Attribute Details

#lockObject (readonly)

The name of the pubsub component, the client, and the serialization to use



16
17
18
# File 'lib/dapr/client/lock.rb', line 16

def lock
  @lock
end

#resource_idObject (readonly)

The name of the pubsub component, the client, and the serialization to use



16
17
18
# File 'lib/dapr/client/lock.rb', line 16

def resource_id
  @resource_id
end

#store_nameObject (readonly)

The name of the pubsub component, the client, and the serialization to use



16
17
18
# File 'lib/dapr/client/lock.rb', line 16

def store_name
  @store_name
end

Class Method Details

.acquire(resource_id, store_name: DEFAULT_STORE_NAME, ttl: 10) ⇒ Lock

Returns The lock object.

Parameters:

  • resource_id (String)

    The unique ID of the resource to lock

  • store_name (String) (defaults to: DEFAULT_STORE_NAME)

    The name of the Dapr lock store component to use

  • ttl (Integer) (defaults to: 10)

    The time-to-live for the lock in seconds

Returns:

  • (Lock)

    The lock object



29
30
31
32
# File 'lib/dapr/client/lock.rb', line 29

def self.acquire(resource_id, store_name: DEFAULT_STORE_NAME, ttl: 10)
  lock = new(store_name, resource_id)
  lock.lock!(ttl:)
end

Instance Method Details

#lock!(ttl: 10) ⇒ Object

Parameters:

  • ttl (Integer) (defaults to: 10)

    The time-to-live for the lock in seconds



44
45
46
47
48
49
50
51
52
53
# File 'lib/dapr/client/lock.rb', line 44

def lock!(ttl: 10)
  response = singleton.try_lock_alpha1 lock_request
  if response.success
    logger.info('Acquired lock', store_name:, resource_id:, ttl:, lock_owner:)
    return self
  end

  logger.warn "Failed to acquire lock for #{resource_id}"
  nil
end

#unlock!Object



55
56
57
58
59
60
61
62
# File 'lib/dapr/client/lock.rb', line 55

def unlock!
  response = singleton.unlock_alpha1(UnlockRequest.new(store_name:, resource_id:, lock_owner:))
  status = response.status
  return true if status == :SUCCESS

  logger.warn('Unlock Failed!', status:, store_name:, resource_id:, lock_owner:)
  false
end