コード例 #1
0
ファイル: cmd_daemon.py プロジェクト: chrisjsewell/aiida_core
def stop(no_wait, all_profiles):
    """
    Stop the daemon
    """
    if all_profiles is True:
        profiles = [
            p for p in get_profiles_list() if not p.startswith('test_')
        ]
    else:
        profiles = [get_current_profile_name()]

    for profile_name in profiles:

        client = DaemonClient(profile_name)

        click.secho('Profile: ', fg='red', bold=True, nl=False)
        click.secho('{}'.format(profile_name), bold=True)

        if not client.is_daemon_running:
            click.echo('Daemon was not running')
            continue

        wait = not no_wait

        if wait:
            click.echo('Waiting for the daemon to shut down... ', nl=False)
        else:
            click.echo('Shutting the daemon down')

        response = client.stop_daemon(wait)

        if wait:
            print_client_response_status(response)
コード例 #2
0
ファイル: cmd_daemon.py プロジェクト: chrisjsewell/aiida_core
def incr(number):
    """
    Add NUMBER [default=1] workers to the running daemon
    """
    client = DaemonClient()
    response = client.increase_workers(number)
    print_client_response_status(response)
コード例 #3
0
ファイル: cmd_daemon.py プロジェクト: chrisjsewell/aiida_core
def start(foreground):
    """
    Start the daemon
    """
    client = DaemonClient()

    click.echo('Starting the daemon... ', nl=False)

    if foreground:
        command = [
            'verdi', '-p', client.profile_name, 'daemon', '_start_circus',
            '--foreground'
        ]
    else:
        command = [
            'verdi', '-p', client.profile_name, 'daemon', '_start_circus'
        ]

    try:
        currenv = get_env_with_venv_bin()
        subprocess.check_output(command, env=currenv)
    except subprocess.CalledProcessError as exception:
        click.echo('failed: {}'.format(exception))
        sys.exit(1)

    # We add a small timeout to give the pid-file a chance to be created
    with spinner():
        time.sleep(1)
        response = client.get_status()

    print_client_response_status(response)
コード例 #4
0
ファイル: cmd_daemon.py プロジェクト: chrisjsewell/aiida_core
def restart(ctx, reset, no_wait):
    """
    Restart the daemon. By default will only reset the workers of the running daemon.
    After the restart the same amount of workers will be running. If the --reset flag
    is passed, however, the full circus daemon will be stopped and restarted with just
    a single worker
    """
    client = DaemonClient()

    wait = not no_wait

    if reset:
        ctx.invoke(stop)
        ctx.invoke(start)
    else:

        if wait:
            click.echo('Restarting the daemon... ', nl=False)
        else:
            click.echo('Restarting the daemon')

        response = client.restart_daemon(wait)

        if wait:
            print_client_response_status(response)
コード例 #5
0
ファイル: cmd_daemon.py プロジェクト: chrisjsewell/aiida_core
def decr(number):
    """
    Remove NUMBER [default=1] workers from the running daemon
    """
    client = DaemonClient()
    response = client.decrease_workers(number)
    print_client_response_status(response)
コード例 #6
0
    def decorated_function(*args, **kwargs):
        """
        If daemon pid file is not found / empty, exit without doing anything
        """
        from aiida.daemon.client import DaemonClient

        daemon_client = DaemonClient()

        if not daemon_client.get_daemon_pid():
            click.echo('The daemon is not running')
            sys.exit(0)

        return function(*args, **kwargs)
コード例 #7
0
def print_last_process_state_change(process_type=None):
    """
    Print the last time that a process of the specified type has changed its state.
    This function will also print a warning if the daemon is not running.

    :param process_type: optional process type for which to get the latest state change timestamp.
        Valid process types are either 'calculation' or 'work'.
    """
    from aiida.cmdline.utils.echo import echo_info, echo_warning
    from aiida.daemon.client import DaemonClient
    from aiida.utils import timezone
    from aiida.common.utils import str_timedelta
    from aiida.work.utils import get_process_state_change_timestamp

    client = DaemonClient()

    timestamp = get_process_state_change_timestamp(process_type)

    if timestamp is None:
        echo_info('last time an entry changed state: never')
    else:
        timedelta = timezone.delta(timestamp, timezone.now())
        formatted = format_local_time(timestamp,
                                      format_str='at %H:%M:%S on %Y-%m-%d')
        relative = str_timedelta(timedelta,
                                 negative_to_zero=True,
                                 max_num_fields=1)
        echo_info('last time an entry changed state: {} ({})'.format(
            relative, formatted))

    if not client.is_daemon_running:
        echo_warning('the daemon is not running', bold=True)
