Skip to content

Commit

Permalink
Schema: use Enum for task run mode instead of String (#61)
Browse files Browse the repository at this point in the history
* Schema: use `Enum` for task run mode instead of `String`

* Tidy
  • Loading branch information
MetRonnie authored and wxtim committed Jul 22, 2024
1 parent 72f2600 commit 60ae7a3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
59 changes: 50 additions & 9 deletions cylc/flow/network/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
from cylc.flow.workflow_status import StopMode

if TYPE_CHECKING:
from enum import Enum
from graphql import ResolveInfo
from graphql.type.definition import (
GraphQLNamedType,
Expand Down Expand Up @@ -596,6 +597,46 @@ class Meta:
string_extended = String()


def describe_run_mode(run_mode: Optional['Enum']) -> str:
"""Returns description for a workflow/task run mode."""
if not run_mode:
return ""
mode = run_mode.value
if mode == RunMode.WORKFLOW:
return "Default to the workflow's run mode."
if mode == RunMode.LIVE:
return "Tasks will run normally."
if mode == RunMode.SIMULATION:
return (
"Simulates job submission with configurable execution time "
"(does not submit real jobs)."
)
if mode == RunMode.DUMMY:
return "Submits real jobs with empty scripts."
if mode == RunMode.SKIP:
return (
"Skips job submission; sets required outputs (by default) or "
"configured outputs."
)
return ""


WorkflowRunMode = graphene.Enum(
'WorkflowRunMode',
[(m.capitalize(), m) for m in RunMode.WORKFLOW_MODES],
description=describe_run_mode,
)
"""The run mode for the workflow."""


TaskRunMode = graphene.Enum(
'TaskRunMode',
[(m.capitalize(), m) for m in (RunMode.WORKFLOW, *RunMode.WORKFLOW_MODES)],
description=describe_run_mode,
)
"""The run mode for tasks."""


class Workflow(ObjectType):
class Meta:
description = """Global workflow info."""
Expand Down Expand Up @@ -822,7 +863,7 @@ class Meta:
directives = graphene.List(RuntimeSetting, resolver=resolve_json_dump)
environment = graphene.List(RuntimeSetting, resolver=resolve_json_dump)
outputs = graphene.List(RuntimeSetting, resolver=resolve_json_dump)
run_mode = graphene.String(default_value=RunMode.WORKFLOW)
run_mode = TaskRunMode(default_value=TaskRunMode.Workflow.name)


RUNTIME_FIELD_TO_CFG_MAP = {
Expand Down Expand Up @@ -1501,9 +1542,9 @@ class RuntimeConfiguration(String):


class BroadcastMode(graphene.Enum):
Set = 'put_broadcast'
Clear = 'clear_broadcast'
Expire = 'expire_broadcast'
Set = cast('Enum', 'put_broadcast')
Clear = cast('Enum', 'clear_broadcast')
Expire = cast('Enum', 'expire_broadcast')

@property
def description(self):
Expand Down Expand Up @@ -1628,10 +1669,10 @@ class WorkflowStopMode(graphene.Enum):
# * Graphene requires special enums.
# * We only want to offer a subset of stop modes (REQUEST_* only).

Clean = StopMode.REQUEST_CLEAN.value # type: graphene.Enum
Kill = StopMode.REQUEST_KILL.value # type: graphene.Enum
Now = StopMode.REQUEST_NOW.value # type: graphene.Enum
NowNow = StopMode.REQUEST_NOW_NOW.value # type: graphene.Enum
Clean = cast('Enum', StopMode.REQUEST_CLEAN.value)
Kill = cast('Enum', StopMode.REQUEST_KILL.value)
Now = cast('Enum', StopMode.REQUEST_NOW.value)
NowNow = cast('Enum', StopMode.REQUEST_NOW_NOW.value)

@property
def description(self):
Expand Down Expand Up @@ -1688,7 +1729,7 @@ class Arguments:
mode = BroadcastMode(
# use the enum name as the default value
# https://github.com/graphql-python/graphql-core-legacy/issues/166
default_value=BroadcastMode.Set.name, # type: ignore
default_value=BroadcastMode.Set.name,
description='What type of broadcast is this?',
required=True
)
Expand Down
13 changes: 6 additions & 7 deletions cylc/flow/task_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,23 @@ class RunMode:
WORKFLOW = 'workflow'
"""Default to workflow run mode"""

MODES = {LIVE, SIMULATION, DUMMY, SKIP, WORKFLOW}
MODES = frozenset({LIVE, SIMULATION, DUMMY, SKIP, WORKFLOW})

WORKFLOW_MODES = [LIVE, DUMMY, SIMULATION, SKIP]
WORKFLOW_MODES = (LIVE, DUMMY, SIMULATION, SKIP)
"""Workflow mode not sensible mode for workflow.
n.b. converted to a list to ensure ordering doesn't change in
CLI
n.b. not using a set to ensure ordering in CLI
"""

JOB_MODES = {LIVE, DUMMY}
JOB_MODES = frozenset({LIVE, DUMMY})
"""Modes which need to have real jobs submitted."""

JOBLESS_MODES = {SKIP, SIMULATION}
JOBLESS_MODES = frozenset({SKIP, SIMULATION})
"""Modes which completely ignore the standard submission path."""

@staticmethod
def get(options: 'Values') -> str:
"""Return the run mode from the options."""
"""Return the workflow run mode from the options."""
return getattr(options, 'run_mode', None) or RunMode.LIVE

@staticmethod
Expand Down

0 comments on commit 60ae7a3

Please sign in to comment.