Debian Codegraph @

cylc/flow/scripts/kill.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 kill [OPTIONS] ARGS
20
21 Kill running or submitted jobs.
22
23 Examples:
24 # kill a specific task in my_workflow
25 $ cylc kill my_workflow//1/a
26
27 # kill multiple tasks in my_workflow
28 $ cylc kill myflow// //1/a //1/b //1/c
29
30 # kill all active tasks in the my_workflow
31 $ cylc kill 'my_workflow//*'
32 """
33
34 from functools import partial
35 import sys
36 from typing import TYPE_CHECKING
37
38 from cylc.flow.network.client_factory import get_client
39 from cylc.flow.network.multi import call_multi
40 from cylc.flow.option_parsers import (
41 FULL_ID_MULTI_ARG_DOC,
42 CylcOptionParser as COP,
43 )
44 from cylc.flow.terminal import cli_function
45
46 if TYPE_CHECKING:
47 from optparse import Values
48 from cylc.flow.id import Tokens
49
50
51 MUTATION = '''
52 mutation (
53 $wFlows: [WorkflowID]!,
54 $tasks: [NamespaceIDGlob]!,
55 ) {
56 kill (
57 workflows: $wFlows,
58 tasks: $tasks,
59 ) {
60 result
61 }
62 }
63 '''
64
65
66 def get_option_parser() -> 'COP':
67 parser = COP(
68 __doc__,
69 comms=True,
70 multitask=True,
71 multiworkflow=True,
72 argdoc=[FULL_ID_MULTI_ARG_DOC],
73 )
74
75 return parser
76
77
78 async def run(options: 'Values', workflow_id: str, *tokens_list: 'Tokens'):
79 pclient = get_client(workflow_id, timeout=options.comms_timeout)
80
81 mutation_kwargs = {
82 'request_string': MUTATION,
83 'variables': {
84 'wFlows': [workflow_id],
85 'tasks': [
86 tokens.relative_id_with_selectors
87 for tokens in tokens_list
88 ]
89 }
90 }
91
92 return await pclient.async_request('graphql', mutation_kwargs)
93
94
95 @cli_function(get_option_parser)
96 def main(parser: 'COP', options: 'Values', *ids: str):
97 """CLI of "cylc kill"."""
98 rets = call_multi(
99 partial(run, options),
100 *ids,
101 )
102 sys.exit(all(rets.values()) is False)