Class: UU::OS::Env::Lock
- Inherits:
-
Object
- Object
- UU::OS::Env::Lock
- Defined in:
- uu_os_framework-0.29.16/lib/uu/os/env/lock.rb,
uu_os_framework-0.29.16/lib/uu/os/env/lock/lock_create.rb
Overview
Lock component. Lock must be used inside running process.
Defined Under Namespace
Classes: LockCreate
Class Method Summary (collapse)
-
+ (Lock) create(resource_uri, params = nil)
Creates new lock.
Instance Method Summary (collapse)
-
- (Object) reallocate(lock_timeout = nil)
Reallocates lock and thus allows prolonging (or shortening) its expiration time.
-
- (Object) unlock
Unlocks lock.
-
- (Boolean) valid?
Checks if lock is valid (e.g. is not expired or was not removed by other process).
Class Method Details
+ (Lock) create(resource_uri, params = nil)
Creates new lock.
60 61 62 63 64 65 66 67 68 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 |
# File 'uu_os_framework-0.29.16/lib/uu/os/env/lock.rb', line 60 def self.create(resource_uri, params = nil) process_uri = UU::OS::Env::Process.active_process_uri if !process_uri raise 'Cannot create lock because there is no active process for current context. Either make sure code is executed inside managed environment or use appropriate API to initialize new process.' end dto = Lock::LockCreate.new(params) dto.send(:attributes)[:ownerId] = owner_id dto.send(:attributes)[:processUri] = process_uri dto.wait_timeout = @@DEFAULT_WAIT_TIMEOUT if !dto.wait_timeout dto.wait_timeout = @@MAXIMUM_WAIT_TIMEOUT if dto.wait_timeout > @@MAXIMUM_WAIT_TIMEOUT payload = dto.to_json svc = UU::OS::REST::RemoteClient.new(self, @@SVC_PATH) remaining_time = dto.wait_timeout wait = 0.5 lock_uri = nil while true lock_uri = svc.post(:create, resource_uri, payload) break if ((lock_uri) && (lock_uri != @@NULL_URI_VALUE)) || (remaining_time <= 0) wait *= 2 if remaining_time > wait remaining_time -= wait else wait = remaining_time remaining_time = 0 end sleep(wait.to_i) end if (lock_uri) && (lock_uri != @@NULL_URI_VALUE) lock_attr = svc.get(:getAttributes, UU::OS::UESURI.new(lock_uri)) if lock_attr return self.new(lock_attr) else raise "The created lock with URI #{lock_uri} was tampered by concurrent process. Resource #{resource_uri} cannot be reliably locked." end elsif dto.wait_timeout > 0 raise UU::OS::Env::LockCreationTimeoutException.new("The lock for resource #{resource_uri} was not created in given time of #{dto.wait_timeout} seconds.") else return nil end end |
Instance Method Details
- (Object) reallocate(lock_timeout = nil)
Reallocates lock and thus allows prolonging (or shortening) its expiration time. In case lock was already removed (e.g. is already expired, or was removed by other process), exception is thrown.
140 141 142 143 144 145 146 |
# File 'uu_os_framework-0.29.16/lib/uu/os/env/lock.rb', line 140 def reallocate(lock_timeout = nil) return if !@lock_uri svc = UU::OS::REST::RemoteClient.new(self.class, @@SVC_PATH) svc.add_parameter(:ownerId, self.class.send(:owner_id)) svc.add_parameter(:lockTimeout, lock_timeout.to_i) if lock_timeout svc.post(:reallocate, @lock_uri) end |
- (Object) unlock
Unlocks lock. In case lock is not valid in time of its unlock exception is thrown.
150 151 152 153 154 155 156 |
# File 'uu_os_framework-0.29.16/lib/uu/os/env/lock.rb', line 150 def unlock return if !@lock_uri svc = UU::OS::REST::RemoteClient.new(self.class, @@SVC_PATH) svc.add_parameter(:ownerId, self.class.send(:owner_id)) svc.delete(:unlock, @lock_uri) @lock_uri = nil end |
- (Boolean) valid?
Checks if lock is valid (e.g. is not expired or was not removed by other process).
126 127 128 129 130 131 132 |
# File 'uu_os_framework-0.29.16/lib/uu/os/env/lock.rb', line 126 def valid? return false if !@lock_uri svc = UU::OS::REST::RemoteClient.new(self.class, @@SVC_PATH) svc.add_parameter(:ownerId, self.class.send(:owner_id)) valid = svc.get(:isValid, @lock_uri) return valid == @@TRUE_VALUE end |