コード例 #8
0
ファイル: runner.py プロジェクト: chrisjsewell/aiida_core
def start_daemon():
    """
    Start a daemon runner for the currently configured profile
    """
    daemon_client = DaemonClient()
    configure_logging(daemon=True,
                      daemon_log_file=daemon_client.daemon_log_file)

    runner = DaemonRunner(rmq_config=get_rmq_config(), rmq_submit=False)

    def shutdown_daemon(num, frame):
        logger.info('Received signal to shut down the daemon runner')
        runner.close()

    signal.signal(signal.SIGINT, shutdown_daemon)
    signal.signal(signal.SIGTERM, shutdown_daemon)

    logger.info('Starting a daemon runner')

    set_runner(runner)
    tick_legacy_workflows(runner)

    try:
        runner.start()
    except SystemError as exception:
        logger.info('Received a SystemError: {}'.format(exception))
        runner.close()

    logger.info('Daemon runner stopped')
コード例 #9
0
    def test_ipc_socket_file_length_limit(self):
        """
        The maximum length of socket filepaths is often limited by the operating system.
        For MacOS it is limited to 103 bytes, versus 107 bytes on Unix. This limit is
        exposed by the Zmq library which is used by Circus library that is used to
        daemonize the daemon runners. This test verifies that the three endpoints used
        for the Circus client have a filepath that does not exceed that path limit.

        See issue #1317 and pull request #1403 for the discussion
        """
        daemon_client = DaemonClient()

        controller_endpoint = daemon_client.get_controller_endpoint()
        pubsub_endpoint = daemon_client.get_pubsub_endpoint()
        stats_endpoint = daemon_client.get_stats_endpoint()

        self.assertTrue(len(controller_endpoint) <= zmq.IPC_PATH_MAX_LEN)
        self.assertTrue(len(pubsub_endpoint) <= zmq.IPC_PATH_MAX_LEN)
        self.assertTrue(len(stats_endpoint) <= zmq.IPC_PATH_MAX_LEN)
コード例 #10
0
def print_daemon_log():
    daemon_client = DaemonClient()
    daemon_log = daemon_client.daemon_log_file

    print "Output of 'cat {}':".format(daemon_log)
    try:
        print subprocess.check_output(
            ['cat', '{}'.format(daemon_log)],
            stderr=subprocess.STDOUT,
        )
    except subprocess.CalledProcessError as e:
        print "Note: the command failed, message: {}".format(e.message)
コード例 #11
0
ファイル: cmd_daemon.py プロジェクト: chrisjsewell/aiida_core
def logshow():
    """
    Show the log of the daemon, press CTRL+C to quit
    """
    client = DaemonClient()

    try:
        currenv = get_env_with_venv_bin()
        process = subprocess.Popen(['tail', '-f', client.daemon_log_file],
                                   env=currenv)
        process.wait()
    except KeyboardInterrupt:
        process.kill()
コード例 #12
0
ファイル: cmd_daemon.py プロジェクト: chrisjsewell/aiida_core
def status(all_profiles):
    """
    Print the status of the current daemon or all daemons
    """
    if all_profiles is True:
        profiles = [
            p for p in get_profiles_list() if not p.startswith('test_')
        ]
    else:
        profiles = [get_current_profile_name()]

    for profile_name in profiles:
        client = DaemonClient(profile_name)
        click.secho('Profile: ', fg='red', bold=True, nl=False)
        click.secho('{}'.format(profile_name), bold=True)
        result = get_daemon_status(client)
        click.echo(result)
コード例 #13
0
ファイル: cmd_daemon.py プロジェクト: chrisjsewell/aiida_core
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)