| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 4 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
"""cylc set [OPTIONS] ARGS |
| 20 |
|
| 21 |
Manually complete task outputs, and satisfy task and xtrigger prerequisites. |
| 22 |
|
| 23 |
By default this command completes required outputs, plus the "submitted", |
| 24 |
"started", and "succeeded" outputs even if they are optional. |
| 25 |
|
| 26 |
Setting prerequisites promotes target tasks to the n=0 active window; setting |
| 27 |
outputs also sets the prerequisites of any tasks that depend on those outputs. |
| 28 |
|
| 29 |
Note: state selectors such as ":failed" determine which tasks to target, not |
| 30 |
the outputs to set - see examples below. |
| 31 |
|
| 32 |
Note: see `cylc trigger` command help if you want rerun a sub-graph of tasks. |
| 33 |
|
| 34 |
Setting Outputs: |
| 35 |
Completing outputs may affect task state, and it will satisfy prerequisites |
| 36 |
of any other tasks that depend on those outputs. |
| 37 |
|
| 38 |
Format: |
| 39 |
* --out=<output> # output name (not message) of the target task |
| 40 |
|
| 41 |
Completing a final output ("succeeded", "failed", or "expired") sets the |
| 42 |
state of the target task accordingly. Completing "started", "submitted", |
| 43 |
or custom outputs does not affect state because no job is actually running. |
| 44 |
|
| 45 |
Implied outputs are completed automatically: |
| 46 |
- "started" implies "submitted" |
| 47 |
- "succeeded" and "failed" imply "started" |
| 48 |
- custom outputs and "expired" do not imply other outputs |
| 49 |
|
| 50 |
For custom outputs, use the output name, not the associated task message. |
| 51 |
In [runtime][my-task][outputs]: |
| 52 |
# <name> = <message> |
| 53 |
x = "file x completed" |
| 54 |
|
| 55 |
Setting Task Prerequisites: |
| 56 |
Satisfying a task's prerequisite contributes to its readiness to run. |
| 57 |
|
| 58 |
Satisfying any prerequisite of an inactive task promotes it to the active |
| 59 |
window where xtrigger checking commences (if the task has any xtriggers). |
| 60 |
The --pre=all option promotes even parentless tasks to the active window. |
| 61 |
|
| 62 |
Format: |
| 63 |
* --pre=<cycle>/<task-name>[:output] # satisfy a single prerequisite |
| 64 |
* --pre=all # satisfy all task (not xtrigger) prerequisites |
| 65 |
|
| 66 |
Setting Xtrigger Prerequisites: |
| 67 |
To satisfy an xtrigger prerequisite use --pre with the word "xtrigger" in |
| 68 |
place of the cycle point, and the xtrigger name in place of the task name. |
| 69 |
|
| 70 |
Format: |
| 71 |
* --pre=xtrigger/<xtrigger-name> # satisfy one xtrigger prerequisite |
| 72 |
* --pre=xtrigger/all # satisfy all xtrigger prerequisites |
| 73 |
|
| 74 |
(The full format is "xtrigger/<xtrigger-name>[:succeeded]" but "succeeded" |
| 75 |
is the only valid xtrigger output and the default, so it can be omitted.) |
| 76 |
|
| 77 |
Flow numbers of tasks affected by the set command are determined as follows |
| 78 |
Active tasks (n=0) already have existing flow numbers. |
| 79 |
* default: merge existing active flow numbers |
| 80 |
* --flow=INT or "new": merge existing and new flow numbers |
| 81 |
* --flow="none": ERROR (not valid for already-active tasks) |
| 82 |
Inactive tasks (n>0) do not have flow numbers assigned: |
| 83 |
* default: assign all active flow numbers |
| 84 |
* --flow=INT or "new": assign the given flow numbers |
| 85 |
* --flow="none": action the command with no flow numbers, which may result |
| 86 |
in a no-flow task running (activity will not flow on downstream of it). |
| 87 |
|
| 88 |
CLI Completion: |
| 89 |
Cylc can auto-complete prerequisite and output names for tasks in the n=0 |
| 90 |
window, if you type the task name before attempting TAB-completion. |
| 91 |
|
| 92 |
Examples: |
| 93 |
# complete all required outputs of 3/bar: |
| 94 |
$ cylc set my_workflow//3/bar |
| 95 |
# or: |
| 96 |
$ cylc set --out=required my_workflow//3/bar |
| 97 |
|
| 98 |
# complete the succeeded output of 3/bar: |
| 99 |
$ cylc set --out=succeeded my_workflow//3/bar |
| 100 |
|
| 101 |
# complete the outputs defined in [runtime][task][skip] |
| 102 |
$ cylc set --out=skip my_workflow//3/bar |
| 103 |
|
| 104 |
# satisfy the 3/foo:succeeded prerequisite of 3/bar: |
| 105 |
$ cylc set --pre=3/foo my_workflow//3/bar # or, |
| 106 |
$ cylc set --pre=3/foo:succeeded my_workflow//3/bar |
| 107 |
|
| 108 |
# satisfy all prerequisites (if any) of 3/bar: |
| 109 |
$ cylc set --pre=all my_workflow//3/bar |
| 110 |
|
| 111 |
# satisfy clock trigger prerequisite @clock1 3000/bar: |
| 112 |
$ cylc set --pre=xtrigger/clock1 my_worklfow//3000/bar |
| 113 |
|
| 114 |
# satisfy the "3/bar:file1" prerequisite of 3/qux: |
| 115 |
$ cylc set --pre=3/bar:file1 my_workflow//3/qux |
| 116 |
|
| 117 |
# complete multiple outputs at once: |
| 118 |
$ cylc set --out=a --out=b,c my_workflow//3/bar |
| 119 |
|
| 120 |
# satisfy multiple prerequisites at once: |
| 121 |
$ cylc set --pre=3/foo:x --pre=3/foo:y,3/foo:z my_workflow//3/bar |
| 122 |
|
| 123 |
# set required outputs of all failed tasks in the n=0 window at cycle 3: |
| 124 |
$ cylc set my_workflow//3/*:failed |
| 125 |
|
| 126 |
# set the succeeded output of all failed tasks in the n=0 window at cycle 3: |
| 127 |
$ cylc set my_workflow//3/*:failed --output=succeeded |
| 128 |
""" |
| 129 |
|
| 130 |
from functools import partial |
| 131 |
import sys |
| 132 |
from typing import TYPE_CHECKING |
| 133 |
|
| 134 |
from cylc.flow.network.client_factory import get_client |
| 135 |
from cylc.flow.network.multi import call_multi |
| 136 |
from cylc.flow.option_parsers import ( |
| 137 |
FULL_ID_MULTI_ARG_DOC, |
| 138 |
CylcOptionParser as COP, |
| 139 |
) |
| 140 |
from cylc.flow.terminal import ( |
| 141 |
cli_function, |
| 142 |
flatten_cli_lists |
| 143 |
) |
| 144 |
from cylc.flow.flow_mgr import add_flow_opts_for_trigger_and_set |
| 145 |
|
| 146 |
|
| 147 |
if TYPE_CHECKING: |
| 148 |
from optparse import Values |
| 149 |
from cylc.flow.id import Tokens |
| 150 |
|
| 151 |
|
| 152 |
# For setting xtriggers with --pre: |
| 153 |
XTRIGGER_PREREQ_PREFIX = "xtrigger" |
| 154 |
|
| 155 |
MUTATION = ''' |
| 156 |
mutation ( |
| 157 |
$wFlows: [WorkflowID]!, |
| 158 |
$tasks: [NamespaceIDGlob]!, |
| 159 |
$prerequisites: [PrerequisiteString], |
| 160 |
$outputs: [OutputLabel], |
| 161 |
$flow: [Flow!], |
| 162 |
$flowWait: Boolean, |
| 163 |
$flowDescr: String, |
| 164 |
) { |
| 165 |
set ( |
| 166 |
workflows: $wFlows, |
| 167 |
tasks: $tasks, |
| 168 |
prerequisites: $prerequisites, |
| 169 |
outputs: $outputs, |
| 170 |
flow: $flow, |
| 171 |
flowWait: $flowWait, |
| 172 |
flowDescr: $flowDescr |
| 173 |
) { |
| 174 |
result |
| 175 |
} |
| 176 |
} |
| 177 |
''' |
| 178 |
|
| 179 |
|
| 180 |
def get_option_parser() -> COP: |
| 181 |
parser = COP( |
| 182 |
__doc__, |
| 183 |
comms=True, |
| 184 |
multitask=True, |
| 185 |
multiworkflow=True, |
| 186 |
argdoc=[FULL_ID_MULTI_ARG_DOC], |
| 187 |
) |
| 188 |
|
| 189 |
parser.add_option( |
| 190 |
"-o", "--out", "--output", metavar="OUTPUT(s)", |
| 191 |
help=( |
| 192 |
"Complete task outputs. For multiple outputs re-use the" |
| 193 |
" option, or give a comma-separated list of outputs." |
| 194 |
" Use '--out=required' to complete all required outputs." |
| 195 |
" Use '--out=skip' to complete outputs defined in the task's" |
| 196 |
" [skip] configuration." |
| 197 |
" OUTPUT format: trigger names as used in the graph." |
| 198 |
), |
| 199 |
action="append", default=None, dest="outputs" |
| 200 |
) |
| 201 |
|
| 202 |
parser.add_option( |
| 203 |
"-p", "--pre", "--prerequisite", metavar="PREREQUISITE(s)", |
| 204 |
help=( |
| 205 |
"Satisfy task prerequisites. For multiple prerequisites" |
| 206 |
" re-use the option, or give a comma-separated list, or" |
| 207 |
' use "--pre=all" to satisfy all prerequisites, if any.' |
| 208 |
" PREREQUISITE format:" |
| 209 |
" '<cycle>/<task>[:<OUTPUT>]' or 'all'" |
| 210 |
" where <OUTPUT> defaults to succeeded; or" |
| 211 |
" xtrigger/<label>[:succeeded]' or 'xtrigger/all'" |
| 212 |
), |
| 213 |
action="append", default=None, dest="prerequisites" |
| 214 |
) |
| 215 |
|
| 216 |
add_flow_opts_for_trigger_and_set(parser) |
| 217 |
return parser |
| 218 |
|
| 219 |
|
| 220 |
async def run( |
| 221 |
options: 'Values', |
| 222 |
workflow_id: str, |
| 223 |
*tokens_list: 'Tokens' |
| 224 |
): |
| 225 |
pclient = get_client(workflow_id, timeout=options.comms_timeout) |
| 226 |
|
| 227 |
mutation_kwargs = { |
| 228 |
'request_string': MUTATION, |
| 229 |
'variables': { |
| 230 |
'wFlows': [workflow_id], |
| 231 |
'tasks': [ |
| 232 |
tokens.relative_id_with_selectors |
| 233 |
for tokens in tokens_list |
| 234 |
], |
| 235 |
'outputs': flatten_cli_lists(options.outputs), |
| 236 |
'prerequisites': flatten_cli_lists(options.prerequisites), |
| 237 |
'flow': options.flow, |
| 238 |
'flowWait': options.flow_wait, |
| 239 |
'flowDescr': options.flow_descr |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
return await pclient.async_request('graphql', mutation_kwargs) |
| 244 |
|
| 245 |
|
| 246 |
@cli_function(get_option_parser) |
| 247 |
def main(parser: COP, options: 'Values', *ids) -> None: |
| 248 |
rets = call_multi(partial(run, options), *ids) |
| 249 |
sys.exit(all(rets.values()) is False) |