From c9ca0d7348638c6611a6e4d4dc10f4234c1ac8d8 Mon Sep 17 00:00:00 2001 From: Marnik Bercx Date: Wed, 12 Jun 2024 10:59:00 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20CLI:=20Give=20feedback=20for=20`?= =?UTF-8?q?configure-rabbitmq`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the `verdi profile configure-rabbitmq` command doesn't give any feedback to the user whether the provided options can successfully connect to the RabbitMQ server. Here we adapt the `detect_rabbitmq_config` function to accept the broker configuration as `**kwargs`, and use it to check if the provided options in the `configure-rabbitmq` can successfully connect to the RabbitMQ server. A "success" message is printed if we can connect to the server, else a warning is printed. --- src/aiida/brokers/rabbitmq/defaults.py | 18 ++++++++++-------- src/aiida/cmdline/commands/cmd_profile.py | 12 ++++++++++++ tests/cmdline/commands/test_profile.py | 9 ++++++++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/aiida/brokers/rabbitmq/defaults.py b/src/aiida/brokers/rabbitmq/defaults.py index aeeaab578d..f34014dcc3 100644 --- a/src/aiida/brokers/rabbitmq/defaults.py +++ b/src/aiida/brokers/rabbitmq/defaults.py @@ -29,7 +29,7 @@ ) -def detect_rabbitmq_config() -> dict[str, t.Any] | None: +def detect_rabbitmq_config(**kwargs) -> dict[str, t.Any] | None: """Try to connect to a RabbitMQ server with the default connection parameters. :returns: The connection parameters if the RabbitMQ server was successfully connected to, or ``None`` otherwise. @@ -37,13 +37,15 @@ def detect_rabbitmq_config() -> dict[str, t.Any] | None: from kiwipy.rmq.threadcomms import connect connection_params = { - 'protocol': os.getenv('AIIDA_BROKER_PROTOCOL', BROKER_DEFAULTS['protocol']), - 'username': os.getenv('AIIDA_BROKER_USERNAME', BROKER_DEFAULTS['username']), - 'password': os.getenv('AIIDA_BROKER_PASSWORD', BROKER_DEFAULTS['password']), - 'host': os.getenv('AIIDA_BROKER_HOST', BROKER_DEFAULTS['host']), - 'port': os.getenv('AIIDA_BROKER_PORT', BROKER_DEFAULTS['port']), - 'virtual_host': os.getenv('AIIDA_BROKER_VIRTUAL_HOST', BROKER_DEFAULTS['virtual_host']), - 'heartbeat': os.getenv('AIIDA_BROKER_HEARTBEAT', BROKER_DEFAULTS['heartbeat']), + 'protocol': kwargs.get('broker_protocol', os.getenv('AIIDA_BROKER_PROTOCOL', BROKER_DEFAULTS['protocol'])), + 'username': kwargs.get('broker_username', os.getenv('AIIDA_BROKER_USERNAME', BROKER_DEFAULTS['username'])), + 'password': kwargs.get('broker_password', os.getenv('AIIDA_BROKER_PASSWORD', BROKER_DEFAULTS['password'])), + 'host': kwargs.get('broker_host', os.getenv('AIIDA_BROKER_HOST', BROKER_DEFAULTS['host'])), + 'port': kwargs.get('broker_port', os.getenv('AIIDA_BROKER_PORT', BROKER_DEFAULTS['port'])), + 'virtual_host': kwargs.get( + 'broker_virtual_host', os.getenv('AIIDA_BROKER_VIRTUAL_HOST', BROKER_DEFAULTS['virtual_host']) + ), + 'heartbeat': kwargs.get('broker_heartbeat', os.getenv('AIIDA_BROKER_HEARTBEAT', BROKER_DEFAULTS['heartbeat'])), } LOGGER.info(f'Attempting to connect to RabbitMQ with parameters: {connection_params}') diff --git a/src/aiida/cmdline/commands/cmd_profile.py b/src/aiida/cmdline/commands/cmd_profile.py index 0c9bb4498a..5c08a1f545 100644 --- a/src/aiida/cmdline/commands/cmd_profile.py +++ b/src/aiida/cmdline/commands/cmd_profile.py @@ -143,10 +143,22 @@ def profile_configure_rabbitmq(ctx, profile, **kwargs): Enable RabbitMQ for a profile that was created without a broker, or reconfigure existing connection details. """ + from aiida.brokers.rabbitmq.defaults import detect_rabbitmq_config + profile.set_process_controller(name='core.rabbitmq', config=kwargs) ctx.obj.config.update_profile(profile) ctx.obj.config.store() + broker_config = detect_rabbitmq_config(**kwargs) + + if broker_config is None: + connection_params = {key: value for key, value in kwargs.items() if key.startswith('broker_')} + echo.echo_warning( + f'RabbitMQ configured but unable to connect to RabbitMQ server with configuration: {connection_params}' + ) + else: + echo.echo_success('Successfully connected to RabbitMQ server.') + @verdi_profile.command('list') def profile_list(): diff --git a/tests/cmdline/commands/test_profile.py b/tests/cmdline/commands/test_profile.py index 4918412cad..659f0f50a3 100644 --- a/tests/cmdline/commands/test_profile.py +++ b/tests/cmdline/commands/test_profile.py @@ -284,14 +284,16 @@ def test_configure_rabbitmq(run_cli_command, isolated_config): # Now run the command to configure the broker options = [profile_name, '-n'] - run_cli_command(cmd_profile.profile_configure_rabbitmq, options, use_subprocess=False) + cli_result = run_cli_command(cmd_profile.profile_configure_rabbitmq, options, use_subprocess=False) assert profile.process_control_backend == 'core.rabbitmq' + assert 'Successfully connected to RabbitMQ server' in cli_result.stdout # Call it again to check it works to reconfigure existing broker connection parameters options = [profile_name, '-n', '--broker-host', 'rabbitmq.broker.com'] run_cli_command(cmd_profile.profile_configure_rabbitmq, options, use_subprocess=False) assert profile.process_control_backend == 'core.rabbitmq' assert profile.process_control_config['broker_host'] == 'rabbitmq.broker.com' + assert 'Successfully connected to RabbitMQ server' in cli_result.stdout # Verify that running in non-interactive mode is the default options = [ @@ -299,3 +301,8 @@ def test_configure_rabbitmq(run_cli_command, isolated_config): ] run_cli_command(cmd_profile.profile_configure_rabbitmq, options, use_subprocess=True) assert profile.process_control_backend == 'core.rabbitmq' + + # Verify that configuring with incorrect options raises a warning + options = [profile_name, '--broker-port', '1234'] + cli_result = run_cli_command(cmd_profile.profile_configure_rabbitmq, options, use_subprocess=False) + assert 'Warning: RabbitMQ configured but unable to connect' in cli_result.stdout