| 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 hold [OPTIONS] ARGS |
| 20 |
|
| 21 |
Hold task(s) in a workflow. |
| 22 |
|
| 23 |
Held tasks do not submit jobs even if ready. |
| 24 |
|
| 25 |
Any task can be held. |
| 26 |
|
| 27 |
Note: a held running task will not submit more jobs itself (e.g. by retries) |
| 28 |
until released, but the running job may still complete outputs that cause |
| 29 |
non-held downstream tasks to trigger. |
| 30 |
|
| 31 |
To pause an entire workflow use "cylc pause". |
| 32 |
|
| 33 |
Examples: |
| 34 |
# Hold mytask at cycle point 1234 in my_workflow (if it has not yet spawned, |
| 35 |
# it will hold as soon as it spawns): |
| 36 |
$ cylc hold my_workflow//1234/mytask |
| 37 |
|
| 38 |
# Hold all active tasks at cycle 1234 in my_workflow (note: tasks |
| 39 |
# before/after this cycle point will not be held): |
| 40 |
$ cylc hold 'my_workflow//1234/*' |
| 41 |
|
| 42 |
# Hold all active instances of mytask in my_workflow (note: this will not |
| 43 |
# hold any unspawned tasks that might spawn in the future): |
| 44 |
$ cylc hold 'my_workflow//*/mytask' |
| 45 |
|
| 46 |
# Hold all active failed tasks: |
| 47 |
$ cylc hold 'my_workflow//*:failed' |
| 48 |
|
| 49 |
# Hold all tasks after cycle point 1234 in my_workflow: |
| 50 |
$ cylc hold my_workflow// --after=1234 |
| 51 |
|
| 52 |
# Hold cycles 1, 2 & 3 in my_workflow: |
| 53 |
$ cylc hold my_workflow// //1 //2 //3 |
| 54 |
|
| 55 |
# Hold cycle "1" in "my_workflow_1" and "my_workflow_2": |
| 56 |
$ cylc hold my_workflow_1//1 my_workflow_2//1 |
| 57 |
|
| 58 |
Note: To pause a workflow (immediately preventing all job submission), use |
| 59 |
'cylc pause' instead. |
| 60 |
|
| 61 |
See also 'cylc release'. |
| 62 |
""" |
| 63 |
|
| 64 |
from functools import partial |
| 65 |
import sys |
| 66 |
from typing import TYPE_CHECKING |
| 67 |
|
| 68 |
from cylc.flow.exceptions import InputError |
| 69 |
from cylc.flow.network.client_factory import get_client |
| 70 |
from cylc.flow.option_parsers import ( |
| 71 |
FULL_ID_MULTI_ARG_DOC, |
| 72 |
CylcOptionParser as COP, |
| 73 |
) |
| 74 |
from cylc.flow.terminal import cli_function |
| 75 |
from cylc.flow.network.multi import call_multi |
| 76 |
|
| 77 |
if TYPE_CHECKING: |
| 78 |
from optparse import Values |
| 79 |
|
| 80 |
|
| 81 |
HOLD_MUTATION = ''' |
| 82 |
mutation ( |
| 83 |
$wFlows: [WorkflowID]!, |
| 84 |
$tasks: [NamespaceIDGlob]! |
| 85 |
) { |
| 86 |
hold ( |
| 87 |
workflows: $wFlows, |
| 88 |
tasks: $tasks |
| 89 |
) { |
| 90 |
result |
| 91 |
} |
| 92 |
} |
| 93 |
''' |
| 94 |
|
| 95 |
SET_HOLD_POINT_MUTATION = ''' |
| 96 |
mutation ( |
| 97 |
$wFlows: [WorkflowID]!, |
| 98 |
$point: CyclePoint! |
| 99 |
) { |
| 100 |
setHoldPoint ( |
| 101 |
workflows: $wFlows, |
| 102 |
point: $point |
| 103 |
) { |
| 104 |
result |
| 105 |
} |
| 106 |
} |
| 107 |
''' |
| 108 |
|
| 109 |
|
| 110 |
def get_option_parser() -> COP: |
| 111 |
parser = COP( |
| 112 |
__doc__, |
| 113 |
comms=True, |
| 114 |
multitask=True, |
| 115 |
multiworkflow=True, |
| 116 |
argdoc=[FULL_ID_MULTI_ARG_DOC], |
| 117 |
) |
| 118 |
|
| 119 |
parser.add_option( |
| 120 |
"--after", |
| 121 |
help="Hold all tasks after this cycle point.", |
| 122 |
metavar="CYCLE_POINT", action="store", dest="hold_point_string") |
| 123 |
|
| 124 |
return parser |
| 125 |
|
| 126 |
|
| 127 |
def _validate(options: 'Values', *task_globs: str) -> None: |
| 128 |
"""Check combination of options and task globs is valid.""" |
| 129 |
if options.hold_point_string: |
| 130 |
if task_globs: |
| 131 |
raise InputError( |
| 132 |
"Cannot combine --after with Cylc/Task IDs.\n" |
| 133 |
"`cylc hold --after` holds all tasks after the given " |
| 134 |
"cycle point.") |
| 135 |
elif not task_globs: |
| 136 |
raise InputError( |
| 137 |
"Must define Cycles/Tasks. See `cylc hold --help`.") |
| 138 |
|
| 139 |
|
| 140 |
async def run(options, workflow_id, *tokens_list): |
| 141 |
_validate(options, *tokens_list) |
| 142 |
|
| 143 |
pclient = get_client(workflow_id, timeout=options.comms_timeout) |
| 144 |
|
| 145 |
if options.hold_point_string: |
| 146 |
mutation = SET_HOLD_POINT_MUTATION |
| 147 |
args = {'point': options.hold_point_string} |
| 148 |
else: |
| 149 |
mutation = HOLD_MUTATION |
| 150 |
args = { |
| 151 |
'tasks': [ |
| 152 |
id_.relative_id_with_selectors |
| 153 |
for id_ in tokens_list |
| 154 |
] |
| 155 |
} |
| 156 |
|
| 157 |
mutation_kwargs = { |
| 158 |
'request_string': mutation, |
| 159 |
'variables': { |
| 160 |
'wFlows': [workflow_id], |
| 161 |
**args |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return await pclient.async_request('graphql', mutation_kwargs) |
| 166 |
|
| 167 |
|
| 168 |
@cli_function(get_option_parser) |
| 169 |
def main(parser: COP, options: 'Values', *ids): |
| 170 |
rets = call_multi( |
| 171 |
partial(run, options), |
| 172 |
*ids, |
| 173 |
constraint='mixed', |
| 174 |
) |
| 175 |
sys.exit(all(rets.values()) is False) |