Class: Deb::S3::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/deb/s3/lock.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLock

Returns a new instance of Lock.



10
11
12
13
# File 'lib/deb/s3/lock.rb', line 10

def initialize
  @user = nil
  @host = nil
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/deb/s3/lock.rb', line 8

def host
  @host
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'lib/deb/s3/lock.rb', line 7

def user
  @user
end

Class Method Details

.current(codename, component = nil, architecture = nil, cache_control = nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/deb/s3/lock.rb', line 44

def current(codename, component = nil, architecture = nil, cache_control = nil)
  lock_content = Deb::S3::Utils.s3_read(lock_path(codename, component, architecture, cache_control))
  lock_content = lock_content.split('@')
  lock = Deb::S3::Lock.new
  lock.user = lock_content[0]
  lock.host = lock_content[1] if lock_content.size > 1
  lock
end

.lock(codename, component = nil, architecture = nil, cache_control = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/deb/s3/lock.rb', line 29

def lock(codename, component = nil, architecture = nil, cache_control = nil)
  lockfile = Tempfile.new("lockfile")
  lockfile.write("#{Etc.getlogin}@#{Socket.gethostname}")
  lockfile.close

  Deb::S3::Utils.s3_store(lockfile.path,
                          lock_path(codename, component, architecture, cache_control),
                          "text/plain",
                          cache_control)
end

.locked?(codename, component = nil, architecture = nil, cache_control = nil) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/deb/s3/lock.rb', line 16

def locked?(codename, component = nil, architecture = nil, cache_control = nil)
  Deb::S3::Utils.s3_exists?(lock_path(codename, component, architecture, cache_control))
end

.unlock(codename, component = nil, architecture = nil, cache_control = nil) ⇒ Object



40
41
42
# File 'lib/deb/s3/lock.rb', line 40

def unlock(codename, component = nil, architecture = nil, cache_control = nil)
  Deb::S3::Utils.s3_remove(lock_path(codename, component, architecture, cache_control))
end

.wait_for_lock(codename, component = nil, architecture = nil, cache_control = nil, max_attempts = 60, wait = 10) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/deb/s3/lock.rb', line 20

def wait_for_lock(codename, component = nil, architecture = nil, cache_control = nil, max_attempts=60, wait=10)
  attempts = 0
  while self.locked?(codename, component, architecture, cache_control) do
    attempts += 1
    throw "Unable to obtain a lock after #{max_attempts}, giving up." if attempts > max_attempts
    sleep(wait)
  end
end