Debian Codegraph @

cylc/flow/scripts/trigger.py

raw
1 #!/usr/bin/env python3
2 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
3 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 """cylc trigger [OPTIONS] ARGS
19
20 Trigger a group of one or more tasks, respecting dependencies among them.
21
22 Prerequisites on tasks outside of the group will be satisfied automatically.
23
24 Tasks will be removed if necessary to allow re-run without intervention, so
25 triggered tasks that are preparing, submitted, or running may be killed.
26
27 Tasks that lead into a group will run immediately even if the workflow is
28 paused; activity will flow on from them once the workflow is resumed.
29
30 Triggering an unqueued task queues it; triggering a queued task runs it.
31
32 How flow numbers are assigned to triggered tasks:
33 Active tasks (n=0) already have assigned flows; inactive tasks (n>0) do not.
34 * If an interdependent group of triggered tasks includes active tasks, the
35 flow will be assigned the existing flow numbers of those active tasks.
36 * Otherwise the flow will be assigned all current active flow numbers.
37
38 Examples:
39 # trigger task foo in cycle 1, in workflow "test"
40 $ cylc trigger test//1/foo
41
42 # trigger all failed tasks in workflow "test"
43 $ cylc trigger 'test//*:failed' # (quotes required)
44
45 # start a new flow from 1/foo
46 # (beware of off-flow prerequisites downstream of 1/foo)
47 $ cylc trigger --flow=new test//1/foo
48
49 # rerun sub-graph "a => b & c" in the same flow, ignoring "off => b"
50 $ cylc trigger test //1/a //1/b //1/c
51
52 """
53
54 from functools import partial
55 import sys
56 from typing import TYPE_CHECKING
57
58 from cylc.flow.network.client_factory import get_client
59 from cylc.flow.network.multi import call_multi
60 from cylc.flow.option_parsers import (
61 FULL_ID_MULTI_ARG_DOC,
62 CylcOptionParser as COP,
63 )
64 from cylc.flow.terminal import cli_function
65 from cylc.flow.flow_mgr import add_flow_opts_for_trigger_and_set
66
67
68 if TYPE_CHECKING:
69 from optparse import Values
70
71
72 MUTATION = '''
73 mutation (
74 $wFlows: [WorkflowID]!,
75 $tasks: [NamespaceIDGlob]!,
76 $flow: [Flow!],
77 $flowWait: Boolean,
78 $flowDescr: String,
79 ) {
80 trigger (
81 workflows: $wFlows,
82 tasks: $tasks,
83 flow: $flow,
84 flowWait: $flowWait,
85 flowDescr: $flowDescr,
86 ) {
87 result
88 }
89 }
90 '''
91
92
93 def get_option_parser() -> COP:
94 parser = COP(
95 __doc__,
96 comms=True,
97 multitask=True,
98 multiworkflow=True,
99 argdoc=[FULL_ID_MULTI_ARG_DOC],
100 )
101
102 add_flow_opts_for_trigger_and_set(parser)
103
104 return parser
105
106
107 async def run(options: 'Values', workflow_id: str, *tokens_list):
108 pclient = get_client(workflow_id, timeout=options.comms_timeout)
109
110 mutation_kwargs = {
111 'request_string': MUTATION,
112 'variables': {
113 'wFlows': [workflow_id],
114 'tasks': [
115 tokens.relative_id_with_selectors
116 for tokens in tokens_list
117 ],
118 'flow': options.flow,
119 'flowWait': options.flow_wait,
120 'flowDescr': options.flow_descr,
121 }
122 }
123 return await pclient.async_request('graphql', mutation_kwargs)
124
125
126 @cli_function(get_option_parser)
127 def main(parser: COP, options: 'Values', *ids: str):
128 """CLI for "cylc trigger"."""
129 rets = call_multi(
130 partial(run, options),
131 *ids,
132 )
133 sys.exit(all(rets.values()) is False)