DaemonClient: Refactor to include parsing of client response #5940
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #5934
Recently, the
DaemonClient
has been updated to provide all necessaryfunctionality to manage the daemon through the Python API just as it is
possible through
verdi daemon
. For example, the methodsstart_daemon
and
stop_daemon
were added. However, the interface of the client isstill lacking, particularly because the various methods returned a
dictionary from which the caller would still have to parse the actual
result from string values.
The
call_client
method is updated to detect various problems andcommunicate them more clearly by raising an exception. Various
subclasses of
DaemonException
are raised for certain problems:DaemonNotRunningException
: If the daemon is not runningDaemonStalePidException
: If the daemon could not be reached and thePID file appears to be stale.
DaemonTimeoutException
: If the daemon connection timed outDaemonException
: For any other (unknown) reasonThe functionality that checks for a stale PID file is moved from the
aiida.cmdline.utils.daemon
module to theDaemonClient
. This way itis not just used through
verdi daemon
but also through the client.The locations where the automatic cleaning of stale PID files has also
been changed. Summary of behavior changes:
verdi daemon status
andverdi status
no longer delete stale PID fileDaemonClient.start_daemon
is now the only place that deletes themThe
verdi status
andverdi daemon status
commands were calling thefunction
aiida.cmdline.utils.daemon.delete_stale_pid_file
which wouldcheck the daemon PID file and delete it if it was considered stale. This
was added to prevent users having to clean up manually if the daemon got
killed abruptly, leaving behind an orphaned PID file. However, this is a
side-effect that users wouldn't expect from a command that should simply
report the status of a service.
The
aiida.cmdline.utils.daemon.delete_stale_pid_file
function has beenremoved and its functionality has been refactored and moved to the
DaemonClient._check_pid_file
method. It raises if the PID file islikely to be stale. This allows it to be wrapped by
_is_pid_file_stale
which returns a boolean, and
_clean_potentially_stale_pid_file
whichreplaces the old functionality of automatically deleting the PID file if
it is stale and logs a warning of having done so.
The
daemon.timeout
option default is changed from 20 to 5 seconds.There is no reason why a functioning daemon wouldn't be able to respond
within 5 seconds, and forcing a user to wait 20 seconds in case of a
broken daemon is frustratingly long.
verdi process list: Simplify the daemon load implementation
The calculation of the daemon load at the end of the command was using a
utility function that relied manually parsed the response of the daemon
client. However, the
DaemonClient.get_numprocesses
now does this auto-matically and
aiida.cmdline.commands.cmd_daemon.execute_client_command
converts this into the correct formatting for stdout.
The
CalculationQueryBuilder
is replaced with a query using theQueryBuilder
directly as we only need the count and with the former weare forced to define a projection and retrieve the results from the
database to count them in Python, which is more expensive an operation.