class Pidfile(object):
def __init__(self, pidfile, procname, uuid=None):
+ self.pidfile = pidfile
+ self.procname = procname
+ self.uuid = uuid
try:
self.fd = os.open(pidfile, os.O_CREAT | os.O_RDWR)
+ fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
- LOG.exception(_("Failed to open pidfile: %s"), pidfile)
+ LOG.exception(_("Error while handling pidfile: %s"), pidfile)
sys.exit(1)
- self.pidfile = pidfile
- self.procname = procname
- self.uuid = uuid
- if not not fcntl.flock(self.fd, fcntl.LOCK_EX):
- raise IOError(_('Unable to lock pid file'))
def __str__(self):
return self.pidfile
daemon.Pidfile('thefile', 'python')
self.os.open.assert_called_once_with('thefile', os.O_CREAT | os.O_RDWR)
- self.fcntl.flock.assert_called_once_with(FAKE_FD, self.fcntl.LOCK_EX)
+ self.fcntl.flock.assert_called_once_with(FAKE_FD, self.fcntl.LOCK_EX |
+ self.fcntl.LOCK_NB)
def test_init_open_fail(self):
self.os.open.side_effect = IOError
p = daemon.Pidfile('thefile', 'python')
p.unlock()
self.fcntl.flock.assert_has_calls([
- mock.call(FAKE_FD, self.fcntl.LOCK_EX),
+ mock.call(FAKE_FD, self.fcntl.LOCK_EX | self.fcntl.LOCK_NB),
mock.call(FAKE_FD, self.fcntl.LOCK_UN)]
)