3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-26 12:43:09 +01:00

launcher: fix PID files not being read if psutil isn't installed

This commit is contained in:
James Lu 2018-08-19 16:55:59 -07:00
parent 49badd1665
commit 874dfaf3f1

View File

@ -17,7 +17,6 @@ except ImportError:
args = {} args = {}
def _main(): def _main():
# FIXME: we can't pass logging on to conf until we set up the config...
conf.load_conf(args.config) conf.load_conf(args.config)
from pylinkirc.log import log from pylinkirc.log import log
@ -29,13 +28,17 @@ def _main():
has_pid = False has_pid = False
pid = None pid = None
if os.path.exists(pidfile): if os.path.exists(pidfile):
try:
with open(pidfile) as f:
pid = int(f.read())
except OSError:
log.exception("Could not read PID file %s:", pidfile)
else:
has_pid = True has_pid = True
if psutil is not None and os.name == 'posix': if psutil is not None and os.name == 'posix':
# FIXME: Haven't tested this on other platforms, so not turning it on by default. # FIXME: Haven't tested this on other platforms, so not turning it on by default.
with open(pidfile) as f:
try: try:
pid = int(f.read())
proc = psutil.Process(pid) proc = psutil.Process(pid)
except psutil.NoSuchProcess: # Process doesn't exist! except psutil.NoSuchProcess: # Process doesn't exist!
has_pid = False has_pid = False
@ -46,7 +49,7 @@ def _main():
log.info("Ignoring stale PID %s from PID file %r: process command line %r is not us", pid, pidfile, proc.cmdline()) log.info("Ignoring stale PID %s from PID file %r: process command line %r is not us", pid, pidfile, proc.cmdline())
has_pid = False has_pid = False
if has_pid: if pid and has_pid:
if args.rehash: if args.rehash:
os.kill(pid, signal.SIGUSR1) os.kill(pid, signal.SIGUSR1)
log.info('OK, rehashed PyLink instance %s (config %r)', pid, args.config) log.info('OK, rehashed PyLink instance %s (config %r)', pid, args.config)
@ -79,7 +82,7 @@ def _main():
world._should_remove_pid = True world._should_remove_pid = True
log.error('Cannot stop/rehash PyLink: no process with PID %s exists.', pid) log.error('Cannot stop/rehash PyLink: no process with PID %s exists.', pid)
else: else:
log.error('Cannot stop/rehash PyLink: PID file %r does not exist.', pidfile) log.error('Cannot stop/rehash PyLink: PID file %r does not exist or cannot be read.', pidfile)
sys.exit(1) sys.exit(1)
world._should_remove_pid = True world._should_remove_pid = True