Skip to content

Commit

Permalink
helper.unix fixes
Browse files Browse the repository at this point in the history
- Use the pidfile from the config file if specified
- Add a NullHandler to the LOGGER to avoid a warning message if no logger is configured
- Remove debug log file
  • Loading branch information
Gavin M. Roy committed Sep 25, 2013
1 parent d6bf1ee commit 64b20a4
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions helper/unix.py
Expand Up @@ -15,8 +15,9 @@
import traceback
import warnings


# Ignore the DeprecationWarning caused by os.popen3 in Python 2.6
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=DeprecationWarning)

LOGGER = logging.getLogger(__name__)

Expand All @@ -31,9 +32,14 @@ def __init__(self, controller):
to run as, a pid file, if core dumps should be prevented and a path to
write out exception logs to.
:param helper.Controller controller: The controller to daaemonize & run
:param controller: The controller to daaemonize & run
:type controller: helper.controller.Controller
"""
# The logger is reset by the time it gets here, fix to avoid warnings
from helper import NullHandler
LOGGER.addHandler(NullHandler())

self.controller = controller
self.pidfile_path = self._get_pidfile_path()

Expand Down Expand Up @@ -204,25 +210,22 @@ def _get_gid(self):
return None
return grp.getgrnam(self.controller.config.daemon.group).gr_gid

def _get_pidfile_path(self, pidfile=None):
def _get_pidfile_path(self):
"""Return the normalized path for the pidfile, raising an
exception if it can not written to.
:param str pidfile: The user specified pid file, if any
:return: str
:raises: ValueError
:raises: OSError
"""
if pidfile:
pidfile = path.abspath(pidfile)
if self.controller.config.daemon.pidfile:
pidfile = path.abspath(self.controller.config.daemon.pidfile)
if not os.access(path.dirname(pidfile), os.W_OK):
raise ValueError('Cannot write to specified pid file path'
' %s' % pidfile)
return pidfile

app = sys.argv[0].split('/')[-1]

for pidfile in ['%s/pids/%s.pid' % (os.getcwd(), app),
'/var/run/%s.pid' % app,
'/var/run/%s/%s.pid' % (app, app),
Expand All @@ -231,7 +234,6 @@ def _get_pidfile_path(self, pidfile=None):
'%s.pid' % app]:
if os.access(path.dirname(pidfile), os.W_OK):
return pidfile

raise OSError('Could not find an appropriate place for a pid file')

def _get_uid(self):
Expand All @@ -246,14 +248,14 @@ def _get_uid(self):

def _remove_pidfile(self):
"""Remove the pid file from the filesystem"""
with open('/tmp/dbug.log', 'a') as handle:
handle.write('Removing %s\n' % self.pidfile_path)
LOGGER.debug('Removing pidfile: %s', self.pidfile_path)
try:
os.unlink(self.pidfile_path)
except OSError:
pass

def _write_pidfile(self):
"""Write the pid file out with the process number in the pid file"""
LOGGER.debug('Writing pidfile: %s', self.pidfile_path)
with open(self.pidfile_path, "w") as handle:
handle.write(str(os.getpid()))

0 comments on commit 64b20a4

Please sign in to comment.