Skip to content

Commit

Permalink
Prevent cancelled invocation resetting back to new or ready
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdbeek committed Nov 14, 2023
1 parent 571c511 commit 2a11420
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
17 changes: 17 additions & 0 deletions lib/galaxy/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8216,6 +8216,23 @@ def active(self):
states = WorkflowInvocation.states
return self.state in [states.NEW, states.READY]

def set_state(self, state: "WorkflowInvocation.states"):
session = object_session(self)
priority_states = (WorkflowInvocation.states.CANCELLING, WorkflowInvocation.states.CANCELLED)
if session and self.id and state not in priority_states:
# generate statement that will not revert CANCELLING or CANCELLED back to anything non-terminal
session.execute(
update(WorkflowInvocation.table)
.where(
WorkflowInvocation.id == self.id,
or_(~WorkflowInvocation.state.in_(priority_states), WorkflowInvocation.state.is_(None)),
)
.values(state=state)
)
else:
# Not bound to a session, or setting cancelling/cancelled
self.state = state

def cancel(self):
if self.state not in [WorkflowInvocation.states.CANCELLING, WorkflowInvocation.states.CANCELLED]:
# No use cancelling workflow again, for all others we may still want to be able to cancel
Expand Down
4 changes: 2 additions & 2 deletions lib/galaxy/workflow/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def invoke(self) -> Dict[int, Any]:
log.debug(
f"Workflow invocation [{workflow_invocation.id}] exceeded maximum number of seconds allowed for scheduling [{maximum_duration}], failing."
)
workflow_invocation.state = model.WorkflowInvocation.states.FAILED
workflow_invocation.set_state(model.WorkflowInvocation.states.FAILED)
# All jobs ran successfully, so we can save now
self.trans.sa_session.add(workflow_invocation)

Expand Down Expand Up @@ -264,7 +264,7 @@ def invoke(self) -> Dict[int, Any]:
state = model.WorkflowInvocation.states.READY
else:
state = model.WorkflowInvocation.states.SCHEDULED
workflow_invocation.state = state
workflow_invocation.set_state(state)

# All jobs ran successfully, so we can save now
self.trans.sa_session.add(workflow_invocation)
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/workflow/scheduling_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def shutdown(self):
raise exception

def queue(self, workflow_invocation, request_params, flush=True):
workflow_invocation.state = model.WorkflowInvocation.states.NEW
workflow_invocation.set_state(model.WorkflowInvocation.states.NEW)
workflow_invocation.scheduler = request_params.get("scheduler", None) or self.default_scheduler_id
sa_session = self.app.model.context
sa_session.add(workflow_invocation)
Expand Down

0 comments on commit 2a11420

Please sign in to comment.