Debian Codegraph @

cylc/flow/scripts/poll.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 poll [OPTIONS] ARGS
20
21 Poll (query) jobs to verify and update their statuses.
22
23 This checks the job status file and queries the job runner on the job platform.
24
25 Pollable tasks are those in the n=0 window with an associated job ID,
26 including incomplete finished tasks.
27
28 Examples:
29 # poll all pollable tasks in my_workflow
30 $ cylc poll 'my_workflow//*'
31
32 # poll specific tasks in my_workflow
33 $ cylc poll my_workflow// //1/a //1/b
34 """
35
36 from functools import partial
37 import sys
38 from typing import TYPE_CHECKING
39
40 from cylc.flow.network.client_factory import get_client
41 from cylc.flow.network.multi import call_multi
42 from cylc.flow.option_parsers import (
43 FULL_ID_MULTI_ARG_DOC,
44 CylcOptionParser as COP
45 )
46 from cylc.flow.terminal import cli_function
47
48 if TYPE_CHECKING:
49 from optparse import Values
50
51
52 MUTATION = '''
53 mutation (
54 $wFlows: [WorkflowID]!,
55 $tasks: [NamespaceIDGlob]!,
56 ) {
57 poll (
58 workflows: $wFlows,
59 tasks: $tasks,
60 ) {
61 result
62 }
63 }
64 '''
65
66
67 def get_option_parser() -> COP:
68 parser = COP(
69 __doc__,
70 comms=True,
71 multitask=True,
72 multiworkflow=True,
73 argdoc=[FULL_ID_MULTI_ARG_DOC],
74 )
75 return parser
76
77
78 async def run(options: 'Values', workflow_id: str, *tokens_list):
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):
97 rets = call_multi(
98 partial(run, options),
99 *ids,
100 )
101 sys.exit(all(rets.values()) is False)