Class: Rubyists::Dapr::Client::Lock
- Inherits:
-
Object
- Object
- Rubyists::Dapr::Client::Lock
- 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
-
#lock ⇒ Object
readonly
The name of the pubsub component, the client, and the serialization to use.
-
#resource_id ⇒ Object
readonly
The name of the pubsub component, the client, and the serialization to use.
-
#store_name ⇒ Object
readonly
The name of the pubsub component, the client, and the serialization to use.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(store_name, resource_id) ⇒ Lock
constructor
Initialize the Lock.
- #lock!(ttl: 10) ⇒ Object
- #unlock! ⇒ Object
Methods included from Rubyists::Dapr::Client
client, #client, singleton, #singleton
Constructor Details
#initialize(store_name, resource_id) ⇒ Lock
Initialize the 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
#lock ⇒ Object (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_id ⇒ Object (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_name ⇒ Object (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.
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
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 |