Skip to content

Commit

Permalink
Add non-zero exit code for verdi daemon status (#3729)
Browse files Browse the repository at this point in the history
`verdi daemon status` was always returning exit code 0, which makes it
difficult to use the command programmatically (e.g. in ansible).
It now returns exit code 3 if the daemon of any of the requested
profiles is not running.
  • Loading branch information
ltalirz authored Feb 11, 2020
1 parent 1ba87c0 commit 67ae49f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 11 additions & 2 deletions aiida/cmdline/commands/cmd_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os
import subprocess
import time
import sys

import click
from click_spinner import spinner
Expand Down Expand Up @@ -74,9 +75,12 @@ def start(foreground, number):


@verdi_daemon.command()
@click.option('--all', 'all_profiles', is_flag=True, help='Show all daemons.')
@click.option('--all', 'all_profiles', is_flag=True, help='Show status of all daemons.')
def status(all_profiles):
"""Print the status of the current daemon or all daemons."""
"""Print the status of the current daemon or all daemons.
Returns exit code 0 if all requested daemons are running, else exit code 3.
"""
from aiida.engine.daemon.client import get_daemon_client

config = get_config()
Expand All @@ -86,13 +90,18 @@ def status(all_profiles):
else:
profiles = [config.current_profile]

daemons_running = []
for profile in profiles:
client = get_daemon_client(profile.name)
delete_stale_pid_file(client)
click.secho('Profile: ', fg='red', bold=True, nl=False)
click.secho('{}'.format(profile.name), bold=True)
result = get_daemon_status(client)
echo.echo(result)
daemons_running.append(client.is_daemon_running)

if not all(daemons_running):
sys.exit(3)


@verdi_daemon.command()
Expand Down
2 changes: 2 additions & 0 deletions tests/cmdline/utils/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def test_daemon_not_running():
"""Test `get_daemon_status` output when the daemon is not running."""
client = get_daemon_client()
assert 'The daemon is not running' in get_daemon_status(client)
assert not client.is_daemon_running


@patch.object(DaemonClient, 'is_daemon_running', lambda: True)
Expand All @@ -120,6 +121,7 @@ def test_daemon_working():
4990 0.231 0 2019-12-17 12:27:38
Use verdi daemon [incr | decr] [num] to increase / decrease the amount of workers"""
assert get_daemon_status(client) == literal
assert client.is_daemon_running


@patch.object(DaemonClient, 'is_daemon_running', lambda: True)
Expand Down

0 comments on commit 67ae49f

Please sign in to comment.