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 cancellation token to hooks and pipeline bootstrap #345

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/saturn_engine/worker/executors/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(
self.output = output
self.resources: dict[str, ResourceContext] = {}
self.queue = queue
self.is_cancelled = False

@property
def id(self) -> str:
Expand Down Expand Up @@ -93,6 +94,9 @@ def saturn_context(self) -> t.Iterator[None]:
with job_context(self.queue.definition), message_context(self.message.message):
yield

def cancel(self) -> None:
self.is_cancelled = True


class ExecutableQueue:
def __init__(
Expand Down
6 changes: 6 additions & 0 deletions src/saturn_engine/worker/executors/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
from .executable import ExecutableMessage


class MessageCancelled(Exception):
pass


class ExecutorQueue:
CLOSE_TIMEOUT = datetime.timedelta(seconds=60)

Expand Down Expand Up @@ -68,6 +72,8 @@ async def scope(
xmsg: ExecutableMessage,
) -> PipelineResults:
try:
if xmsg.is_cancelled:
raise MessageCancelled
return await self.executor.process_message(xmsg)
except Exception:
exc_type, exc_value, exc_traceback = sys.exc_info()
Expand Down