Class: PagerDuty::ChefServer::SyncLock

Inherits:
Object
  • Object
show all
Defined in:
lib/pagerduty/chef_server/synclock.rb

Instance Method Summary collapse

Constructor Details

#initialize(lockfile, server_hostname, local_hostname, user, branch, force_lock = false) ⇒ SyncLock

Returns a new instance of SyncLock.



27
28
29
30
31
32
33
34
# File 'lib/pagerduty/chef_server/synclock.rb', line 27

def initialize(lockfile, server_hostname, local_hostname, user, branch, force_lock = false)
  @lockfile = lockfile
  @server_hostname = server_hostname.include?('.') ? server_hostname.split('.')[0] : server_hostname
  @local_hostname = local_hostname
  @user = user
  @branch = branch
  @opts = parse_opts({ announce: true, lock: true, force: force_lock })
end

Instance Method Details

#lock(time = Time.now) ⇒ Object

get lock



37
38
39
40
41
# File 'lib/pagerduty/chef_server/synclock.rb', line 37

def lock(time = Time.now)
  verify_lockable

  @f_lock = procure_lock if @opts[:lock] || @opts[:force]
end

#unlockObject

remove lock



44
45
46
47
48
49
50
51
# File 'lib/pagerduty/chef_server/synclock.rb', line 44

def unlock
  if @opts[:lock]
    Chef::Knife.ui.info 'removing lockfile'
    @f_lock.flock(File::LOCK_UN)
    @f_lock.close
    FileUtils.rm(@f_lock.path)
  end
end