def main(argv): # setup logging setup_logging() # parse command line arguemnts parser = argparse.ArgumentParser() parser.add_argument("-v", "--version", action="version", version=__version__) parser.add_argument("-f", "--foreground", action="store_true", help="run in foreground / do not daemonize") parser.add_argument("-c", "--config", help="path to the configuration file", default="/etc/graphite_worker/worker.json") parser.add_argument("-u", "--user", help="run as specific user (default: nobody)", default="nobody") parser.add_argument("-g", "--group", help="run as specific group (default: nogroup)", default="nogroup") parser.add_argument("-p", "--pidfile", help="place for pidfile", default="/var/run/graphite_worker.pid") args = parser.parse_args(args=argv) # only root can run this program if os.getuid() != 0: raise SystemError("you need to be root to run this program") # try to read configuration try: global config config.load(args.config) except Exception as err: logger.error("unable to read config file (%s): %s" % (args.config, str(err))) sys.exit(1) # create worker instance worker_pool = GraphiteWorkerPool(config['pool_size'], user=args.user, group=args.group) # install signal handler install_signal_handler(worker_pool) # run in foreground if args.foreground: worker_pool.work() else: # daemonize with daemon.DaemonContext(pidfile=PidFile(args.pidfile)): worker_pool.work()
def main(): args = get_args() try: with PidFile(args.pid_file): init_event_server() except PidFileLockFailed as e: sys.stderr.write("Failed to get lock for pid file({0}): {1}".format( args.pid_file, e)) except KeyboardInterrupt: sys.exit(1)