From 36d6656c49e395206dae325baf3f69ff4ea7ed73 Mon Sep 17 00:00:00 2001 From: Krzysztof Jagiello Date: Wed, 20 Apr 2016 16:59:27 +0200 Subject: [PATCH] PID file support (#46) --- thunderpush/runner.py | 36 +++++++++++++++++++++++++++++++++--- thunderpush/settings.py | 3 +++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/thunderpush/runner.py b/thunderpush/runner.py index 4eaa20c..1f5652e 100644 --- a/thunderpush/runner.py +++ b/thunderpush/runner.py @@ -5,7 +5,9 @@ from sockjs.tornado import SockJSRouter +import os import sys +import fcntl import tornado.ioloop import argparse import logging @@ -13,6 +15,28 @@ logger = logging.getLogger() +class PIDFile(object): + def __init__(self, path): + self.path = path + + def __enter__(self): + self.pidfile = open(self.path, 'w+') + try: + fcntl.flock(self.pidfile, fcntl.LOCK_EX | fcntl.LOCK_NB) + self.pidfile.seek(0) + self.pidfile.truncate() + self.pidfile.write(str(os.getpid())) + self.pidfile.flush() + except IOError: + # PID file exists and is held by another process + msg = 'Process already running (PID file: {}).'.format(self.path) + raise SystemExit(msg) + + def __exit__(self, exc_type, exc_val, exc_tb): + self.pidfile.close() + os.remove(self.path) + + def run_app(): # configure logging level if settings.VERBOSE: @@ -43,10 +67,10 @@ def run_app(): def update_settings(args): args = vars(args) - for optname in ["PORT", "HOST", "VERBOSE", "DEBUG"]: + for optname in ["PORT", "HOST", "VERBOSE", "DEBUG", "PIDPATH"]: value = args.get(optname, None) - if not value is None: + if value is not None: setattr(settings, optname, value) settings.APIKEY = args['clientkey'] @@ -76,6 +100,11 @@ def parse_args(args): help='debug mode (useful for development)', action="store_true", dest="DEBUG") + parser.add_argument('--pid-path', + default=settings.PIDPATH, + help='path to the PID file', + action='store', type=str, dest='PIDPATH') + parser.add_argument('-V', '--version', action='version', version=__version__) @@ -91,7 +120,8 @@ def parse_args(args): def main(): args = parse_args(sys.argv[1:]) update_settings(args) - run_app() + with PIDFile(settings.PIDPATH): + run_app() if __name__ == "__main__": main() diff --git a/thunderpush/settings.py b/thunderpush/settings.py index 426e322..2818be7 100644 --- a/thunderpush/settings.py +++ b/thunderpush/settings.py @@ -23,3 +23,6 @@ # This sets the logging level to DEBUG. VERBOSE = False + +# Path to the PID file +PIDPATH = '/tmp/thunderpush.pid'