Exemple #1
0
def main():
    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level',
                        dest='loglevel',
                        default='info',
                        help="log level")
    parser.add_argument('--log-output',
                        dest='logoutput',
                        default='-',
                        help="log output")
    parser.add_argument('--daemon',
                        dest='daemonize',
                        action='store_true',
                        help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')

    args = parser.parse_args()

    if args.daemonize:
        daemonize()

    pidfile = None
    if args.pidfile:
        pidfile = Pidfile(args.pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError, e:
            print(str(e))
            sys.exit(1)
Exemple #2
0
def main():
    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level', dest='loglevel', default='info',
            help="log level")
    parser.add_argument('--log-output', dest='logoutput', default='-',
            help="log output")
    parser.add_argument('--daemon', dest='daemonize', action='store_true',
            help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')

    args = parser.parse_args()
    cfg, cfg_files_read = util.read_config(args.config)

    if args.daemonize:
        daemonize()

    pidfile = None
    if args.pidfile:
        pidfile = Pidfile(args.pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError, e:
            print(str(e))
            sys.exit(1)
Exemple #3
0
def main():
    parser = argparse.ArgumentParser(description="Run some watchers.")
    parser.add_argument("config", help="configuration file", nargs="?")

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument("--log-level", dest="loglevel", default="info", choices=LOG_LEVELS.keys(), help="log level")
    parser.add_argument("--log-output", dest="logoutput", default="-", help="log output")
    parser.add_argument("--daemon", dest="daemonize", action="store_true", help="Start circusd in the background")
    parser.add_argument("--pidfile", dest="pidfile")
    parser.add_argument("--version", action="store_true", default=False, help="Displays Circus version and exits.")

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    pidfile = None
    if args.pidfile:
        pidfile = Pidfile(args.pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError, e:
            print(str(e))
            sys.exit(1)
Exemple #4
0
def main():
    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file', nargs='?')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level',
                        dest='loglevel',
                        choices=LOG_LEVELS.keys() +
                        [key.upper() for key in LOG_LEVELS.keys()],
                        help="log level")
    parser.add_argument(
        '--log-output',
        dest='logoutput',
        help=(
            "The location where the logs will be written. The default behavior "
            "is to write to stdout (you can force it by passing '-' to "
            "this option). Takes a filename otherwise."))

    parser.add_argument('--daemon',
                        dest='daemonize',
                        action='store_true',
                        help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')
    parser.add_argument('--version',
                        action='store_true',
                        default=False,
                        help='Displays Circus version and exits.')

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    # basic logging configuration
    logging.basicConfig()

    # From here it can also come from the arbiter configuration
    # load the arbiter from config
    arbiter = Arbiter.load_from_config(args.config)

    pidfile = args.pidfile or arbiter.pidfile or None
    if pidfile:
        pidfile = Pidfile(pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError, e:
            print(str(e))
            sys.exit(1)
Exemple #5
0
def main():
    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file', nargs='?')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level', dest='loglevel',
                        choices=LOG_LEVELS.keys() + [key.upper() for key in
                                                     LOG_LEVELS.keys()],
                        help="log level")
    parser.add_argument('--log-output', dest='logoutput', help=(
        "The location where the logs will be written. The default behavior "
        "is to write to stdout (you can force it by passing '-' to "
        "this option). Takes a filename otherwise."))

    parser.add_argument('--daemon', dest='daemonize', action='store_true',
                        help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')
    parser.add_argument('--version', action='store_true', default=False,
                        help='Displays Circus version and exits.')

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    # basic logging configuration
    logging.basicConfig()

    # From here it can also come from the arbiter configuration
    # load the arbiter from config
    arbiter = Arbiter.load_from_config(args.config)

    pidfile = args.pidfile or arbiter.pidfile or None
    if pidfile:
        pidfile = Pidfile(pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError, e:
            print(str(e))
            sys.exit(1)
Exemple #6
0
def main():
    parser = argparse.ArgumentParser(description="Run some watchers.")
    parser.add_argument("config", help="configuration file", nargs="?")

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument(
        "--log-level",
        dest="loglevel",
        default="info",
        choices=LOG_LEVELS.keys() + [key.upper() for key in LOG_LEVELS.keys()],
        help="log level",
    )
    parser.add_argument(
        "--log-output",
        dest="logoutput",
        default="-",
        help=(
            "The location where the logs will be written. The default behavior "
            "is to write to stdout (you can force it by passing '-' to "
            "this option). Takes a filename otherwise."
        ),
    )

    parser.add_argument("--daemon", dest="daemonize", action="store_true", help="Start circusd in the background")
    parser.add_argument("--pidfile", dest="pidfile")
    parser.add_argument("--version", action="store_true", default=False, help="Displays Circus version and exits.")

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    pidfile = None
    if args.pidfile:
        pidfile = Pidfile(args.pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError, e:
            print(str(e))
            sys.exit(1)
Exemple #7
0
def main():
    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file', nargs='?')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level',
                        dest='loglevel',
                        default='info',
                        choices=LOG_LEVELS.keys() +
                        [key.upper() for key in LOG_LEVELS.keys()],
                        help="log level")
    parser.add_argument('--log-output',
                        dest='logoutput',
                        default='-',
                        help="log output")
    parser.add_argument('--daemon',
                        dest='daemonize',
                        action='store_true',
                        help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')
    parser.add_argument('--version',
                        action='store_true',
                        default=False,
                        help='Displays Circus version and exits.')

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    pidfile = None
    if args.pidfile:
        pidfile = Pidfile(args.pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError, e:
            print(str(e))
            sys.exit(1)
Exemple #8
0
    def test_pidfile(self):
        proc = subprocess.Popen('sleep 120', shell=True)
        fd, path = tempfile.mkstemp()
        os.close(fd)

        try:
            pidfile = Pidfile(path)

            pidfile.create(proc.pid)
            self.assertRaises(RuntimeError, pidfile.create, proc.pid)
            pidfile.unlink()
            pidfile.create(proc.pid)
            pidfile.rename(path + '.2')
            self.assertTrue(os.path.exists(path + '.2'))
            self.assertFalse(os.path.exists(path))
        finally:
            os.remove(path + '.2')
Exemple #9
0
    def test_pidfile(self):
        cmd = shlex.split(SLEEP % 120, posix=not IS_WINDOWS)
        proc = subprocess.Popen(cmd, shell=True)
        fd, path = tempfile.mkstemp()
        os.close(fd)

        try:
            pidfile = Pidfile(path)

            pidfile.create(proc.pid)
            self.assertRaises(RuntimeError, pidfile.create, proc.pid)
            pidfile.unlink()
            pidfile.create(proc.pid)
            pidfile.rename(path + '.2')
            self.assertTrue(os.path.exists(path + '.2'))
            self.assertFalse(os.path.exists(path))
        finally:
            os.remove(path + '.2')
Exemple #10
0
    def test_pidfile_data(self):
        proc = subprocess.Popen(SLEEP % 120, shell=True)
        fd, path = tempfile.mkstemp()

        os.write(fd, "fail-to-validate\n".encode('utf-8'))
        os.close(fd)

        try:
            pidfile = Pidfile(path)

            pidfile.create(proc.pid)
            self.assertTrue(os.path.exists(path))
            pid = 0
            with open(path, "r") as f:
                pid = int(f.read() or 0)
            self.assertEqual(pid, proc.pid)
        finally:
            os.remove(path)

        fd, path = tempfile.mkstemp()

        proc2 = subprocess.Popen(SLEEP % 0, shell=True)

        os.write(fd, "{0}\n".format(proc2.pid).encode('utf-8'))
        os.close(fd)

        proc2.wait()

        try:
            pidfile = Pidfile(path)

            self.assertNotEqual(proc2.pid, proc.pid)
            pidfile.create(proc.pid)
        finally:
            os.remove(path)

        fd, path = tempfile.mkstemp()

        os.write(fd, "fail-to-int\n".encode('utf-8'))
        os.close(fd)

        try:
            pidfile = Pidfile(path)

            pidfile.unlink()
            self.assertFalse(os.path.exists(path))
        except Exception as e:
            self.fail(str(e))
Exemple #11
0
    def test_pidfile(self):
        cmd = shlex.split(SLEEP % 120, posix=not IS_WINDOWS)
        proc = subprocess.Popen(cmd, shell=True)
        fd, path = tempfile.mkstemp()
        os.close(fd)

        try:
            pidfile = Pidfile(path)

            pidfile.create(proc.pid)
            self.assertRaises(RuntimeError, pidfile.create, proc.pid)
            pidfile.unlink()
            pidfile.create(proc.pid)
            pidfile.rename(path + ".2")
            self.assertTrue(os.path.exists(path + ".2"))
            self.assertFalse(os.path.exists(path))
        finally:
            os.remove(path + ".2")
Exemple #12
0
def main():
    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file', nargs='?')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level', dest='loglevel', default='info',
                        choices=LOG_LEVELS.keys() + [key.upper() for key in
                                                     LOG_LEVELS.keys()],
                        help="log level")
    parser.add_argument('--log-output', dest='logoutput', default='-',
                        help="log output")
    parser.add_argument('--daemon', dest='daemonize', action='store_true',
                        help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')
    parser.add_argument('--version', action='store_true', default=False,
                        help='Displays Circus version and exits.')

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    pidfile = None
    if args.pidfile:
        pidfile = Pidfile(args.pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError, e:
            print(str(e))
            sys.exit(1)
Exemple #13
0
    def test_pidfile(self):
        proc = subprocess.Popen(SLEEP % 120, shell=True)
        fd, path = tempfile.mkstemp()
        os.close(fd)

        rf = path + '.2'
        try:
            pidfile = Pidfile(path)

            pidfile.create(proc.pid)
            mode = os.stat(path).st_mode
            self.assertEqual(stat.S_IMODE(mode), pidfile.perm_mode, path)
            pidfile.unlink()
            self.assertFalse(os.path.exists(path))
            pidfile.create(proc.pid)
            pidfile.rename(rf)
            self.assertTrue(os.path.exists(rf))
            self.assertFalse(os.path.exists(path))
            mode = os.stat(rf).st_mode
            self.assertEqual(stat.S_IMODE(mode), pidfile.perm_mode, rf)
        finally:
            os.remove(rf)
Exemple #14
0
    def test_pidfile(self):
        proc = subprocess.Popen(SLEEP % 120, shell=True)
        fd, path = tempfile.mkstemp()
        os.close(fd)

        rf = path + '.2'
        try:
            pidfile = Pidfile(path)

            pidfile.create(proc.pid)
            mode = os.stat(path).st_mode
            self.assertEqual(stat.S_IMODE(mode), pidfile.perm_mode, path)
            pidfile.unlink()
            self.assertFalse(os.path.exists(path))
            pidfile.create(proc.pid)
            pidfile.rename(rf)
            self.assertTrue(os.path.exists(rf))
            self.assertFalse(os.path.exists(path))
            mode = os.stat(rf).st_mode
            self.assertEqual(stat.S_IMODE(mode), pidfile.perm_mode, rf)
        finally:
            os.remove(rf)
Exemple #15
0
def main(profile, loggerconfig, foreground, pidfile):
    """Run an aiida daemon"""
    import zmq
    try:
        zmq_version = [int(part) for part in zmq.__version__.split('.')[:2]]
        if len(zmq_version) < 2:
            raise ValueError()
    except (AttributeError, ValueError):
        print('Unknown PyZQM version - aborting...')
        sys.exit(0)

    if zmq_version[0] < 13 or (zmq_version[0] == 13 and zmq_version[1] < 1):
        print('circusd needs PyZMQ >= 13.1.0 to run - aborting...')
        sys.exit(0)

    loglevel = 'INFO'
    logoutput = '-'

    if not foreground:
        logoutput = 'balrog-{}.log'.format(profile)
        daemonize()

    # Create the arbiter
    profile_config = ProfileConfig(profile)

    arbiter = get_arbiter(
        controller=profile_config.get_endpoint(0),
        pubsub_endpoint=profile_config.get_endpoint(1),
        stats_endpoint=profile_config.get_endpoint(2),
        logoutput=logoutput,
        loglevel=loglevel,
        debug=False,
        statsd=True,
        pidfile='balrog-{}.pid'.format(
            profile
        ),  # aiida.common.setup.AIIDA_CONFIG_FOLDER + '/daemon/aiida-{}.pid'.format(uuid)
        watchers=[{
            'name': profile_config.daemon_name,
            'cmd': profile_config.cmd_string,
            'virtualenv': VIRTUALENV,
            'copy_env': True,
            'stdout_stream': {
                'class': 'FileStream',
                'filename': '{}.log'.format(profile_config.daemon_name)
            },
            'env': {
                'PYTHONUNBUFFERED': 'True'
            }
        }])

    # go ahead and set umask early if it is in the config
    if arbiter.umask is not None:
        os.umask(arbiter.umask)

    pidfile = pidfile or arbiter.pidfile or None
    if pidfile:
        pidfile = Pidfile(pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError as e:
            print(str(e))
            sys.exit(1)

    # configure the logger
    loglevel = loglevel or arbiter.loglevel or 'info'
    logoutput = logoutput or arbiter.logoutput or '-'
    loggerconfig = loggerconfig or arbiter.loggerconfig or None
    configure_logger(logger, loglevel, logoutput, loggerconfig)

    # Main loop
    restart = True
    while restart:
        try:
            arbiter = arbiter
            future = arbiter.start()
            restart = False
            if check_future_exception_and_log(future) is None:
                restart = arbiter._restarting
        except Exception as e:
            # emergency stop
            arbiter.loop.run_sync(arbiter._emergency_stop)
            raise (e)
        except KeyboardInterrupt:
            pass
        finally:
            arbiter = None
            if pidfile is not None:
                pidfile.unlink()
    sys.exit(0)
Exemple #16
0
def main():
    import zmq

    try:
        zmq_version = [int(part) for part in zmq.__version__.split(".")]
        if len(zmq_version) < 2:
            raise ValueError()
    except (AttributeError, ValueError):
        print("Unknown PyZQM version - aborting...")
        sys.exit(0)

    if zmq_version[0] < 13 or (zmq_version[0] == 13 and zmq_version[1] < 1):
        print("circusd needs PyZMQ >= 13.1.0 to run - aborting...")
        sys.exit(0)

    parser = argparse.ArgumentParser(description="Run some watchers.")
    parser.add_argument("config", help="configuration file", nargs="?")

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument(
        "--log-level",
        dest="loglevel",
        choices=list(LOG_LEVELS.keys()) + [key.upper() for key in LOG_LEVELS.keys()],
        help="log level",
    )
    parser.add_argument(
        "--log-output",
        dest="logoutput",
        help=(
            "The location where the logs will be written. The default behavior "
            "is to write to stdout (you can force it by passing '-' to "
            "this option). Takes a filename otherwise."
        ),
    )
    parser.add_argument(
        "--logger-config",
        dest="loggerconfig",
        help=(
            "The location where a standard Python logger configuration INI, "
            "JSON or YAML file can be found.  This can be used to override "
            "the default logging configuration for the arbiter."
        ),
    )

    parser.add_argument("--daemon", dest="daemonize", action="store_true", help="Start circusd in the background")
    parser.add_argument("--pidfile", dest="pidfile")
    parser.add_argument("--version", action="store_true", default=False, help="Displays Circus version and exits.")

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    # From here it can also come from the arbiter configuration
    # load the arbiter from config
    arbiter = Arbiter.load_from_config(args.config)

    # go ahead and set umask early if it is in the config
    if arbiter.umask is not None:
        os.umask(arbiter.umask)

    pidfile = args.pidfile or arbiter.pidfile or None
    if pidfile:
        pidfile = Pidfile(pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError as e:
            print(str(e))
            sys.exit(1)

    # configure the logger
    loglevel = args.loglevel or arbiter.loglevel or "info"
    logoutput = args.logoutput or arbiter.logoutput or "-"
    loggerconfig = args.loggerconfig or arbiter.loggerconfig or None
    configure_logger(logger, loglevel, logoutput, loggerconfig)

    # Main loop
    restart = True
    while restart:
        try:
            arbiter = arbiter or Arbiter.load_from_config(args.config)
            future = arbiter.start()
            restart = False
            if check_future_exception_and_log(future) is None:
                restart = arbiter._restarting
        except Exception as e:
            # emergency stop
            arbiter.loop.run_sync(arbiter._emergency_stop)
            raise (e)
        except KeyboardInterrupt:
            pass
        finally:
            arbiter = None
            if pidfile is not None:
                pidfile.unlink()
    sys.exit(0)
Exemple #17
0
def _start_circus(foreground):
    """
    This will actually launch the circus daemon, either daemonized in the background
    or in the foreground, printing all logs to stdout.

    .. note:: this should not be called directly from the commandline!
    """
    from circus import get_arbiter
    from circus import logger as circus_logger
    from circus.circusd import daemonize
    from circus.pidfile import Pidfile
    from circus.util import check_future_exception_and_log, configure_logger

    client = DaemonClient()

    loglevel = client.loglevel
    logoutput = '-'

    if not foreground:
        logoutput = client.circus_log_file

    arbiter_config = {
        'controller': client.get_controller_endpoint(),
        'pubsub_endpoint': client.get_pubsub_endpoint(),
        'stats_endpoint': client.get_stats_endpoint(),
        'logoutput': logoutput,
        'loglevel': loglevel,
        'debug': False,
        'statsd': True,
        'pidfile': client.circus_pid_file,
        'watchers': [{
            'name': client.daemon_name,
            'cmd': client.cmd_string,
            'virtualenv': client.virtualenv,
            'copy_env': True,
            'stdout_stream': {
                'class': 'FileStream',
                'filename': client.daemon_log_file,
            },
            'env': get_env_with_venv_bin(),
        }]
    } # yapf: disable

    if not foreground:
        daemonize()

    arbiter = get_arbiter(**arbiter_config)
    pidfile = Pidfile(arbiter.pidfile)

    try:
        pidfile.create(os.getpid())
    except RuntimeError as exception:
        click.echo(str(exception))
        sys.exit(1)

    # Configure the logger
    loggerconfig = None
    loggerconfig = loggerconfig or arbiter.loggerconfig or None
    configure_logger(circus_logger, loglevel, logoutput, loggerconfig)

    # Main loop
    should_restart = True

    while should_restart:
        try:
            arbiter = arbiter
            future = arbiter.start()
            should_restart = False
            if check_future_exception_and_log(future) is None:
                should_restart = arbiter._restarting  # pylint: disable=protected-access
        except Exception as exception:
            # Emergency stop
            arbiter.loop.run_sync(arbiter._emergency_stop)  # pylint: disable=protected-access
            raise exception
        except KeyboardInterrupt:
            pass
        finally:
            arbiter = None
            if pidfile is not None:
                pidfile.unlink()

    sys.exit(0)
Exemple #18
0
def main():
    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file', nargs='?')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level', dest='loglevel',
                        choices=list(LOG_LEVELS.keys()) + [
                            key.upper() for key in LOG_LEVELS.keys()],
                        help="log level")
    parser.add_argument('--log-output', dest='logoutput', help=(
        "The location where the logs will be written. The default behavior "
        "is to write to stdout (you can force it by passing '-' to "
        "this option). Takes a filename otherwise."))

    parser.add_argument('--daemon', dest='daemonize', action='store_true',
                        help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')
    parser.add_argument('--version', action='store_true', default=False,
                        help='Displays Circus version and exits.')

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    # basic logging configuration
    logging.basicConfig()

    # From here it can also come from the arbiter configuration
    # load the arbiter from config
    arbiter = Arbiter.load_from_config(args.config)

    pidfile = args.pidfile or arbiter.pidfile or None
    if pidfile:
        pidfile = Pidfile(pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError as e:
            print(str(e))
            sys.exit(1)

    # configure the logger
    loglevel = args.loglevel or arbiter.loglevel or 'info'
    logoutput = args.logoutput or arbiter.logoutput or '-'
    configure_logger(logger, loglevel, logoutput)

    # configure the main loop
    #ioloop.install()
    #loop = ioloop.IOLoop.instance()
    #cb = functools.partial(manage_restart, loop, arbiter)
    #periodic = tornado.ioloop.PeriodicCallback(cb, 1000, loop)
    #periodic.start()

    # schedule the arbiter start
    #arbiter = Arbiter.load_from_config(args.config, loop=loop)
    #loop.add_future(arbiter.start(), _arbiter_start_cb)

    # Main loop
    restart = True
    while restart:
        try:
            arbiter = Arbiter.load_from_config(args.config)
            future = arbiter.start()
            restart = False
            if check_future_exception_and_log(future) is None:
                restart = arbiter._restarting
        except Exception as e:
            # emergency stop
            arbiter.loop.run_sync(arbiter._emergency_stop)
            raise(e)
        except KeyboardInterrupt:
            pass
        finally:
            if pidfile is not None:
                pidfile.unlink()
    sys.exit(0)
Exemple #19
0
def main():
    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file', nargs='?')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level',
                        dest='loglevel',
                        choices=list(LOG_LEVELS.keys()) +
                        [key.upper() for key in LOG_LEVELS.keys()],
                        help="log level")
    parser.add_argument(
        '--log-output',
        dest='logoutput',
        help=(
            "The location where the logs will be written. The default behavior "
            "is to write to stdout (you can force it by passing '-' to "
            "this option). Takes a filename otherwise."))

    parser.add_argument('--daemon',
                        dest='daemonize',
                        action='store_true',
                        help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')
    parser.add_argument('--version',
                        action='store_true',
                        default=False,
                        help='Displays Circus version and exits.')

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    # basic logging configuration
    logging.basicConfig()

    # From here it can also come from the arbiter configuration
    # load the arbiter from config
    arbiter = Arbiter.load_from_config(args.config)

    pidfile = args.pidfile or arbiter.pidfile or None
    if pidfile:
        pidfile = Pidfile(pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError as e:
            print(str(e))
            sys.exit(1)

    # configure the logger
    loglevel = args.loglevel or arbiter.loglevel or 'info'
    logoutput = args.logoutput or arbiter.logoutput or '-'
    configure_logger(logger, loglevel, logoutput)

    # configure the main loop
    #ioloop.install()
    #loop = ioloop.IOLoop.instance()
    #cb = functools.partial(manage_restart, loop, arbiter)
    #periodic = tornado.ioloop.PeriodicCallback(cb, 1000, loop)
    #periodic.start()

    # schedule the arbiter start
    #arbiter = Arbiter.load_from_config(args.config, loop=loop)
    #loop.add_future(arbiter.start(), _arbiter_start_cb)

    # Main loop
    restart = True
    while restart:
        try:
            arbiter = Arbiter.load_from_config(args.config)
            future = arbiter.start()
            restart = False
            if check_future_exception_and_log(future) is None:
                restart = arbiter._restarting
        except Exception as e:
            # emergency stop
            arbiter.loop.run_sync(arbiter._emergency_stop)
            raise (e)
        except KeyboardInterrupt:
            pass
        finally:
            if pidfile is not None:
                pidfile.unlink()
    sys.exit(0)
Exemple #20
0
def main():
    import zmq
    try:
        zmq_version = [int(part) for part in zmq.__version__.split('.')]
        if len(zmq_version) < 2:
            raise ValueError()
    except (AttributeError, ValueError):
        print('Unknown PyZQM version - aborting...')
        sys.exit(0)

    if zmq_version[0] < 13 or (zmq_version[0] == 13 and zmq_version[1] < 1):
        print('circusd needs PyZMQ >= 13.1.0 to run - aborting...')
        sys.exit(0)

    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file', nargs='?')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level', dest='loglevel',
                        choices=list(LOG_LEVELS.keys()) + [
                            key.upper() for key in LOG_LEVELS.keys()],
                        help="log level")
    parser.add_argument('--log-output', dest='logoutput', help=(
        "The location where the logs will be written. The default behavior "
        "is to write to stdout (you can force it by passing '-' to "
        "this option). Takes a filename otherwise."))

    parser.add_argument('--daemon', dest='daemonize', action='store_true',
                        help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')
    parser.add_argument('--version', action='store_true', default=False,
                        help='Displays Circus version and exits.')

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    # This config call is done to avoid any
    #     "no handlers could be found for logger"
    #
    # error while loding the configuration and setting up the arbiter.
    # The real logging configuration is done right after via
    # a configure_logger() call
    logging.basicConfig()

    # From here it can also come from the arbiter configuration
    # load the arbiter from config
    arbiter = Arbiter.load_from_config(args.config)

    # go ahead and set umask early if it is in the config
    if arbiter.umask is not None:
        os.umask(arbiter.umask)

    pidfile = args.pidfile or arbiter.pidfile or None
    if pidfile:
        pidfile = Pidfile(pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError as e:
            print(str(e))
            sys.exit(1)

    # configure the logger
    loglevel = args.loglevel or arbiter.loglevel or 'info'
    logoutput = args.logoutput or arbiter.logoutput or '-'
    configure_logger(logger, loglevel, logoutput)

    # Main loop
    restart = True
    while restart:
        try:
            arbiter = arbiter or Arbiter.load_from_config(args.config)
            future = arbiter.start()
            restart = False
            if check_future_exception_and_log(future) is None:
                restart = arbiter._restarting
        except Exception as e:
            # emergency stop
            arbiter.loop.run_sync(arbiter._emergency_stop)
            raise(e)
        except KeyboardInterrupt:
            pass
        finally:
            arbiter = None
            if pidfile is not None:
                pidfile.unlink()
    sys.exit(0)
Exemple #21
0
    def test_pidfile_data(self):
        proc = subprocess.Popen(SLEEP % 120, shell=True)
        fd, path = tempfile.mkstemp()

        os.write(fd, "fail-to-validate\n".encode('utf-8'))
        os.close(fd)

        try:
            pidfile = Pidfile(path)

            pidfile.create(proc.pid)
            self.assertTrue(os.path.exists(path))
            pid = 0
            with open(path, "r") as f:
                pid = int(f.read() or 0)
            self.assertEqual(pid, proc.pid)
        finally:
            os.remove(path)

        fd, path = tempfile.mkstemp()

        proc2 = subprocess.Popen(SLEEP % 0, shell=True)

        os.write(fd, "{0}\n".format(proc2.pid).encode('utf-8'))
        os.close(fd)

        proc2.wait()

        try:
            pidfile = Pidfile(path)

            self.assertNotEqual(proc2.pid, proc.pid)
            pidfile.create(proc.pid)
        finally:
            os.remove(path)

        fd, path = tempfile.mkstemp()

        os.write(fd, "fail-to-int\n".encode('utf-8'))
        os.close(fd)

        try:
            pidfile = Pidfile(path)

            pidfile.unlink()
            self.assertFalse(os.path.exists(path))
        except Exception as e:
            self.fail(str(e))
Exemple #22
0
def main():
    import zmq
    try:
        zmq_version = [int(part) for part in zmq.__version__.split('.')]
        if len(zmq_version) < 2:
            raise ValueError()
    except (AttributeError, ValueError):
        print('Unknown PyZQM version - aborting...')
        sys.exit(0)

    if zmq_version[0] < 13 or (zmq_version[0] == 13 and zmq_version[1] < 1):
        print('circusd needs PyZMQ >= 13.1.0 to run - aborting...')
        sys.exit(0)

    parser = argparse.ArgumentParser(description='Run some watchers.')
    parser.add_argument('config', help='configuration file', nargs='?')

    # XXX we should be able to add all these options in the config file as well
    parser.add_argument('--log-level',
                        dest='loglevel',
                        choices=list(LOG_LEVELS.keys()) +
                        [key.upper() for key in LOG_LEVELS.keys()],
                        help="log level")
    parser.add_argument(
        '--log-output',
        dest='logoutput',
        help=(
            "The location where the logs will be written. The default behavior "
            "is to write to stdout (you can force it by passing '-' to "
            "this option). Takes a filename otherwise."))
    parser.add_argument(
        "--logger-config",
        dest="loggerconfig",
        help=("The location where a standard Python logger configuration INI, "
              "JSON or YAML file can be found.  This can be used to override "
              "the default logging configuration for the arbiter."))

    parser.add_argument('--daemon',
                        dest='daemonize',
                        action='store_true',
                        help="Start circusd in the background")
    parser.add_argument('--pidfile', dest='pidfile')
    parser.add_argument('--version',
                        action='store_true',
                        default=False,
                        help='Displays Circus version and exits.')

    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config is None:
        parser.print_usage()
        sys.exit(0)

    if args.daemonize:
        daemonize()

    # From here it can also come from the arbiter configuration
    # load the arbiter from config
    arbiter = Arbiter.load_from_config(args.config)

    # go ahead and set umask early if it is in the config
    if arbiter.umask is not None:
        os.umask(arbiter.umask)

    pidfile = args.pidfile or arbiter.pidfile or None
    if pidfile:
        pidfile = Pidfile(pidfile)

        try:
            pidfile.create(os.getpid())
        except RuntimeError as e:
            print(str(e))
            sys.exit(1)

    # configure the logger
    loglevel = args.loglevel or arbiter.loglevel or 'info'
    logoutput = args.logoutput or arbiter.logoutput or '-'
    loggerconfig = args.loggerconfig or arbiter.loggerconfig or None
    configure_logger(logger, loglevel, logoutput, loggerconfig)

    # Main loop
    restart = True
    while restart:
        try:
            arbiter = arbiter or Arbiter.load_from_config(args.config)
            future = arbiter.start()
            restart = False
            if check_future_exception_and_log(future) is None:
                restart = arbiter._restarting
        except Exception as e:
            # emergency stop
            arbiter.loop.run_sync(arbiter._emergency_stop)
            raise (e)
        except KeyboardInterrupt:
            pass
        finally:
            arbiter = None
            if pidfile is not None:
                pidfile.unlink()
    sys.exit(0)