From 338c2135b4a19f62b0eb547a98a9bdd6ac46f86e Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Mon, 30 Sep 2013 10:27:00 -0700 Subject: [PATCH] Make pid file configurable And some other fixups around starting the daemon: * read config file before forking * add '-d' option to avoid forking * default pidfile to /var/run/elastic-recheck/elastic-recheck.pid * add pidfile option to config file * switch to python-daemon library (which is the version of the lib that the code was expecting anyway) * use expanduser in the query file path (to match the rest of the paths) Change-Id: I674778ef189cd216a80f74bd449cdc3b12b57a7d --- elastic_recheck/bot.py | 25 +++++++++++++++++-------- elastic_recheck/elasticRecheck.py | 2 ++ requirements.txt | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/elastic_recheck/bot.py b/elastic_recheck/bot.py index 4b24c3f2..5cbe2f66 100755 --- a/elastic_recheck/bot.py +++ b/elastic_recheck/bot.py @@ -42,6 +42,7 @@ import ConfigParser import daemon import irc.bot import logging +import logging.config import os import sys import threading @@ -167,9 +168,7 @@ class ChannelConfig(object): self.events[event] = event_set -def _main(): - config = ConfigParser.ConfigParser({'server_password': None}) - config.read(sys.argv[1]) +def _main(config): setup_logging(config) fp = config.get('ircbot', 'channel_config') @@ -199,14 +198,24 @@ def _main(): def main(): - if len(sys.argv) != 2: + if len(sys.argv) < 2: print "Usage: %s CONFIGFILE" % sys.argv[0] sys.exit(1) - pid = pid_file_module.TimeoutPIDLockFile( - "/tmp/recheckwatchbot.pid", 10) - with daemon.DaemonContext(pidfile=pid): - _main() + config = ConfigParser.ConfigParser({'server_password': None}) + config.read(sys.argv[1]) + + if config.has_option('ircbot', 'pidfile'): + pid_fn = os.path.expanduser(config.get('ircbot', 'pidfile')) + else: + pid_fn = '/var/run/elastic-recheck/elastic-recheck.pid' + + if '-d' in sys.argv: + _main(config) + else: + pid = pid_file_module.TimeoutPIDLockFile(pid_fn, 10) + with daemon.DaemonContext(pidfile=pid): + _main(config) def setup_logging(config): diff --git a/elastic_recheck/elasticRecheck.py b/elastic_recheck/elasticRecheck.py index d05290b8..ffbf4ed0 100755 --- a/elastic_recheck/elasticRecheck.py +++ b/elastic_recheck/elasticRecheck.py @@ -22,6 +22,7 @@ import urllib2 import ConfigParser import copy import logging +import os import sys import time import yaml @@ -268,6 +269,7 @@ def main(): user = config.get('gerrit', 'user', 'jogo') host = config.get('gerrit', 'host', 'review.openstack.org') queries = config.get('gerrit', 'query_file', 'queries.yaml') + queries = os.path.expanduser(queries) key = config.get('gerrit', 'key') classifier = Classifier(queries) stream = Stream(user, host, key) diff --git a/requirements.txt b/requirements.txt index 41b806ca..71da148c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ pyelasticsearch gerritlib -daemon +python-daemon irc pyyaml lockfile