Class: MotherBrain::LockManager

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Logging
Defined in:
lib/mb/lock_manager.rb

Overview

A registry of locks obtained against resources on a Chef Server

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

add_argument_header, dev, filename, #log_exception, logger, #logger, reset, set_logger, setup

Constructor Details

#initializeLockManager

Returns a new instance of LockManager.



21
22
23
24
# File 'lib/mb/lock_manager.rb', line 21

def initialize
  log.debug { "Lock Manager starting..." }
  @locks = Set.new
end

Instance Attribute Details

#locksSet<ChefMutex> (readonly)

Returns:



17
18
19
# File 'lib/mb/lock_manager.rb', line 17

def locks
  @locks
end

Class Method Details

.instanceCelluloid::Actor(LockManager)

Returns:

Raises:

  • (Celluloid::DeadActorError)

    if lock manager has not been started



8
9
10
# File 'lib/mb/lock_manager.rb', line 8

def instance
  MB::Application[:lock_manager] or raise Celluloid::DeadActorError, "lock manager not running"
end

Instance Method Details

#async_lock(environment) ⇒ MB::JobRecord

Asynchronously lock an environment

Parameters:

  • environment (String)

Returns:



63
64
65
66
67
# File 'lib/mb/lock_manager.rb', line 63

def async_lock(environment)
  job = Job.new(:lock)
  async(:lock, job, environment)
  job.ticket
end

#async_unlock(environment) ⇒ MB::JobRecord

Asynchronously unlock an environment

Parameters:

  • environment (String)

Returns:



89
90
91
92
93
# File 'lib/mb/lock_manager.rb', line 89

def async_unlock(environment)
  job = Job.new(:unlock)
  async(:unlock, job, environment)
  job.ticket
end

#find(options) ⇒ ChefMutex?

Find a lock of the given name in the list of registered locks

Returns:

See Also:



31
32
33
34
35
36
37
38
# File 'lib/mb/lock_manager.rb', line 31

def find(options)
  type, name = options.to_a.flatten

  locks.find { |mutex|
    mutex.type == type &&
    mutex.name == name
  }
end

#lock(job, environment) ⇒ Boolean

Lock an environment

Parameters:

  • job (MB::Job)
  • environment (String)

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
# File 'lib/mb/lock_manager.rb', line 75

def lock(job, environment)
  ChefMutex.new(
    chef_environment: environment,
    force: true,
    job: job,
    report_job_status: true
  ).lock
end

#register(mutex) ⇒ Object

Register the given lock

Parameters:



43
44
45
# File 'lib/mb/lock_manager.rb', line 43

def register(mutex)
  locks.add(mutex)
end

#resetObject



47
48
49
# File 'lib/mb/lock_manager.rb', line 47

def reset
  self.locks.clear
end

#unlock(job, environment) ⇒ Boolean

Unlock an environment

Parameters:

  • job (MB::Job)
  • environment (String)

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
# File 'lib/mb/lock_manager.rb', line 101

def unlock(job, environment)
  ChefMutex.new(
    chef_environment: environment,
    force: true,
    job: job,
    report_job_status: true
  ).unlock
end

#unregister(mutex) ⇒ Object

Unregister the given lock

Parameters:



54
55
56
# File 'lib/mb/lock_manager.rb', line 54

def unregister(mutex)
  locks.delete(mutex)
end