Class: UU::OS::Env::Lock

Inherits:
Object
  • Object
show all
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.

Examples:

Usage:

UU::OS::Env::Process.run('ues:SYS:SOME_UC', :main_entity_uri => 'ues:UNI:SOME_ARTIFACT') do
  ...
  lock = UU::OS::Env::Lock.create(resourceUri, createDto)
  begin
    ...
  ensure
    lock.unlock
  end
  ...
end

Defined Under Namespace

Classes: LockCreate

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Lock) create(resource_uri, params = nil)

Creates new lock.

Parameters:

  • resource_uri (String, UU::OS::UESURI)

    URI of resource to be locked

  • params (LockCreate) (defaults to: nil)

    DTO with additional lock parameters

Returns:

  • (Lock)

    Instance of lock or nil if lock was not created (e.g. resource is already locked)



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.

Parameters:

  • lock_timeout (Numeric) (defaults to: nil)

    New value of lock timeout in seconds (may be nil or -1 to maintain lock timeout initially set on lock create)



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).

Returns:

  • (Boolean)

    True if lock is valid, else false.



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