Debian Codegraph @

cylc/flow/scripts/verbosity.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 verbosity [OPTIONS] ARGS
20
21 Set the logging severity level of a running scheduler.
22
23 Messages at or above the chosen level are logged. If you choose
24 WARNING (say) only WARNING and CRITICAL messages will be logged.
25 """
26
27 from functools import partial
28 import sys
29 from typing import TYPE_CHECKING
30
31 from cylc.flow import LOG_LEVELS
32 from cylc.flow.exceptions import InputError
33 from cylc.flow.network.client_factory import get_client
34 from cylc.flow.network.multi import call_multi
35 from cylc.flow.option_parsers import (
36 WORKFLOW_ID_MULTI_ARG_DOC,
37 CylcOptionParser as COP,
38 )
39 from cylc.flow.terminal import cli_function
40
41 if TYPE_CHECKING:
42 from optparse import Values
43
44
45 MUTATION = '''
46 mutation (
47 $wFlows: [WorkflowID]!,
48 $level: LogLevels!,
49 ) {
50 setVerbosity (
51 workflows: $wFlows,
52 level: $level,
53 ) {
54 result
55 }
56 }
57 '''
58
59
60 def get_option_parser() -> COP:
61 parser = COP(
62 __doc__,
63 comms=True,
64 multiworkflow=True,
65 argdoc=[
66 ('LEVEL', ', '.join(LOG_LEVELS.keys())),
67 WORKFLOW_ID_MULTI_ARG_DOC,
68 ]
69 )
70 return parser
71
72
73 async def run(options: 'Values', severity: str, workflow_id: str):
74 pclient = get_client(workflow_id, timeout=options.comms_timeout)
75
76 mutation_kwargs = {
77 'request_string': MUTATION,
78 'variables': {
79 'wFlows': [workflow_id],
80 'level': severity,
81 }
82 }
83
84 return await pclient.async_request('graphql', mutation_kwargs)
85
86
87 @cli_function(get_option_parser)
88 def main(parser: COP, options: 'Values', severity: str, *ids: str) -> None:
89 if severity not in LOG_LEVELS:
90 raise InputError(f"Illegal logging level, {severity}")
91 rets = call_multi(
92 partial(run, options, severity),
93 *ids,
94 constraint='workflows',
95 )
96 sys.exit(all(rets.values()) is False)