Skip to content

Commit 5785eaf

Browse files
committed
verdi presto: Fall back to ZMQ (aiidateam#7284)
Change `verdi presto` to always configure a broker: it tries RabbitMQ first and falls back to ZMQ if unavailable. Add `--use-zmq` flag to skip RabbitMQ detection and use ZMQ directly. Previously, profiles created without RabbitMQ had no broker at all, limiting functionality. Now every `verdi presto` profile gets a working broker out of the box. Regenerate the command-line reference during the rebase so the tracked autodocs stay in sync with the updated CLI.
1 parent 82b8458 commit 5785eaf

3 files changed

Lines changed: 50 additions & 26 deletions

File tree

docs/source/reference/command_line.rst

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,21 @@ Below is a list with all available subcommands.
320320
* Set up the localhost as a `Computer` and configure it
321321
* Set a number of configuration options with sensible defaults
322322
323-
By default the command creates a profile that uses SQLite for the database. It
324-
automatically checks for RabbitMQ running on the localhost, and, if it can connect,
325-
configures that as the broker for the profile. Otherwise, the profile is created without
326-
a broker, in which case some functionality will be unavailable, most notably running the
327-
daemon and submitting processes to said daemon.
323+
By default the command creates a profile that uses SQLite for the database. For the
324+
message broker, it automatically checks for RabbitMQ running on localhost. If found, it
325+
configures RabbitMQ as the broker. Otherwise, it falls back to the ZMQ broker, which
326+
requires no external services and is started automatically with the daemon.
328327
329328
When the `--use-postgres` flag is toggled, the command tries to connect to the
330329
PostgreSQL server with connection paramaters taken from the `--postgres-hostname`,
331330
`--postgres-port`, `--postgres-username` and `--postgres-password` options. It uses
332331
these credentials to try and automatically create a user and database. If successful,
333332
the newly created profile uses the new PostgreSQL database instead of SQLite.
334333
334+
When the `--use-zmq` flag is toggled, the command skips the RabbitMQ auto-detection and
335+
directly configures the ZMQ broker. To switch to RabbitMQ later, use `verdi profile
336+
configure-rabbitmq`.
337+
335338
Options:
336339
-p, --profile-name TEXT Name of the profile. By default, a unique name starting
337340
with `presto` is automatically generated. [default:
@@ -343,6 +346,12 @@ Below is a list with all available subcommands.
343346
options. The command attempts to automatically create a
344347
user and database to use for the profile, but this can
345348
fail depending on the configuration of the server.
349+
--use-zmq When toggled on, the profile uses the ZMQ broker, which
350+
requires no external services and is started
351+
automatically with the daemon. When not specified, the
352+
command automatically tries RabbitMQ first and falls
353+
back to ZMQ if unavailable. To switch to RabbitMQ later,
354+
use `verdi profile configure-rabbitmq`.
346355
--postgres-hostname TEXT The hostname of the PostgreSQL server.
347356
--postgres-port INTEGER The port of the PostgreSQL server.
348357
--postgres-username TEXT The username of the PostgreSQL user that is authorized

src/aiida/cmdline/commands/cmd_presto.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ def detect_postgres_config(
126126
'the PostgreSQL server can be configured with the relevant options. The command attempts to automatically create a '
127127
'user and database to use for the profile, but this can fail depending on the configuration of the server.',
128128
)
129+
@click.option(
130+
'--use-zmq',
131+
is_flag=True,
132+
help='When toggled on, the profile uses the ZMQ broker, which requires no external services and is started '
133+
'automatically with the daemon. When not specified, the command automatically tries RabbitMQ first and falls back '
134+
'to ZMQ if unavailable. To switch to RabbitMQ later, use `verdi profile configure-rabbitmq`.',
135+
)
129136
@click.option('--postgres-hostname', type=str, default='localhost', help='The hostname of the PostgreSQL server.')
130137
@click.option('--postgres-port', type=int, default=5432, help='The port of the PostgreSQL server.')
131138
@click.option(
@@ -147,6 +154,7 @@ def verdi_presto(
147154
profile_name,
148155
email,
149156
use_postgres,
157+
use_zmq,
150158
postgres_hostname,
151159
postgres_port,
152160
postgres_username,
@@ -169,18 +177,21 @@ def verdi_presto(
169177
* Set up the localhost as a `Computer` and configure it
170178
* Set a number of configuration options with sensible defaults
171179
172-
By default the command creates a profile that uses SQLite for the database. It automatically checks for RabbitMQ
173-
running on the localhost, and, if it can connect, configures that as the broker for the profile. Otherwise, the
174-
profile is created without a broker, in which case some functionality will be unavailable, most notably running the
175-
daemon and submitting processes to said daemon.
180+
By default the command creates a profile that uses SQLite for the database. For the message broker, it automatically
181+
checks for RabbitMQ running on localhost. If found, it configures RabbitMQ as the broker. Otherwise, it falls back
182+
to the ZMQ broker, which requires no external services and is started automatically with the daemon.
176183
177184
When the `--use-postgres` flag is toggled, the command tries to connect to the PostgreSQL server with connection
178185
paramaters taken from the `--postgres-hostname`, `--postgres-port`, `--postgres-username` and `--postgres-password`
179186
options. It uses these credentials to try and automatically create a user and database. If successful, the newly
180187
created profile uses the new PostgreSQL database instead of SQLite.
188+
189+
When the `--use-zmq` flag is toggled, the command skips the RabbitMQ auto-detection and directly configures the ZMQ
190+
broker. To switch to RabbitMQ later, use `verdi profile configure-rabbitmq`.
181191
"""
182192
from aiida.brokers.rabbitmq.defaults import detect_rabbitmq_config
183-
from aiida.common import docs, exceptions
193+
from aiida.brokers.zmq.defaults import get_zmq_config
194+
from aiida.common import exceptions
184195
from aiida.manage.configuration import create_profile, load_profile
185196
from aiida.orm import Computer
186197

@@ -212,17 +223,20 @@ def verdi_presto(
212223
else:
213224
echo.echo_report('Option `--use-postgres` not enabled: configuring the profile to use SQLite.')
214225

215-
broker_backend = None
216-
broker_config = None
217-
218-
try:
219-
broker_config = detect_rabbitmq_config()
220-
except ConnectionError as exception:
221-
echo.echo_report(f'RabbitMQ server not found ({exception}): configuring the profile without a broker.')
222-
echo.echo_report(f'See {docs.URL_NO_BROKER} for details on the limitations of running without a broker.')
226+
if use_zmq:
227+
echo.echo_report('`--use-zmq` enabled: configuring the profile with ZMQ broker.')
228+
broker_backend = 'core.zmq'
229+
broker_config = get_zmq_config()
223230
else:
224-
echo.echo_report('RabbitMQ server detected: configuring the profile with a broker.')
225-
broker_backend = 'core.rabbitmq'
231+
try:
232+
broker_config = detect_rabbitmq_config()
233+
except ConnectionError:
234+
echo.echo_report('RabbitMQ server not found: falling back to ZMQ broker.')
235+
broker_backend = 'core.zmq'
236+
broker_config = get_zmq_config()
237+
else:
238+
echo.echo_report('RabbitMQ server detected: configuring the profile with RabbitMQ broker.')
239+
broker_backend = 'core.rabbitmq'
226240

227241
try:
228242
profile = create_profile(

tests/cmdline/commands/test_presto.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_profile_names(self):
3232

3333
@pytest.mark.usefixtures('empty_config')
3434
def test_presto_without_rmq(pytestconfig, run_cli_command, monkeypatch):
35-
"""Test the ``verdi presto`` without RabbitMQ."""
35+
"""Test the ``verdi presto`` without RabbitMQ falls back to ZMQ."""
3636
from aiida.brokers.rabbitmq import defaults
3737

3838
def detect_rabbitmq_config(**kwargs):
@@ -48,21 +48,22 @@ def detect_rabbitmq_config(**kwargs):
4848
assert profile.name == 'presto'
4949
localhost = Computer.collection.get(label='localhost')
5050
assert localhost.is_configured
51-
assert profile.process_control_backend is None
51+
# When RabbitMQ is not available, presto falls back to ZMQ broker
52+
assert profile.process_control_backend == 'core.zmq'
5253

5354

54-
@pytest.mark.requires_rmq
5555
@pytest.mark.usefixtures('empty_config')
56-
def test_presto_with_rmq(pytestconfig, run_cli_command):
57-
"""Test the ``verdi presto``."""
56+
def test_presto(run_cli_command):
57+
"""Test that ``verdi presto`` configures a broker (RabbitMQ if available, otherwise ZMQ)."""
5858
result = run_cli_command(verdi_presto, ['--non-interactive'])
5959
assert 'Created new profile `presto`.' in result.output
6060

6161
with profile_context('presto', allow_switch=True) as profile:
6262
assert profile.name == 'presto'
6363
localhost = Computer.collection.get(label='localhost')
6464
assert localhost.is_configured
65-
assert profile.process_control_backend == 'core.rabbitmq'
65+
# Presto auto-detects RabbitMQ, falls back to ZMQ if not available
66+
assert profile.process_control_backend in ('core.rabbitmq', 'core.zmq')
6667

6768

6869
@pytest.mark.requires_psql

0 commit comments

Comments
 (0)