| 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 typing import Dict, Optional, Tuple, Any |
| 18 |
import asyncio |
| 19 |
from inspect import signature |
| 20 |
|
| 21 |
from cylc.flow.scripts.workflow_state import WorkflowPoller |
| 22 |
from cylc.flow.id import tokenise |
| 23 |
from cylc.flow.exceptions import WorkflowConfigError, InputError |
| 24 |
from cylc.flow.task_state import TASK_STATUS_SUCCEEDED |
| 25 |
from cylc.flow.dbstatecheck import check_polling_config |
| 26 |
|
| 27 |
|
| 28 |
DEFAULT_STATUS = TASK_STATUS_SUCCEEDED |
| 29 |
|
| 30 |
|
| 31 |
def workflow_state( |
| 32 |
workflow_task_id: str, |
| 33 |
offset: Optional[str] = None, |
| 34 |
flow_num: Optional[int] = None, |
| 35 |
is_trigger: bool = False, |
| 36 |
is_message: bool = False, |
| 37 |
alt_cylc_run_dir: Optional[str] = None, |
| 38 |
) -> Tuple[bool, Dict[str, Any]]: |
| 39 |
"""Connect to a workflow DB and check a task status or output. |
| 40 |
|
| 41 |
If the status or output has been achieved, return {True, result}. |
| 42 |
|
| 43 |
Args: |
| 44 |
workflow_task_id: |
| 45 |
ID (workflow//point/task:selector) of the target task. |
| 46 |
offset: |
| 47 |
Offset from cycle point as an ISO8601 or integer duration, |
| 48 |
e.g. PT1H (1 hour) or P1 (1 integer cycle) |
| 49 |
flow_num: |
| 50 |
Flow number of the target task. |
| 51 |
is_trigger: |
| 52 |
Interpret the task:selector as a task trigger name rather than a |
| 53 |
task status. |
| 54 |
is_message: |
| 55 |
Interpret the task:selector as a task output message rather than a |
| 56 |
task status. |
| 57 |
alt_cylc_run_dir: |
| 58 |
Alternate cylc-run directory, e.g. for another user. |
| 59 |
|
| 60 |
Returns: |
| 61 |
tuple: (satisfied, result) |
| 62 |
|
| 63 |
satisfied: |
| 64 |
True if ``satisfied`` else ``False``. |
| 65 |
result: |
| 66 |
Dict containing the keys: |
| 67 |
|
| 68 |
* ``workflow`` |
| 69 |
* ``task`` |
| 70 |
* ``point`` |
| 71 |
* ``offset`` |
| 72 |
* ``status`` |
| 73 |
* ``message`` |
| 74 |
* ``trigger`` |
| 75 |
* ``flow_num`` |
| 76 |
* ``run_dir`` |
| 77 |
|
| 78 |
.. versionchanged:: 8.3.0 |
| 79 |
|
| 80 |
The ``workflow_task_id`` argument was introduced to replace the |
| 81 |
separate ``workflow``, ``point``, ``task``, ``status``, and ``message`` |
| 82 |
arguments (which are still supported for backwards compatibility). |
| 83 |
The ``flow_num`` argument was added. The ``cylc_run_dir`` argument |
| 84 |
was renamed to ``alt_cylc_run_dir``. |
| 85 |
""" |
| 86 |
poller = WorkflowPoller( |
| 87 |
workflow_task_id, |
| 88 |
offset, |
| 89 |
flow_num, |
| 90 |
alt_cylc_run_dir, |
| 91 |
DEFAULT_STATUS, |
| 92 |
is_trigger, is_message, |
| 93 |
old_format=False, |
| 94 |
condition=workflow_task_id, |
| 95 |
max_polls=1, # (for xtriggers the scheduler does the polling) |
| 96 |
interval=0, # irrelevant for 1 poll |
| 97 |
args=[] |
| 98 |
) |
| 99 |
|
| 100 |
# NOTE the results dict item names remain compatible with older usage. |
| 101 |
|
| 102 |
if asyncio.run(poller.poll()): |
| 103 |
results = { |
| 104 |
'workflow': poller.workflow_id, |
| 105 |
'task': poller.task, |
| 106 |
'point': poller.cycle, |
| 107 |
} |
| 108 |
if poller.alt_cylc_run_dir is not None: |
| 109 |
results['cylc_run_dir'] = poller.alt_cylc_run_dir |
| 110 |
|
| 111 |
if offset is not None: |
| 112 |
results['offset'] = poller.offset |
| 113 |
|
| 114 |
if flow_num is not None: |
| 115 |
results["flow_num"] = poller.flow_num |
| 116 |
|
| 117 |
if poller.is_message: |
| 118 |
results['message'] = poller.selector |
| 119 |
elif poller.is_trigger: |
| 120 |
results['trigger'] = poller.selector |
| 121 |
else: |
| 122 |
results['status'] = poller.selector |
| 123 |
|
| 124 |
return (True, results) |
| 125 |
else: |
| 126 |
return (False, {}) |
| 127 |
|
| 128 |
|
| 129 |
def validate(args: Dict[str, Any]): |
| 130 |
"""Validate workflow_state xtrigger function args. |
| 131 |
|
| 132 |
Arguments: |
| 133 |
workflow_task_id: |
| 134 |
full workflow//cycle/task[:selector] |
| 135 |
offset: |
| 136 |
must be a valid status |
| 137 |
flow_num: |
| 138 |
must be an integer |
| 139 |
alt_cylc_run_dir: |
| 140 |
must be a valid path |
| 141 |
|
| 142 |
""" |
| 143 |
tokens = tokenise(args["workflow_task_id"]) |
| 144 |
|
| 145 |
if any( |
| 146 |
tokens[token] is None |
| 147 |
for token in ("workflow", "cycle", "task") |
| 148 |
): |
| 149 |
raise WorkflowConfigError( |
| 150 |
"Full ID needed: workflow//cycle/task[:selector].") |
| 151 |
|
| 152 |
if ( |
| 153 |
args["flow_num"] is not None and |
| 154 |
not isinstance(args["flow_num"], int) |
| 155 |
): |
| 156 |
raise WorkflowConfigError("flow_num must be an integer if given.") |
| 157 |
|
| 158 |
try: |
| 159 |
check_polling_config( |
| 160 |
tokens['cycle_sel'] or tokens['task_sel'] or DEFAULT_STATUS, |
| 161 |
args['is_trigger'], |
| 162 |
args['is_message'], |
| 163 |
) |
| 164 |
except InputError as exc: |
| 165 |
raise WorkflowConfigError(str(exc)) from None |
| 166 |
|
| 167 |
|
| 168 |
# BACK COMPAT: workflow_state_backcompat |
| 169 |
# from: 8.0.0 |
| 170 |
# to: 8.3.0 |
| 171 |
# remove at: 8.x |
| 172 |
def _workflow_state_backcompat( |
| 173 |
workflow: str, |
| 174 |
task: str, |
| 175 |
point: str, |
| 176 |
offset: Optional[str] = None, |
| 177 |
status: str = 'succeeded', |
| 178 |
message: Optional[str] = None, |
| 179 |
cylc_run_dir: Optional[str] = None |
| 180 |
) -> Tuple[bool, Optional[Dict[str, Optional[str]]]]: |
| 181 |
"""Back-compat wrapper for the workflow_state xtrigger. |
| 182 |
|
| 183 |
Note Cylc 7 DBs only stored custom task outputs, not standard ones. |
| 184 |
|
| 185 |
Arguments: |
| 186 |
workflow: |
| 187 |
The workflow to interrogate. |
| 188 |
task: |
| 189 |
The name of the task to query. |
| 190 |
point: |
| 191 |
The cycle point. |
| 192 |
offset: |
| 193 |
The offset between the cycle this xtrigger is used in and the one |
| 194 |
it is querying for as an ISO8601 time duration. |
| 195 |
e.g. PT1H (one hour). |
| 196 |
status: |
| 197 |
The task status required for this xtrigger to be satisfied. |
| 198 |
message: |
| 199 |
The custom task output required for this xtrigger to be satisfied. |
| 200 |
|
| 201 |
.. note:: |
| 202 |
|
| 203 |
This cannot be specified in conjunction with ``status``. |
| 204 |
|
| 205 |
cylc_run_dir: |
| 206 |
Alternate cylc-run directory, e.g. for another user. |
| 207 |
|
| 208 |
Returns: |
| 209 |
tuple: (satisfied, results) |
| 210 |
|
| 211 |
satisfied: |
| 212 |
True if ``satisfied`` else ``False``. |
| 213 |
results: |
| 214 |
Dictionary containing the args / kwargs which were provided |
| 215 |
to this xtrigger. |
| 216 |
|
| 217 |
""" |
| 218 |
args = { |
| 219 |
'workflow': workflow, |
| 220 |
'task': task, |
| 221 |
'point': point, |
| 222 |
'offset': offset, |
| 223 |
'status': status, |
| 224 |
'message': message, |
| 225 |
'cylc_run_dir': cylc_run_dir |
| 226 |
} |
| 227 |
upg_args = _upgrade_workflow_state_sig(args) |
| 228 |
satisfied, _results = workflow_state(**upg_args) |
| 229 |
|
| 230 |
return (satisfied, args) |
| 231 |
|
| 232 |
|
| 233 |
# BACK COMPAT: workflow_state_backcompat |
| 234 |
# from: 8.0.0 |
| 235 |
# to: 8.3.0 |
| 236 |
# remove at: 8.x |
| 237 |
def _upgrade_workflow_state_sig(args: Dict[str, Any]) -> Dict[str, Any]: |
| 238 |
"""Return upgraded args for workflow_state, given the deprecated args.""" |
| 239 |
is_message = False |
| 240 |
workflow_task_id = f"{args['workflow']}//{args['point']}/{args['task']}" |
| 241 |
status = args.get('status') |
| 242 |
message = args.get('message') |
| 243 |
if status is not None: |
| 244 |
workflow_task_id += f":{status}" |
| 245 |
elif message is not None: |
| 246 |
is_message = True |
| 247 |
workflow_task_id += f":{message}" |
| 248 |
return { |
| 249 |
'workflow_task_id': workflow_task_id, |
| 250 |
'offset': args.get('offset'), |
| 251 |
'alt_cylc_run_dir': args.get('cylc_run_dir'), |
| 252 |
'is_message': is_message, |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
# BACK COMPAT: workflow_state_backcompat |
| 257 |
# from: 8.0.0 |
| 258 |
# to: 8.3.0 |
| 259 |
# remove at: 8.x |
| 260 |
def _validate_backcompat(args: Dict[str, Any]): |
| 261 |
"""Validate old workflow_state xtrigger function args. |
| 262 |
""" |
| 263 |
bound_args = signature(workflow_state).bind( |
| 264 |
**_upgrade_workflow_state_sig(args) |
| 265 |
) |
| 266 |
bound_args.apply_defaults() |
| 267 |
validate(bound_args.arguments) |