Debian Codegraph @

cylc/flow/scripts/reload.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 reload [OPTIONS] ARGS
20
21 Reload the configuration of a running workflow.
22
23 Example:
24 # install and run the workflow
25 $ cylc install
26 $ cylc play my_workflow
27
28 # make changes to the workflow source directory
29
30 # reinstall the workflow
31 $ cylc reinstall my_workflow
32
33 # reload the workflow to pick up changes
34 $ cylc reload my_workflow
35 # the workflow is now running with the new config
36
37 All settings including task definitions, with the exception of workflow log
38 config, can be changed on reload. Changes to task definitions take effect
39 immediately, unless a task is already running at reload time.
40
41 Upon reload, remote file installation will be triggered for all relevant
42 platforms on the next job submit. Any changed files that are configured to be
43 included in the file installation will be transferred to the appropriate remote
44 platform(s).
45
46 If the workflow was started with Jinja2 template variables set on the command
47 line (cylc play --set 'FOO="bar"' WORKFLOW_ID) the same template settings apply
48 to the reload (only changes to the flow.cylc file itself are reloaded).
49
50 If the modified workflow definition does not parse, failure to reload will
51 be reported but no harm will be done to the running workflow.
52 """
53
54 from functools import partial
55 import sys
56 from typing import TYPE_CHECKING
57
58 from packaging.version import parse as parse_version
59
60 from cylc.flow.exceptions import InputError
61 from cylc.flow.network.client_factory import get_client
62 from cylc.flow.network.multi import call_multi
63 from cylc.flow.option_parsers import (
64 WORKFLOW_ID_MULTI_ARG_DOC,
65 CylcOptionParser as COP,
66 OptionSettings,
67 )
68 from cylc.flow.terminal import cli_function
69
70
71 if TYPE_CHECKING:
72 from optparse import Values
73
74
75 RELOAD_OPTIONS = [
76 OptionSettings(
77 ['-g', '--global'],
78 help='also reload global configuration.',
79 action="store_true",
80 default=False,
81 dest="reload_global",
82 sources={'reload'}
83 ),
84 ]
85
86
87 MUTATION = '''
88 mutation (
89 $wFlows: [WorkflowID]!,
90 ) {
91 reload (
92 workflows: $wFlows
93 ) {
94 result
95 }
96 }
97 '''
98
99 # Separate mutation, not backwards compatible with Cylc < 8.5.0:
100 MUTATION_GLOBAL = '''
101 mutation (
102 $wFlows: [WorkflowID]!,
103 $reloadGlobal: Boolean,
104 ) {
105 reload (
106 workflows: $wFlows
107 reloadGlobal: $reloadGlobal
108 ) {
109 result
110 }
111 }
112 '''
113
114
115 def get_option_parser():
116 parser = COP(
117 __doc__,
118 comms=True,
119 multiworkflow=True,
120 argdoc=[WORKFLOW_ID_MULTI_ARG_DOC],
121 )
122 for option in RELOAD_OPTIONS:
123 parser.add_option(*option.args, **option.kwargs)
124
125 return parser
126
127
128 async def run(options: 'Values', workflow_id: str):
129 pclient = get_client(workflow_id, timeout=options.comms_timeout)
130
131 if options.reload_global:
132 if parse_version(pclient.scheduler_version) < parse_version(
133 '8.5.0.dev'
134 ):
135 raise InputError(
136 "The --global option is not supported by the version of Cylc "
137 f"running the workflow ({pclient.scheduler_version})."
138 )
139 mutation_kwargs = {
140 'request_string': MUTATION_GLOBAL,
141 'variables': {
142 'wFlows': [workflow_id],
143 'reloadGlobal': options.reload_global,
144 }
145 }
146 else:
147 mutation_kwargs = {
148 'request_string': MUTATION,
149 'variables': {
150 'wFlows': [workflow_id],
151 }
152 }
153
154 return await pclient.async_request('graphql', mutation_kwargs)
155
156
157 @cli_function(get_option_parser)
158 def main(parser: COP, options: 'Values', *ids) -> None:
159 reload_cli(options, *ids)
160
161
162 def reload_cli(options: 'Values', *ids) -> None:
163 rets = call_multi(
164 partial(run, options),
165 *ids,
166 constraint='workflows',
167 )
168 sys.exit(all(rets.values()) is False)