Module: Bosh::Director::LockHelper

Overview

Helper for managing BOSH locks.

Instance Method Summary collapse

Instance Method Details

#with_compile_lock(package_id, stemcell_id, opts = {}) {|void| ... } ⇒ void

This method returns an undefined value.

Surround with compile lock.

Parameters:

  • package_id (String|Number)

    package id.

  • stemcell_id (String|Number)

    stemcell id.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • timeout (Number)

    how long to wait before giving up

Yields:

  • (void)

    block to surround



83
84
85
86
87
88
89
90
# File 'lib/bosh/director/lock_helper.rb', line 83

def with_compile_lock(package_id, stemcell_id, opts = {})
  timeout = opts[:timeout] || 15 * 60 # 15 minutes

  Config.logger.info("Acquiring compile lock on " +
                         "#{package_id} #{stemcell_id}")
  Lock.new("lock:compile:#{package_id}:#{stemcell_id}",
           :timeout => timeout).lock { yield }
end

#with_deployment_lock(deployment, opts = {}) {|void| ... } ⇒ void

This method returns an undefined value.

Surround with deployment lock.

Parameters:

  • deployment (DeploymentPlan|String)

    plan or name.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • timeout (Number)

    how long to wait before giving up

Yields:

  • (void)

    block to surround



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bosh/director/lock_helper.rb', line 11

def with_deployment_lock(deployment, opts = {})
  if deployment.respond_to?(:name)
    name = deployment.name
  elsif deployment.kind_of?(String)
    name = deployment
  else
    raise ArgumentError, "invalid deployment: #{deployment}"
  end
  timeout = opts[:timeout] || 10
  Config.logger.info("Acquiring deployment lock on #{name}")
  Lock.new("lock:deployment:#{name}", :timeout => timeout).lock { yield }
end

#with_release_lock(release, opts = {}) {|void| ... } ⇒ void

This method returns an undefined value.

Surround with release lock.

Parameters:

  • release (String)

    name.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • timeout (Number)

    how long to wait before giving up

Yields:

  • (void)

    block to surround



44
45
46
47
48
# File 'lib/bosh/director/lock_helper.rb', line 44

def with_release_lock(release, opts = {})
  timeout = opts[:timeout] || 10
  Config.logger.info("Acquiring release lock on #{release}")
  Lock.new("lock:release:#{release}", :timeout => timeout).lock { yield }
end

#with_release_locks(deployment_plan, opts = {}) {|void| ... } ⇒ void

This method returns an undefined value.

Surround with deployment releases lock.

Parameters:

  • deployment (DeploymentPlan)

    plan.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • timeout (Number)

    how long to wait before giving up

Yields:

  • (void)

    block to surround



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bosh/director/lock_helper.rb', line 56

def with_release_locks(deployment_plan, opts = {})
  timeout = opts[:timeout] || 10
  release_names = deployment_plan.releases.map do |release|
    release.name
  end

  # Sorting to enforce lock order to avoid deadlocks
  locks = release_names.sort.map do |release_name|
    Config.logger.info("Acquiring release lock: #{release_name}")
    Lock.new("lock:release:#{release_name}", :timeout => timeout)
  end

  begin
    locks.each { |lock| lock.lock }
    yield
  ensure
    locks.reverse_each { |lock| lock.release }
  end
end

#with_stemcell_lock(name, version, opts = {}) {|void| ... } ⇒ void

This method returns an undefined value.

Surround with stemcell lock.

Parameters:

  • name (String)

    stemcell name.

  • version (String)

    stemcell version.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • timeout (Number)

    how long to wait before giving up

Yields:

  • (void)

    block to surround



31
32
33
34
35
36
# File 'lib/bosh/director/lock_helper.rb', line 31

def with_stemcell_lock(name, version, opts = {})
  timeout = opts[:timeout] || 10
  Config.logger.info("Acquiring stemcell lock on #{name}:#{version}")
  Lock.new("lock:stemcells:#{name}:#{version}", :timeout => timeout).
      lock { yield }
end