Debian Codegraph @

cylc/flow/run_modes/skip.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 """Utilities supporting skip modes
17 """
18 from logging import INFO
19 from typing import (
20 TYPE_CHECKING,
21 Dict,
22 List,
23 Literal,
24 Optional,
25 Set,
26 Tuple,
27 )
28
29 from cylc.flow import LOG
30 from cylc.flow.exceptions import WorkflowConfigError
31 from cylc.flow.run_modes import RunMode
32 from cylc.flow.task_outputs import (
33 TASK_OUTPUT_FAILED,
34 TASK_OUTPUT_STARTED,
35 TASK_OUTPUT_SUBMITTED,
36 TASK_OUTPUT_SUCCEEDED,
37 )
38 from cylc.flow.util import serialise_set
39
40
41 if TYPE_CHECKING:
42 from cylc.flow.task_job_mgr import TaskJobManager
43 from cylc.flow.task_proxy import TaskProxy
44 from cylc.flow.taskdef import TaskDef
45
46
47 def submit_task_job(
48 task_job_mgr: 'TaskJobManager',
49 itask: 'TaskProxy',
50 rtconfig: Dict,
51 now: Tuple[float, str]
52 ) -> 'Literal[True]':
53 """Submit a task in skip mode.
54
55 Returns:
56 True - indicating that TaskJobManager need take no further action.
57 """
58 task_job_mgr._set_retry_timers(itask, rtconfig)
59 itask.summary['started_time'] = now[0]
60 itask.waiting_on_job_prep = False
61 itask.submit_num += 1
62
63 itask.platform = {
64 'name': RunMode.SKIP.value,
65 'install target': 'localhost',
66 'hosts': ['localhost'],
67 'disable task event handlers':
68 rtconfig['skip']['disable task event handlers'],
69 'execution polling intervals': [],
70 'submission retry delays': [],
71 'execution retry delays': []
72 }
73 itask.summary['job_runner_name'] = RunMode.SKIP.value
74 itask.jobs.append(
75 task_job_mgr.get_simulation_job_conf(itask)
76 )
77 itask.run_mode = RunMode.SKIP
78 task_job_mgr.workflow_db_mgr.put_insert_task_jobs(
79 itask, {
80 'time_submit': now[1],
81 'try_num': itask.get_try_num(),
82 'flow_nums': serialise_set(itask.flow_nums),
83 'is_manual_submit': itask.is_manual_submit,
84 'job_runner_name': RunMode.SKIP.value,
85 'platform_name': RunMode.SKIP.value,
86 'submit_status': 0 # Submission has succeeded
87 }
88 )
89 task_job_mgr.workflow_db_mgr.put_update_task_state(itask)
90 for output in sorted(
91 process_outputs(itask, rtconfig),
92 key=itask.state.outputs.output_sort_key,
93 ):
94 task_job_mgr.task_events_mgr.process_message(itask, INFO, output)
95
96 return True
97
98
99 def process_outputs(
100 itask: 'TaskProxy', rtconfig: Optional[dict] = None
101 ) -> Set[str]:
102 """Process Skip Mode Outputs:
103
104 * By default, all required outputs will be generated plus succeeded
105 if success is optional.
106 * The outputs submitted and started are always produced and do not
107 need to be defined in outputs.
108 * If outputs is specified and does not include either
109 succeeded or failed then succeeded will be produced.
110
111 Return:
112 A set of outputs to emit.
113
114 """
115 # Always produce `submitted` & `started` outputs first:
116 result: Set[str] = {TASK_OUTPUT_SUBMITTED, TASK_OUTPUT_STARTED}
117
118 conf_outputs = list(rtconfig['skip']['outputs']) if rtconfig else []
119
120 # Send the rest of our outputs, unless they are succeeded or failed,
121 # which we hold back, to prevent warnings about pre-requisites being
122 # unmet being shown because a "finished" output happens to come first.
123 for message in itask.state.outputs.iter_required_messages(
124 disable=(
125 TASK_OUTPUT_SUCCEEDED
126 if TASK_OUTPUT_FAILED in conf_outputs
127 else TASK_OUTPUT_FAILED
128 )
129 ):
130 trigger = itask.state.outputs._message_to_trigger[message]
131 # Send message unless it be succeeded/failed.
132 if (
133 trigger not in {TASK_OUTPUT_SUCCEEDED, TASK_OUTPUT_FAILED}
134 and (not conf_outputs or trigger in conf_outputs)
135 ):
136 result.add(message)
137
138 # Add optional outputs specified in skip settings:
139 result.update(
140 message
141 for message, trigger in itask.state.outputs._message_to_trigger.items()
142 if trigger in conf_outputs
143 )
144
145 if TASK_OUTPUT_FAILED in conf_outputs:
146 result.add(TASK_OUTPUT_FAILED)
147 else:
148 result.add(TASK_OUTPUT_SUCCEEDED)
149
150 return result
151
152
153 def check_task_skip_config(tdef: 'TaskDef') -> None:
154 """Validate Skip Mode configuration.
155
156 Raises:
157 * Error if outputs include succeeded and failed.
158 """
159 skip_outputs = tdef.rtconfig.get('skip', {}).get('outputs', {})
160 if not skip_outputs:
161 return
162
163 # Error if outputs include succeded and failed:
164 if (
165 TASK_OUTPUT_SUCCEEDED in skip_outputs
166 and TASK_OUTPUT_FAILED in skip_outputs
167 ):
168 raise WorkflowConfigError(
169 f'Skip mode settings for task {tdef.name} has'
170 ' mutually exclusive outputs: succeeded AND failed.')
171
172
173 def skip_mode_validate(taskdefs: 'Dict[str, TaskDef]') -> None:
174 """Warn user if any tasks have "run mode" set to skip.
175 """
176 skip_mode_tasks: List[str] = []
177 for taskdef in taskdefs.values():
178 if (taskdef.rtconfig.get('run mode', None) == RunMode.SKIP.value):
179 skip_mode_tasks.append(taskdef.name)
180
181 # Run any mode specific validation checks:
182 check_task_skip_config(taskdef)
183
184 if skip_mode_tasks:
185 message = 'The following tasks are set to run in skip mode:'
186 for taskname in skip_mode_tasks:
187 message += f'\n * {taskname}'
188 LOG.info(message)