Debian Codegraph @

cylc/flow/scripts/remove.py

raw
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 remove [OPTIONS] ARGS
20
21 Remove active (n=0) tasks, and erase flow history.
22
23 The primary use cases for this command are:
24 * Remove final-status incomplete tasks from n=0 to prevent or release a stall,
25 if you don't want to rerun them to complete required outputs.
26 * Erase the flow history of tasks to allow them to rerun without starting a
27 new flow. (Note that `cylc trigger` now does this automatically, however).
28
29 Tasks will be removed from ALL flows, by defaut.
30
31 Tasks removed from all flows, and any waiting downstream tasks spawned by
32 their outputs, will be recorded with no flow numbers and will not affect
33 the evolution of the workflow.
34
35 If you remove a task from some of its flows, it will still exist in the
36 remaining flows but will not affect the evolution of the removed flows.
37
38 Removing a submitted or running task also kills it (see "cylc kill").
39
40 Examples:
41 # Remove a task that already ran.
42 # (Any downstream tasks that are already running or finished will be
43 # left alone. The task and its outputs will be left in the None flow.)
44 $ cylc remove <id>
45
46 # Remove a task from a specified flow.
47 # (The task may remain in other flows)
48 $ cylc remove <id> --flow=1
49 """
50
51 from functools import partial
52 import sys
53 from typing import TYPE_CHECKING
54
55 from cylc.flow.flow_mgr import add_flow_opts_for_remove
56 from cylc.flow.network.client_factory import get_client
57 from cylc.flow.network.multi import call_multi
58 from cylc.flow.option_parsers import (
59 FULL_ID_MULTI_ARG_DOC,
60 CylcOptionParser as COP,
61 )
62 from cylc.flow.terminal import cli_function
63
64
65 if TYPE_CHECKING:
66 from optparse import Values
67
68
69 MUTATION = '''
70 mutation (
71 $wFlows: [WorkflowID]!,
72 $tasks: [NamespaceIDGlob]!,
73 $flow: [Flow!],
74 ) {
75 remove (
76 workflows: $wFlows,
77 tasks: $tasks,
78 flow: $flow
79 ) {
80 result
81 }
82 }
83 '''
84
85
86 def get_option_parser() -> COP:
87 parser = COP(
88 __doc__,
89 comms=True,
90 multitask=True,
91 multiworkflow=True,
92 argdoc=[FULL_ID_MULTI_ARG_DOC],
93 )
94 add_flow_opts_for_remove(parser)
95 return parser
96
97
98 async def run(options: 'Values', workflow_id: str, *tokens_list):
99 pclient = get_client(workflow_id, timeout=options.comms_timeout)
100
101 mutation_kwargs = {
102 'request_string': MUTATION,
103 'variables': {
104 'wFlows': [workflow_id],
105 'tasks': [
106 tokens.relative_id_with_selectors
107 for tokens in tokens_list
108 ],
109 'flow': options.flow,
110 }
111 }
112
113 return await pclient.async_request('graphql', mutation_kwargs)
114
115
116 @cli_function(get_option_parser)
117 def main(parser: COP, options: 'Values', *ids: str):
118 rets = call_multi(
119 partial(run, options),
120 *ids,
121 )
122 sys.exit(all(rets.values()) is False)