Debian Codegraph @

cylc/flow/run_modes/__init__.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from enum import Enum
18 from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple
19
20 if TYPE_CHECKING:
21 from optparse import Values
22 from cylc.flow.task_job_mgr import TaskJobManager
23 from cylc.flow.task_proxy import TaskProxy
24
25 # The interface for submitting jobs
26 SubmissionInterface = Callable[
27 [ # Args:
28 # the task job manager instance
29 'TaskJobManager',
30 # the task to submit
31 'TaskProxy',
32 # the task's runtime config (with broadcasts applied)
33 Dict[str, Any],
34 # the current time as (float_unix_time, str_ISO8601)
35 Tuple[float, str]
36 ],
37 # Return False if the job requires live-mode submission
38 # (dummy mode does this), else return True.
39 bool
40 ]
41
42
43 class RunMode(Enum):
44 """The possible run modes of a task/workflow."""
45
46 LIVE = 'live'
47 """Tasks will submit their configured jobs."""
48
49 SIMULATION = 'simulation'
50 """Simulates job submission with configurable exection time
51 and succeeded/failed outcomes (but does not submit real jobs)."""
52
53 DUMMY = 'dummy'
54 """Submits real jobs with empty scripts."""
55
56 SKIP = 'skip'
57 """Skips job submission; sets required outputs (by default) or
58 configured outputs."""
59
60 def describe(self):
61 """Return user friendly description of run mode.
62
63 For use by configuration spec documenter.
64 """
65 if self == self.LIVE:
66 return "Task will submit their configured jobs."
67 if self == self.SKIP:
68 return (
69 "Skips job submission; sets required outputs"
70 " (by default) or configured outputs.")
71 if self == self.DUMMY:
72 return "Submits real jobs with empty scripts."
73
74 # self == self.SIMULATION:
75 return (
76 "Simulates job submission with configurable"
77 " exection time and succeeded/failed outcomes"
78 " (but does not submit real jobs).")
79
80 @staticmethod
81 def get(options: 'Values') -> "RunMode":
82 """Return the workflow run mode from the options."""
83 run_mode = getattr(options, 'run_mode', None)
84 if run_mode:
85 return RunMode(run_mode)
86 return RunMode.LIVE
87
88 @classmethod
89 def _missing_(cls, value):
90 value = value.lower()
91 for member in cls:
92 if member.value.lower() == value:
93 return member
94 return None
95
96 def get_submit_method(self) -> 'Optional[SubmissionInterface]':
97 """Return the job submission method for this run mode.
98
99 This returns None for live-mode jobs as these use a
100 different code pathway for job submission.
101 """
102 submit_task_job: 'Optional[SubmissionInterface]' = None
103 if self == RunMode.DUMMY:
104 from cylc.flow.run_modes.dummy import submit_task_job
105 elif self == RunMode.SIMULATION:
106 from cylc.flow.run_modes.simulation import submit_task_job
107 elif self == RunMode.SKIP:
108 from cylc.flow.run_modes.skip import submit_task_job
109 return submit_task_job
110
111
112 def disable_task_event_handlers(itask: 'TaskProxy'):
113 """Should we disable event handlers for this task?
114
115 No event handlers in simulation mode, or in skip mode
116 if we don't deliberately enable them:
117 """
118 mode = itask.run_mode
119 return (
120 mode == RunMode.SIMULATION
121 or (
122 mode == RunMode.SKIP
123 and itask.platform.get(
124 'disable task event handlers', False)
125 )
126 )
127
128
129 # Modes available for running a whole workflow:
130 WORKFLOW_RUN_MODES = frozenset(i.value for i in {
131 RunMode.LIVE, RunMode.DUMMY, RunMode.SIMULATION})
132
133 # Modes which can be set in task config:
134 TASK_CONFIG_RUN_MODES = frozenset(
135 i.value for i in (RunMode.LIVE, RunMode.SKIP))
136 # And those only available to the workflow:
137 WORKFLOW_ONLY_MODES = frozenset(
138 i.value for i in RunMode) - TASK_CONFIG_RUN_MODES
139
140 # Modes which completely ignore the standard submission path:
141 JOBLESS_MODES = frozenset(i.value for i in {
142 RunMode.SKIP, RunMode.SIMULATION})