Method: Puppet::FileSystem::FileImpl#exclusive_open

Defined in:
lib/puppet/file_system/file_impl.rb

#exclusive_open(path, mode, options = 'r', timeout = 300, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puppet/file_system/file_impl.rb', line 52

def exclusive_open(path, mode, options = 'r', timeout = 300, &block)
  wait = 0.001 + (Kernel.rand / 1000)
  written = false
  until written
    ::File.open(path, options, mode) do |rf|
      if rf.flock(::File::LOCK_EX | ::File::LOCK_NB)
        Puppet.debug { _("Locked '%{path}'") % { path: path } }
        yield rf
        written = true
        Puppet.debug { _("Unlocked '%{path}'") % { path: path } }
      else
        Puppet.debug { "Failed to lock '%s' retrying in %.2f milliseconds" % [path, wait * 1000] }
        sleep wait
        timeout -= wait
        wait *= 2
        if timeout < 0
          raise Timeout::Error, _("Timeout waiting for exclusive lock on %{path}") % { path: path }
        end
      end
    end
  end
end