Debian Codegraph @

cylc/flow/scripts/ping.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 ping [OPTIONS] ARGS
20
21 Test communication with running workflows.
22
23 Print the HOST:PORT of running workflows.
24 If any are not running, exit with error status.
25 """
26
27 from functools import partial
28 import sys
29 from typing import Any, Dict, TYPE_CHECKING
30
31 from cylc.flow.network.client_factory import get_client
32 from cylc.flow.network.multi import call_multi
33 from cylc.flow.option_parsers import (
34 ID_MULTI_ARG_DOC,
35 CylcOptionParser as COP,
36 )
37 from cylc.flow.terminal import cli_function
38
39 if TYPE_CHECKING:
40 from optparse import Values
41
42
43 FLOW_QUERY = '''
44 query ($wFlows: [ID]) {
45 workflows(ids: $wFlows) {
46 id
47 name
48 port
49 }
50 }
51 '''
52
53
54 def get_option_parser() -> COP:
55 parser = COP(
56 __doc__,
57 comms=True,
58 multiworkflow=True,
59 argdoc=[ID_MULTI_ARG_DOC],
60 )
61
62 return parser
63
64
65 async def run(
66 options: 'Values',
67 workflow_id: str,
68 client=None
69 ) -> Dict:
70
71 pclient = client or get_client(workflow_id, timeout=options.comms_timeout)
72
73 ret: Dict[str, Any] = {
74 'stdout': [],
75 'stderr': [],
76 'exit': 0
77 }
78 flow_kwargs: Dict[str, Any] = {
79 'request_string': FLOW_QUERY,
80 'variables': {'wFlows': [workflow_id]}
81 }
82
83 result = await pclient.async_request('graphql', flow_kwargs)
84
85 for flow in result['workflows']:
86 ret['stdout'].append(f"{pclient.host}:{flow['port']}")
87 return ret
88
89
90 def report(response):
91 return (
92 '\n'.join(response['stdout']),
93 '\n'.join(response['stderr']),
94 response['exit'] == 0,
95 )
96
97
98 @cli_function(get_option_parser)
99 def main(
100 parser: COP,
101 options: 'Values',
102 *ids,
103 ) -> None:
104 rets = call_multi(
105 partial(run, options),
106 *ids,
107 report=report,
108 constraint='workflows',
109 )
110 sys.exit(all(rets.values()) is False)