Skip to content

Commit

Permalink
bugfix:
Browse files Browse the repository at this point in the history
if daemon got down hard, and was not able to delete its pidfile, the new daemon append its pidfile. now the pid file is cleared before writing the new pid into it.
  • Loading branch information
schlitzered committed Oct 23, 2014
1 parent b615621 commit 91c20bd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
11 changes: 7 additions & 4 deletions pep3143daemon/pidfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ def acquire(self):
:raise: SystemExit
"""
try:
self.pidfile = open(self._pidfile, "a")
pidfile = open(self._pidfile, "a")
except IOError as err:
raise SystemExit(err)
try:
fcntl.flock(self.pidfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
fcntl.flock(pidfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
raise SystemExit('Already running according to ' + self._pidfile)
self.pidfile.write(str(os.getpid())+'\n')
self.pidfile.flush()
pidfile.seek(0)
pidfile.truncate()
pidfile.write(str(os.getpid())+'\n')
pidfile.flush()
self.pidfile = pidfile
atexit.register(self.release)

def release(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='pep3143daemon',
version='0.0.2',
version='0.0.3',
description='Implementation of PEP 3143, a unix daemon',
long_description=pep3143daemon.__doc__,
packages=['pep3143daemon'],
Expand Down
5 changes: 2 additions & 3 deletions test/unit/test_PidFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def test___init__(self):
self.assertIsNone(self.mockpidfile.pidfile)
self.assertEqual(self.mockpidfile._pidfile, 'test.pid')


def test_acquire(self):
self.mockpidfile._pidfile = 'test.pid'
pep3143daemon.pidfile.PidFile.acquire(self.mockpidfile)
Expand All @@ -48,6 +47,8 @@ def test_acquire(self):
call()
]
)
self.mockpidfile.pidfile.seek.assert_called_with(0)
self.mockpidfile.pidfile.truncate.assert_called_with()
self.mockpidfile.pidfile.write.assert_called_with(str(self.os_mock.getpid()) + '\n')
self.mockpidfile.pidfile.flush.assert_called_with()

Expand All @@ -66,7 +67,5 @@ def test_acquire_open_fail(self):
def test_release(self):
self.mockpidfile._pidfile = 'test.pid'
pep3143daemon.pidfile.PidFile.release(self.mockpidfile)

self.mockpidfile.pidfile.close.assert_called_with()

self.os_mock.remove.assert_called_with(self.mockpidfile._pidfile)

0 comments on commit 91c20bd

Please sign in to comment.