Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ExecutionInterrupt (special exception) #459

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions gufe/protocols/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ class MissingUnitResultError(ProtocolDAGResultError):

class ProtocolUnitFailureError(ProtocolDAGResultError):
"""Error when a ProtocolDAGResult has only ProtocolUnitFailure(s) for a given ProtocolUnit."""


class ExecutionInterrupt(BaseException):
"""Exception for unrecoverable execution failures that are unrelated to user inputs.
Will not be caught by ProtocolUnit.execute().
"""
5 changes: 3 additions & 2 deletions gufe/protocols/protocolunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Any, Dict, List, Optional, Tuple, Union

from ..tokenization import TOKENIZABLE_REGISTRY, GufeKey, GufeTokenizable
from .errors import ExecutionInterrupt


@dataclass
Expand Down Expand Up @@ -328,8 +329,8 @@ def execute(
end_time=datetime.datetime.now(),
)

except KeyboardInterrupt:
# if we "fail" due to a KeyboardInterrupt, we always want to raise
except (KeyboardInterrupt, ExecutionInterrupt):
# we always want to raise in these situations
atravitz marked this conversation as resolved.
Show resolved Hide resolved
raise
except Exception as e:
if raise_error:
Expand Down
31 changes: 31 additions & 0 deletions gufe/tests/test_protocolunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from gufe.protocols.errors import ExecutionInterrupt
from gufe.protocols.protocolunit import Context, ProtocolUnit, ProtocolUnitFailure, ProtocolUnitResult
from gufe.tests.test_tokenization import GufeTokenizableTestsMixin

Expand All @@ -27,6 +28,16 @@ def _execute(ctx: Context, an_input=2, **inputs):
return {"foo": "bar"}


class DummyExecutionInterruptUnit(ProtocolUnit):
@staticmethod
def _execute(ctx: Context, an_input=2, **inputs):

if an_input != 2:
raise ExecutionInterrupt

return {"foo": "bar"}


@pytest.fixture
def dummy_unit():
return DummyUnit(name="qux")
Expand Down Expand Up @@ -77,6 +88,26 @@ def test_execute(self, tmpdir):
with pytest.raises(ValueError, match="should always be 2"):
unit.execute(context=ctx, raise_error=True, an_input=3)

def test_execute_ExecutionInterrupt(self, tmpdir):
with tmpdir.as_cwd():

unit = DummyExecutionInterruptUnit()

shared = Path("shared") / str(unit.key)
shared.mkdir(parents=True)

scratch = Path("scratch") / str(unit.key)
scratch.mkdir(parents=True)

ctx = Context(shared=shared, scratch=scratch)

with pytest.raises(ExecutionInterrupt):
unit.execute(context=ctx, an_input=3)

u: ProtocolUnitResult = unit.execute(context=ctx, an_input=2)

assert u.outputs == {"foo": "bar"}

def test_execute_KeyboardInterrupt(self, tmpdir):
with tmpdir.as_cwd():

Expand Down
23 changes: 23 additions & 0 deletions news/add_execution_interrupt.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added ExecutionInterrupt, a special exception that does not get handled as a ``ProtocolUnitFailure``.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
Loading