Skip to content

Commit

Permalink
Remove SkipNSteps Exception (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: Néstor Pérez <[email protected]>
  • Loading branch information
contrains and prryplatypus authored Dec 2, 2024
1 parent 96b9d1c commit 8dbc4d6
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 60 deletions.
4 changes: 1 addition & 3 deletions docs/docs/basics/manual-step-ordering.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# Using workflow step names for manual ordering

Workflow steps may be manually ordered and redirected by use of `GoToEnd`, `GoToStep` and `SkipNSteps` exceptions.
Workflow steps may be manually ordered and redirected by use of `GoToEnd` and `GoToStep` exceptions.

Workflow ordering can be preempted and redirected by raising the `GoToStep` exception, passing the workflow step
function along with the optional return value.

Workflows may also be advanced directly to completion by raising the `GoToEnd` exception.

Workflows may also be advanced by a set number of steps by raising the `SkipNSteps` exception.

## Defining manual workflow order

The following workflow contains five steps in total, which are executed in a specific order as directed by the
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/basics/workflow-path-hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ completion percentage and total steps.
This logic may be enabled by specifying the `paths=` kwarg for a given `@workflow.step()` decorator.

* It is not necessary to specify `paths` for workflow steps which only return a value via `return`.
* It is suggested to define `paths` for steps which utilise the `SkipNSteps` or `GoToEnd` exceptions.
* It is suggested to define `paths` for steps which utilise the `GoToEnd` exceptions.
* It is recommended to define `paths` for steps which utilise the `GoToStep` exception, or which have branching logic with multiple potential paths.

`paths` may be freely defined alongside other kwargs in the `@workflow.step` decorator.
Expand Down
5 changes: 1 addition & 4 deletions ergate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
GoToStep,
InvalidDefinitionError,
ReverseGoToError,
SkipNSteps,
UnknownStepError,
ValidationError,
)
from .job import Job
from .job_status import JobStatus
from .paths import GoToEndPath, GoToStepPath, NextStepPath, SkipNStepsPath
from .paths import GoToEndPath, GoToStepPath, NextStepPath
from .queue import QueueProtocol
from .state_store import StateStoreProtocol
from .workflow import Workflow, WorkflowStep
Expand All @@ -35,8 +34,6 @@
"NextStepPath",
"QueueProtocol",
"ReverseGoToError",
"SkipNSteps",
"SkipNStepsPath",
"StateStoreProtocol",
"UnknownStepError",
"ValidationError",
Expand Down
8 changes: 0 additions & 8 deletions ergate/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,3 @@ class GoToStep(ErgateError): # noqa: N818
def __init__(self, step: WorkflowStep, *, retval: Any = None) -> None:
self.retval = retval
self.step = step


class SkipNSteps(ErgateError): # noqa: N818
"""Raised from a step to skip N steps."""

def __init__(self, n: int, retval: Any = None) -> None:
self.n = n
self.retval = retval
14 changes: 1 addition & 13 deletions ergate/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,7 @@ def mark_step_n_completed(
return_value: Any,
total_steps: int,
) -> None:
self.mark_n_steps_completed(
n - self.current_step,
return_value,
total_steps,
)

def mark_n_steps_completed(
self,
n: int,
return_value: Any,
total_steps: int,
) -> None:
self.current_step += n
self.current_step = n
self.steps_completed = min(self.steps_completed + 1, total_steps)
self.percent_completed = float((self.steps_completed / total_steps) * 100)
self.status = (
Expand Down
24 changes: 4 additions & 20 deletions ergate/job_runner.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Generic, TypeVar

from .exceptions import AbortJob, GoToEnd, GoToStep, ReverseGoToError, SkipNSteps
from .exceptions import AbortJob, GoToEnd, GoToStep, ReverseGoToError
from .handler import ErrorHookHandler
from .interrupt import DelayedKeyboardInterrupt
from .job import Job
from .log import LOG
from .paths import GoToStepPath, NextStepPath, SkipNStepsPath
from .paths import GoToStepPath, NextStepPath
from .queue import QueueProtocol
from .state_store import StateStoreProtocol
from .workflow_registry import WorkflowRegistry
Expand Down Expand Up @@ -83,22 +83,6 @@ def _run_job(self, job: JobType) -> None:
job.mark_step_n_completed(
exc.step.index, exc.retval, job.steps_completed + remaining_steps
)
except SkipNSteps as exc:
LOG.info("User requested to skip %d steps", exc.n)

remaining_steps = max(
(
len(path)
for path in paths
if isinstance(path[0][0], SkipNStepsPath)
and path[0][0].n == exc.n
),
default=len(workflow) - job.current_step,
)

job.mark_n_steps_completed(
exc.n + 1, exc.retval, job.steps_completed + remaining_steps
)
except Exception as exc:
# Since `except GoToStep` potentially raises an exception, the logic
# for handling exceptions had to be moved to a higher scope.
Expand All @@ -115,8 +99,8 @@ def _run_job(self, job: JobType) -> None:
default=len(workflow) - job.current_step + 1,
)

job.mark_n_steps_completed(
1, retval, job.steps_completed + remaining_steps
job.mark_step_n_completed(
job.current_step + 1, retval, job.steps_completed + remaining_steps
)
except Exception as exc:
LOG.exception("Job raised an exception")
Expand Down
7 changes: 0 additions & 7 deletions ergate/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,3 @@ def __init__(self, step_name: str) -> None:

class NextStepPath(WorkflowPath):
"""WorkflowPath class for the default `return` from function."""


class SkipNStepsPath(WorkflowPath):
"""WorkflowPath class for the `SkipNSteps` exception."""

def __init__(self, n: int) -> None:
self.n = n
5 changes: 1 addition & 4 deletions ergate/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)

from .exceptions import ReverseGoToError, UnknownStepError
from .paths import GoToEndPath, GoToStepPath, NextStepPath, SkipNStepsPath, WorkflowPath
from .paths import GoToEndPath, GoToStepPath, NextStepPath, WorkflowPath
from .workflow_step import WorkflowStep

CallableSpec = ParamSpec("CallableSpec")
Expand Down Expand Up @@ -103,9 +103,6 @@ def _find_next_step(self, index: int, path: WorkflowPath) -> int:
if isinstance(path, GoToStepPath):
return self.get_step_index_by_name(path.step_name)

if isinstance(path, SkipNStepsPath):
return index + 1 + path.n

return index + 1

def get_step_index_by_name(self, step_name: str) -> int:
Expand Down

0 comments on commit 8dbc4d6

Please sign in to comment.