Debian Codegraph @

cylc/flow/scripts/release.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 release [OPTIONS] ARGS
20
21 Release held tasks in a workflow.
22
23 Examples:
24 # Release mytask at cycle 1234 in my_workflow
25 $ cylc release my_workflow//1234/mytask
26
27 # Release all active tasks at cycle 1234 in my_workflow
28 $ cylc release 'my_workflow//1234/*'
29
30 # Release all active instances of mytask in my_workflow
31 $ cylc release 'my_workflow//*/mytask'
32
33 # Release all held tasks and remove the hold point
34 $ cylc release my_workflow --all
35
36 Held tasks do not submit their jobs even if ready to run.
37
38 See also 'cylc hold'.
39 """
40
41 from functools import partial
42 import sys
43 from typing import TYPE_CHECKING
44
45 from cylc.flow.exceptions import InputError
46 from cylc.flow.network.client_factory import get_client
47 from cylc.flow.network.multi import call_multi
48 from cylc.flow.option_parsers import (
49 FULL_ID_MULTI_ARG_DOC,
50 CylcOptionParser as COP,
51 )
52 from cylc.flow.terminal import cli_function
53
54 if TYPE_CHECKING:
55 from optparse import Values
56
57
58 RELEASE_MUTATION = '''
59 mutation (
60 $wFlows: [WorkflowID]!,
61 $tasks: [NamespaceIDGlob]!
62 ) {
63 release (
64 workflows: $wFlows,
65 tasks: $tasks,
66 ) {
67 result
68 }
69 }
70 '''
71
72 RELEASE_HOLD_POINT_MUTATION = '''
73 mutation (
74 $wFlows: [WorkflowID]!
75 ) {
76 releaseHoldPoint (
77 workflows: $wFlows
78 ) {
79 result
80 }
81 }
82 '''
83
84
85 def get_option_parser() -> COP:
86 parser = COP(
87 __doc__,
88 comms=True,
89 multitask=True,
90 multiworkflow=True,
91 argdoc=[FULL_ID_MULTI_ARG_DOC],
92 )
93
94 parser.add_option(
95 "--all",
96 help=(
97 "Release all held tasks and remove the 'hold after cycle point', "
98 "if set."),
99 action="store_true", dest="release_all")
100
101 return parser
102
103
104 def _validate(options: 'Values', *tokens_list: str) -> None:
105 """Check combination of options and task globs is valid."""
106 if options.release_all:
107 if tokens_list:
108 raise InputError("Cannot combine --all with Cycle/Task IDs")
109 else:
110 if not tokens_list:
111 raise InputError(
112 "Must define Cycles/Tasks. See `cylc release --help`."
113 )
114
115
116 async def run(options: 'Values', workflow_id, *tokens_list):
117 _validate(options, *tokens_list)
118
119 pclient = get_client(workflow_id, timeout=options.comms_timeout)
120
121 if options.release_all:
122 mutation = RELEASE_HOLD_POINT_MUTATION
123 args = {'tasks': ['*/*']}
124 else:
125 mutation = RELEASE_MUTATION
126 args = {
127 'tasks': [
128 tokens.relative_id_with_selectors
129 for tokens in tokens_list
130 ]
131 }
132
133 mutation_kwargs = {
134 'request_string': mutation,
135 'variables': {
136 'wFlows': [workflow_id],
137 **args
138 }
139 }
140
141 return await pclient.async_request('graphql', mutation_kwargs)
142
143
144 @cli_function(get_option_parser)
145 def main(parser: COP, options: 'Values', *ids):
146 rets = call_multi(
147 partial(run, options),
148 *ids,
149 constraint='mixed',
150 )
151 sys.exit(all(rets.values()) is False)