Debian Codegraph @

cylc/flow/job_runner_handlers/documentation.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """
18 This file is used to auto-generate some reference documentation for the
19 job runner plugin interface.
20
21 Note the class contained here is just for documentation purposes and is
22 not intended to be subclassed.
23 """
24
25 from typing import (
26 Iterable,
27 List,
28 Tuple,
29 TYPE_CHECKING,
30 )
31
32 if TYPE_CHECKING:
33 import re
34
35
36 class ExampleHandler():
37 """Documentation for writing job runner handlers.
38
39 Cylc can submit jobs to a number of different job runners
40 (aka batch systems) e.g. Slurm and PBS. For a list of built-in integrations
41 see :ref:`AvailableMethods`.
42
43 If the job runner you require is not on this list, Cylc provides a generic
44 interface for writing your own integration.
45
46 Defining a new job runner handler requires a little Python programming. Use
47 the built-in handlers
48 (e.g. :py:mod:`cylc.flow.job_runner_handlers.background`) as examples.
49
50 .. _where to put job runner handler modules:
51
52 .. rubric:: Installation
53
54 Custom job runner handlers must be installed on workflow and job
55 hosts in one of these locations:
56
57 - under ``WORKFLOW-RUN-DIR/lib/python/``
58 - under ``CYLC-PATH/cylc/flow/job_runner_handlers/``
59 - or anywhere in ``$PYTHONPATH``
60
61 Each module should export the symbol ``JOB_RUNNER_HANDLER`` for the
62 singleton instance that implements the job system handler logic e.g:
63
64 .. code-block:: python
65 :caption: my_handler.py
66
67 class MyHandler():
68 pass
69
70 JOB_RUNNER_HANDLER = MyHandler()
71
72 Each job runner handler class should instantiate with no argument.
73
74
75 .. rubric:: Usage
76
77 You can then define a Cylc platform using the handler:
78
79 .. code-block:: cylc
80 :caption: global.cylc
81
82 [platforms]
83 [[my_platform]]
84 job runner = my_handler # note matches Python module name
85 hosts = localhost
86
87 And configure tasks to submit to it:
88
89 .. code-block:: cylc
90 :caption: flow.cylc
91
92 [runtime]
93 [[my_task]]
94 script = echo "Hello World!"
95 platform = my_platform
96
97
98 .. rubric:: Common Arguments
99
100 ``job_conf: dict``
101 The Cylc job configuration as a dictionary with the following fields:
102
103 * ``dependencies``
104 * ``directives``
105 * ``env-script``
106 * ``environment``
107 * ``err-script``
108 * ``execution_time_limit``
109 * ``exit-script``
110 * ``flow_nums``
111 * ``init-script``
112 * ``job_d``
113 * ``job_file_path``
114 * ``job_runner_command_template``
115 * ``job_runner_name``
116 * ``namespace_hierarchy``
117 * ``param_var``
118 * ``platform``
119 * ``post-script``
120 * ``pre-script``
121 * ``script``
122 * ``submit_num``
123 * ``task_id``
124 * ``try_num``
125 * ``uuid_str``
126 * ``work_d``
127 * ``workflow_name``
128
129 ``submit_opts: dict``
130 The Cylc job submission options as a dictionary which may contain
131 the following fields:
132
133 * ``env``
134 * ``execution_time_limit``
135 * ``execution_time_limit``
136 * ``job_runner_cmd_tmpl``
137 * ``job_runner_cmd_tmpl``
138
139
140 .. rubric:: An Example
141
142 The following ``qsub.py`` module overrides the built-in *pbs*
143 job runner handler to change the directive prefix from ``#PBS`` to
144 ``#QSUB``:
145
146 .. TODO - double check that this still works, it's been a while
147
148 .. code-block:: python
149
150 #!/usr/bin/env python3
151
152 from cylc.flow.job_runner_handlers.pbs import PBSHandler
153
154 class QSUBHandler(PBSHandler):
155 DIRECTIVE_PREFIX = "#QSUB "
156
157 JOB_RUNNER_HANDLER = QSUBHandler()
158
159 If this is in the Python search path (see
160 :ref:`Where To Put Job Runner Handler Modules` above) you can use it by
161 name in your global configuration:
162
163 .. code-block:: cylc
164
165 [platforms]
166 [[my_platform]]
167 hosts = myhostA, myhostB
168 job runner = qsub # <---!
169
170 Then in your ``flow.cylc`` file you can use this platform:
171
172 .. code-block:: cylc
173
174 # Note, this workflow will fail at run time because we only changed the
175 # directive format, and PBS does not accept ``#QSUB`` directives in
176 # reality.
177
178 [scheduling]
179 [[graph]]
180 R1 = "a"
181 [runtime]
182 [[root]]
183 execution time limit = PT1M
184 platform = my_platform
185 [[[directives]]]
186 -l nodes = 1
187 -q = long
188 -V =
189
190 .. note::
191
192 Don't subclass this class as it provides optional interfaces which
193 you may not want to inherit.
194
195 """
196
197 FAIL_SIGNALS: Tuple[str]
198 """A tuple containing the names of signals to trap for reporting errors.
199
200 The default is ``("EXIT", "ERR", "TERM", "XCPU")``.
201
202 ``ERR`` and ``EXIT`` are always recommended.
203 ``EXIT`` is used to report premature stopping of the job
204 script, and its trap is unset at the end of the script.
205 """
206
207 KILL_CMD_TMPL: str
208 """Command template for killing a job submission.
209
210 A Python string template for getting the job runner command to remove
211 and terminate a job ID. The command is formed using the logic:
212 ``job_runner.KILL_CMD_TMPL % {"job_id": job_id}``.
213
214 For info on Python string template format see:
215 https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
216
217 """
218
219 POLL_CMD: str
220 """Command for checking job submissions.
221
222 A list of job IDs to poll will be provided as arguments.
223
224 The command should write valid submitted/running job IDs to stdout.
225
226 * To filter out invalid/failed jobs use
227 :py:meth:`ExampleHandler.filter_poll_many_output`.
228 * To build a more advanced command than is possible with this configuration
229 use :py:meth:`ExampleHandler.get_poll_many_cmd`.
230
231 """
232
233 POLL_CANT_CONNECT_ERR: str
234 """String for detecting communication errors in poll command output.
235
236 A string containing an error message. If this is defined, when a poll
237 command returns a non-zero return code and its STDERR contains this
238 string, then the poll result will not be trusted, because it is assumed
239 that the job runner is currently unavailable. Jobs submitted to the
240 job runner will be assumed OK until we are able to connect to the
241 job runner again.
242
243 """
244
245 SHOULD_KILL_PROC_GROUP: bool
246 """Kill jobs by killing the process group.
247
248 A boolean to indicate whether it is necessary to kill a job by sending
249 a signal to its Unix process group. This boolean also indicates that
250 a job submitted via this job runner will physically run on the same
251 host it is submitted to.
252
253 """
254
255 SHOULD_POLL_PROC_GROUP: bool
256 """Poll jobs by PID.
257
258 A boolean to indicate whether it is necessary to poll a job by its PID
259 as well as the job ID.
260
261 """
262
263 REC_ID_FROM_SUBMIT_OUT: 're.Pattern'
264 """Regular expression to extract job IDs from submission stderr.
265
266 A regular expression (compiled) to extract the job "id" from the standard
267 output or standard error of the job submission command.
268
269 """
270
271 REC_ID_FROM_SUBMIT_ERR: 're.Pattern'
272 """Regular expression to extract job IDs from submission stderr.
273
274 See :py:attr:`ExampleHandler.REC_ID_FROM_SUBMIT_OUT`.
275
276 """
277
278 SUBMIT_CMD_ENV: Iterable[str]
279 """Extra environment variables for the job runner command.
280
281 A Python dict (or an iterable that can be used to update a dict)
282 containing extra environment variables for getting the job runner
283 command to submit a job file.
284
285 """
286
287 SUBMIT_CMD_TMPL: str
288 """Command template for job submission.
289
290 A Python string template for getting the job runner command to submit a
291 job file. The command is formed using the logic:
292 ``job_runner.SUBMIT_CMD_TMPL % {"job": job_file_path}``
293
294 For info on Python string template format see:
295 https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
296
297 """
298
299 def filter_poll_many_output(self, out: str) -> List[str]:
300 """Filter job ides out of poll output.
301
302 Called after the job runner's poll command. The method should read
303 the output and return a list of job IDs that are still in the
304 job runner.
305
306 Args:
307 out: Job poll stdout.
308
309 Returns:
310 List of job IDs
311
312 """
313 raise NotImplementedError()
314
315 def filter_submit_output(self, out: str, err: str) -> Tuple[str, str]:
316 """Filter job submission stdout/err.
317
318 Filter the standard output and standard error of the job submission
319 command. This is useful if the job submission command returns
320 information that should just be ignored.
321
322 See also :py:meth:`ExampleHandler.SUBMIT_CMD_TMPL`.
323
324 Args:
325 out: Job submit stdout.
326 err: Job submit stderr.
327
328 Returns:
329 (new_out, new_err)
330 """
331 raise NotImplementedError()
332
333 def format_directives(self, job_conf: dict) -> List[str]:
334 """Returns lines to be appended to the job script.
335
336 This method formats the job directives for a job file, if
337 job file directives are relevant for the job runner. The argument
338 "job_conf" is a dict containing the job configuration.
339
340 Args:
341 job_conf: The Cylc configuration.
342
343 Returns:
344 lines
345
346 """
347 raise NotImplementedError()
348
349 def get_poll_many_cmd(self, job_id_list: List[str]) -> List[str]:
350 """Return a command to poll the specified jobs.
351
352 If specified, this will be called instead of
353 :py:attr:`ExampleHandler.POLL_CMD`.
354
355 Args:
356 job_id_list: The list of job IDs to poll.
357
358 Returns:
359 command e.g. ['foo', '--bar', 'baz']
360
361 """
362 raise NotImplementedError()
363
364 def get_submit_stdin(self, job_file_path: str, submit_opts: dict) -> Tuple:
365 """
366
367 Return a 2-element tuple ``(proc_stdin_arg, proc_stdin_value)``.
368
369 * Element 1 is suitable for the ``stdin=...`` argument of
370 ``subprocess.Popen`` so it can be a file handle, ``subprocess.PIPE``
371 or ``None``.
372 * Element 2 is the string content to pipe to stdin of the submit
373 command (relevant only if ``proc_stdin_arg`` is ``subprocess.PIPE``.
374
375 Args:
376 job_file_path: The path to the job file for this submission.
377 submit_opts: Job submission options.
378
379 Returns:
380 (proc_stdin_arg, proc_stdin_value)
381
382 """
383 raise NotImplementedError()
384
385 def get_vacation_signal(self, job_conf: dict) -> str:
386 """Return the vacation signal.
387
388 If relevant, return a string containing the name of the signal that
389 indicates the job has been vacated by the job runner.
390
391 Args:
392 job_conf: The Cylc configuration.
393
394 Returns:
395 signal
396
397 """
398 raise NotImplementedError()
399
400 def submit(
401 self,
402 job_file_path: str,
403 submit_opts: dict,
404 ) -> Tuple[int, str, str]:
405 """Submit a job.
406
407 Submit a job and return an instance of the Popen object for the
408 submission. This method is useful if the job submission requires logic
409 beyond just running a system or shell command.
410
411 See also :py:attr:`ExampleHandler.SUBMIT_CMD_TMPL`.
412
413 You must pass "env=submit_opts.get('env')" to Popen - see
414 :py:mod:`cylc.flow.job_runner_handlers.background`
415 for an example.
416
417 Args:
418 job_file_path: The job file for this submission.
419 submit_opts: Job submission options.
420
421 Returns:
422 (ret_code, out, err)
423
424 ret_code:
425 Subprocess return code.
426 out:
427 Subprocess standard output.
428 err:
429 Subprocess standard error.
430
431 """
432 raise NotImplementedError()
433
434 def manip_job_id(self, job_id: str) -> str:
435 """Modify the job ID that is returned by the job submit command.
436
437 Args:
438 job_id: The job ID returned by the submit command.
439
440 Returns:
441 job_id
442
443 """
444 raise NotImplementedError()