Skip to content

Commit

Permalink
CLI: Fix verdi process repair not actually repairing (#6264)
Browse files Browse the repository at this point in the history
The command would use `echo_critical` to report an inconsistency when
found, however, this function immediately exists the interpreter and so
the code that would perform the fix is unreachable. The command now
simply emits a warning if an inconsistency is detected, except in the
case of a `--dry-run` when the command still immediately exits with a
non-zero exit code.
  • Loading branch information
sphuber committed Jan 30, 2024
1 parent 06ea130 commit 784ad64
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/source/nitpick-exceptions
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ py:func click.shell_completion._start_of_option
py:meth click.Option.get_default
py:meth fail

py:class ComputedFieldInfo
py:class pydantic.main.BaseModel

py:class requests.models.Response
Expand Down
9 changes: 4 additions & 5 deletions src/aiida/cmdline/commands/cmd_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,18 @@ def process_repair(ctx, manager, dry_run):
echo.echo_warning('There are active processes without process task: ', nl=False)
echo.echo(set_active_processes.difference(set_process_tasks))

if state_inconsistent:
echo.echo_critical('Inconsistencies detected between database and RabbitMQ.')

if not state_inconsistent:
echo.echo_success('No inconsistencies detected between database and RabbitMQ.')
return

echo.echo_warning('Inconsistencies detected between database and RabbitMQ.')

if dry_run:
return
echo.echo_critical('This was a dry-run, no changes will be made.')

# At this point we have either exited because of inconsistencies and ``--dry-run`` was passed, or we returned
# because there were no inconsistencies, so all that is left is to address inconsistencies
echo.echo_info('Attempting to fix inconsistencies')
echo.echo_report('Attempting to fix inconsistencies')

# Eliminate duplicate tasks and tasks that correspond to terminated process
for task in iterate_process_tasks(ctx.obj.profile, manager.get_communicator()):
Expand Down
21 changes: 17 additions & 4 deletions tests/cmdline/commands/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def test_process_repair_duplicate_tasks(monkeypatch, run_cli_command):
monkeypatch.setattr(process_control, 'get_active_processes', lambda *args, **kwargs: [1, 2])
monkeypatch.setattr(process_control, 'get_process_tasks', lambda *args: [1, 2, 2])

result = run_cli_command(cmd_process.process_repair, raises=True, use_subprocess=False)
result = run_cli_command(cmd_process.process_repair, use_subprocess=False)
assert 'There are duplicates process tasks:' in result.output
assert 'Inconsistencies detected between database and RabbitMQ.' in result.output

Expand All @@ -519,9 +519,10 @@ def test_process_repair_additional_tasks(monkeypatch, run_cli_command):
monkeypatch.setattr(process_control, 'get_active_processes', lambda *args, **kwargs: [1, 2])
monkeypatch.setattr(process_control, 'get_process_tasks', lambda *args: [1, 2, 3])

result = run_cli_command(cmd_process.process_repair, raises=True, use_subprocess=False)
result = run_cli_command(cmd_process.process_repair, use_subprocess=False)
assert 'There are process tasks for terminated processes:' in result.output
assert 'Inconsistencies detected between database and RabbitMQ.' in result.output
assert 'Attempting to fix inconsistencies' in result.output


@pytest.mark.usefixtures('stopped_daemon_client')
Expand All @@ -530,9 +531,21 @@ def test_process_repair_missing_tasks(monkeypatch, run_cli_command):
monkeypatch.setattr(process_control, 'get_active_processes', lambda *args, **kwargs: [1, 2, 3])
monkeypatch.setattr(process_control, 'get_process_tasks', lambda *args: [1, 2])

result = run_cli_command(cmd_process.process_repair, raises=True, use_subprocess=False)
result = run_cli_command(cmd_process.process_repair, use_subprocess=False)
assert 'There are active processes without process task:' in result.output
assert 'Inconsistencies detected between database and RabbitMQ.' in result.output
assert 'Attempting to fix inconsistencies' in result.output


@pytest.mark.usefixtures('stopped_daemon_client')
def test_process_repair_dry_run(monkeypatch, run_cli_command):
"""Test the ``verdi process repair`` command with ``--dry-run```."""
monkeypatch.setattr(process_control, 'get_active_processes', lambda *args, **kwargs: [1, 2, 3, 4])
monkeypatch.setattr(process_control, 'get_process_tasks', lambda *args: [1, 2])

result = run_cli_command(cmd_process.process_repair, ['--dry-run'], raises=True, use_subprocess=False)
assert 'Inconsistencies detected between database and RabbitMQ.' in result.output
assert 'This was a dry-run, no changes will be made.' in result.output


@pytest.mark.usefixtures('stopped_daemon_client')
Expand All @@ -541,6 +554,6 @@ def test_process_repair_verbosity(monkeypatch, run_cli_command):
monkeypatch.setattr(process_control, 'get_active_processes', lambda *args, **kwargs: [1, 2, 3, 4])
monkeypatch.setattr(process_control, 'get_process_tasks', lambda *args: [1, 2])

result = run_cli_command(cmd_process.process_repair, ['-v', 'INFO'], raises=True, use_subprocess=False)
result = run_cli_command(cmd_process.process_repair, ['-v', 'INFO'], use_subprocess=False)
assert 'Active processes: [1, 2, 3, 4]' in result.output
assert 'Process tasks: [1, 2]' in result.output

0 comments on commit 784ad64

Please sign in to comment.