Skip to content

Commit

Permalink
CompResourcesWidget: do not raise when computer test fails (#506)
Browse files Browse the repository at this point in the history
* Execute the test computer function with `subprocess.run` and check the result.
* In case of failure, simply print the output and do not raise an exception.
* Do not test if the computer's name is not provided or the computer doesn't exist.
* Show testing output in the `ipw.HTML` widget instead of the `print` function.

Co-authored-by: Daniel Hollas <[email protected]>
  • Loading branch information
yakutovicha and danielhollas authored Sep 8, 2023
1 parent 96d589d commit 5309f5d
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions aiidalab_widgets_base/computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ def observe_memory_per_machine(change):
self.setup_button.on_click(self.on_setup_computer)
test_button = ipw.Button(description="Test computer")
test_button.on_click(self.test)
self._test_out = ipw.Output(layout=LAYOUT)
self._test_out = ipw.HTML(layout=LAYOUT)

# Organize the widgets
children = [
Expand Down Expand Up @@ -884,14 +884,18 @@ def _configure_computer_local(self, computer: orm.Computer, user: orm.User):

def _run_callbacks_if_computer_exists(self, label):
"""Run things on an existing computer"""
try:
orm.Computer.objects.get(label=label)
if self._computer_exists(label):
for function in self._on_setup_computer_success:
function()
return True
return False

def _computer_exists(self, label):
try:
orm.load_computer(label=label)
except common.NotExistent:
return False
else:
return True
return True

def _validate_computer_settings(self):
if self.label.value == "": # check computer label
Expand Down Expand Up @@ -963,13 +967,31 @@ def on_setup_computer_success(self, function):
self._on_setup_computer_success.append(function)

def test(self, _=None):
with self._test_out:
clear_output()
print(
subprocess.check_output(
["verdi", "computer", "test", "--print-traceback", self.label.value]
).decode("utf-8")
if self.label.value == "":
self._test_out.value = "Please specify the computer name (for AiiDA)."
return False
elif not self._computer_exists(self.label.value):
self._test_out.value = (
f"A computer called <b>{self.label.value}</b> does not exist."
)
return False

self._test_out.value = '<i class="fa fa-spinner fa-pulse"></i>'
process_result = subprocess.run(
["verdi", "computer", "test", "--print-traceback", self.label.value],
capture_output=True,
)

if process_result.returncode == 0:
self._test_out.value = process_result.stdout.decode("utf-8").replace(
"\n", "<br>"
)
return True
else:
self._test_out.value = process_result.stderr.decode("utf-8").replace(
"\n", "<br>"
)
return False

def _reset(self):
self.label.value = ""
Expand Down

0 comments on commit 5309f5d

Please sign in to comment.