| 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 |
"""Submits job scripts to loadleveler by the ``llsubmit`` command. |
| 17 |
|
| 18 |
.. cylc-scope:: flow.cylc[runtime][<namespace>] |
| 19 |
|
| 20 |
Loadleveler directives can be provided in the flow.cylc file: |
| 21 |
|
| 22 |
.. code-block:: cylc |
| 23 |
:caption: global.cylc |
| 24 |
|
| 25 |
[platforms] |
| 26 |
[[myplatform]] |
| 27 |
job runner = loadleveler |
| 28 |
|
| 29 |
.. code-block:: cylc |
| 30 |
:caption: flow.cylc |
| 31 |
|
| 32 |
[runtime] |
| 33 |
[[my_task]] |
| 34 |
platform = myplatform |
| 35 |
execution time limit = PT10M |
| 36 |
[[[directives]]] |
| 37 |
foo = bar |
| 38 |
baz = qux |
| 39 |
|
| 40 |
These are written to the top of the job script like this: |
| 41 |
|
| 42 |
.. code-block:: bash |
| 43 |
|
| 44 |
#!/bin/bash |
| 45 |
# DIRECTIVES |
| 46 |
# @ foo = bar |
| 47 |
# @ baz = qux |
| 48 |
# @ wall_clock_limit = 660,600 |
| 49 |
# @ queue |
| 50 |
|
| 51 |
If ``restart=yes`` is specified as a directive for loadleveler, the job will |
| 52 |
automatically trap SIGUSR1, which loadleveler may use to preempt the job. On |
| 53 |
trapping SIGUSR1, the job will inform the workflow that it has been vacated by |
| 54 |
loadleveler. This will put it back to the submitted state, until it starts |
| 55 |
running again. |
| 56 |
|
| 57 |
If :cylc:conf:`execution time limit` is specified, it is used to generate the |
| 58 |
``wall_clock_limit`` directive. The setting is assumed to be the soft limit. |
| 59 |
The hard limit will be set by adding an extra minute to the soft limit. Do not |
| 60 |
specify the ``wall_clock_limit`` directive explicitly if :cylc:conf:`execution |
| 61 |
time limit` is specified. Otherwise, the execution time limit known by the |
| 62 |
workflow may be out of sync with what is submitted to the job runner. |
| 63 |
|
| 64 |
.. cylc-scope:: |
| 65 |
|
| 66 |
""" |
| 67 |
|
| 68 |
import re |
| 69 |
|
| 70 |
from cylc.flow.id import Tokens |
| 71 |
|
| 72 |
|
| 73 |
class LoadlevelerHandler(): |
| 74 |
|
| 75 |
"""Loadleveler job submission""" |
| 76 |
|
| 77 |
DIRECTIVE_PREFIX = "# @ " |
| 78 |
KILL_CMD_TMPL = "llcancel '%(job_id)s'" |
| 79 |
POLL_CMD = "llq" |
| 80 |
REC_ID_FROM_SUBMIT_OUT = re.compile( |
| 81 |
r"""\Allsubmit:\sThe\sjob\s"(?P<id>[^"]+)"\s""") |
| 82 |
REC_ERR_FILTERS = [ |
| 83 |
re.compile("^llsubmit: Processed command file through Submit Filter:")] |
| 84 |
SUBMIT_CMD_TMPL = "llsubmit '%(job)s'" |
| 85 |
VACATION_SIGNAL = "USR1" |
| 86 |
TIME_LIMIT_DIRECTIVE = "wall_clock_limit" |
| 87 |
|
| 88 |
def format_directives(self, job_conf): |
| 89 |
"""Format the job directives for a job file.""" |
| 90 |
job_file_path = job_conf['job_file_path'] |
| 91 |
directives = job_conf["directives"].__class__() |
| 92 |
tokens = Tokens(job_conf["task_id"], relative=True) |
| 93 |
directives["job_name"] = ( |
| 94 |
f'{job_conf["workflow_name"]}.{tokens["task"]}.{tokens["cycle"]}' |
| 95 |
|
| 96 |
) |
| 97 |
directives["output"] = job_file_path + ".out" |
| 98 |
directives["error"] = job_file_path + ".err" |
| 99 |
if (job_conf["execution_time_limit"] and |
| 100 |
directives.get(self.TIME_LIMIT_DIRECTIVE) is None): |
| 101 |
directives[self.TIME_LIMIT_DIRECTIVE] = "%d,%d" % ( |
| 102 |
job_conf["execution_time_limit"] + 60, |
| 103 |
job_conf["execution_time_limit"]) |
| 104 |
for key, value in list(job_conf["directives"].items()): |
| 105 |
directives[key] = value |
| 106 |
lines = [] |
| 107 |
for key, value in directives.items(): |
| 108 |
if value: |
| 109 |
lines.append("%s%s = %s" % (self.DIRECTIVE_PREFIX, key, value)) |
| 110 |
else: |
| 111 |
lines.append("%s%s" % (self.DIRECTIVE_PREFIX, key)) |
| 112 |
lines.append("%squeue" % (self.DIRECTIVE_PREFIX)) |
| 113 |
return lines |
| 114 |
|
| 115 |
def filter_submit_output(self, out, err): |
| 116 |
"""Filter the stdout/stderr output - suppress process message.""" |
| 117 |
new_err = "" |
| 118 |
if err: |
| 119 |
for line in err.splitlines(): |
| 120 |
if any(rec.match(line) for rec in self.REC_ERR_FILTERS): |
| 121 |
continue |
| 122 |
new_err += line + "\n" |
| 123 |
return out, new_err |
| 124 |
|
| 125 |
@classmethod |
| 126 |
def filter_poll_many_output(cls, out): |
| 127 |
"""Return a list of job IDs still in the job runner. |
| 128 |
|
| 129 |
Drop STEPID from the JOBID.STEPID returned by 'llq'. |
| 130 |
""" |
| 131 |
job_ids = [] |
| 132 |
for line in out.splitlines(): |
| 133 |
try: |
| 134 |
head = line.split(None, 1)[0] |
| 135 |
except IndexError: |
| 136 |
continue |
| 137 |
job_ids.append(".".join(head.split(".")[:2])) |
| 138 |
return job_ids |
| 139 |
|
| 140 |
def get_vacation_signal(self, job_conf): |
| 141 |
"""Return "USR1" if "restart" directive is "yes".""" |
| 142 |
if job_conf["directives"].get("restart") == "yes": |
| 143 |
return self.VACATION_SIGNAL |
| 144 |
|
| 145 |
|
| 146 |
JOB_RUNNER_HANDLER = LoadlevelerHandler() |