Debian Codegraph @

cylc/flow/job_runner_handlers/at.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 """Submits job scripts to the rudimentary Unix ``at`` scheduler.
17
18 .. cylc-scope:: flow.cylc[runtime][<namespace>]
19
20 .. note::
21
22 The ``atd`` daemon must be running.
23
24 .. note::
25
26 The ``atq`` command does not report if the job is running or not.
27
28 If an :cylc:conf:`execution time limit` is specified for a task, its job will
29 be wrapped by the ``timeout`` command.
30
31 .. cylc-scope::
32
33 """
34
35 import errno
36 import os
37 import re
38 from subprocess import PIPE
39
40
41 class AtCommandHandler():
42 """Logic to submit jobs to the "at" job runner."""
43 # List of known error strings when atd is not running
44 ERR_NO_ATD_STRS = [
45 "Can't open /var/run/atd.pid to signal atd. No atd running?",
46 "Warning: at daemon not running",
47 ]
48 SHOULD_KILL_PROC_GROUP = True
49 SHOULD_POLL_PROC_GROUP = True
50 KILL_CMD_TMPL = "atrm '%(job_id)s'"
51 POLL_CMD = "atq"
52 REC_ERR_FILTERS = [
53 re.compile("warning: commands will be executed using /bin/sh")]
54 REC_ID_FROM_SUBMIT_ERR = re.compile(r"\Ajob\s(?P<id>\S+)\sat")
55 # Note: The SUBMIT_CMD_STDIN_TMPL below requires "sh" compatible shell. The
56 # safest way, therefore, is to force the command to run under "/bin/sh" by
57 # exporting "SHELL=/bin/sh" for the "at" command.
58 SUBMIT_CMD_ENV = {"SHELL": "/bin/sh"}
59 SUBMIT_CMD_TMPL = "at now"
60 # Note: The perl command ensures that the job script is executed in its own
61 # process group, which allows the job script and its child processes to be
62 # killed correctly.
63 SUBMIT_CMD_STDIN_TMPL = (
64 r"exec perl -e 'setpgrp(0,0);exec(@ARGV)'" +
65 r" '%(job)s' 1>'%(job)s.out' 2>'%(job)s.err'")
66 SUBMIT_CMD_STDIN_TMPL_2 = (
67 r"exec perl -e 'setpgrp(0,0);exec(@ARGV)'" +
68 r" timeout --signal=XCPU %(execution_time_limit)d" +
69 r" '%(job)s' 1>'%(job)s.out' 2>'%(job)s.err'")
70
71 # atq properties:
72 # * stdout is "job-num date hour queue username", e.g.:
73 # 1762 Wed May 15 00:20:00 2013 = hilary
74 # * queue is '=' if running
75 #
76
77 def filter_submit_output(self, out, err):
78 """Suppress at's routine output to stderr.
79
80 Otherwise we get warning messages that suggest something is wrong.
81 1) move the standard job ID message from stderr to stdout
82 2) suppress the message warning that commands will be executed with
83 /bin/sh (this refers to the command line that runs the job script).
84
85 Call get_id() first, to extract the job ID.
86
87 """
88
89 new_err = ""
90 if err:
91 for line in err.splitlines(True):
92 if self.REC_ID_FROM_SUBMIT_ERR.match(line):
93 out += line
94 elif any(rec.match(line) for rec in self.REC_ERR_FILTERS):
95 continue
96 elif line.strip() in self.ERR_NO_ATD_STRS:
97 raise OSError(
98 errno.ESRCH, os.strerror(errno.ESRCH), line)
99 else:
100 new_err += line
101 return out, new_err
102
103 @classmethod
104 def get_submit_stdin(cls, job_file_path, submit_opts):
105 """Return proc_stdin_arg, proc_stdin_value."""
106 try:
107 return (PIPE, cls.SUBMIT_CMD_STDIN_TMPL_2 % {
108 "job": job_file_path,
109 "execution_time_limit": submit_opts["execution_time_limit"]})
110 except KeyError:
111 return (PIPE, cls.SUBMIT_CMD_STDIN_TMPL % {"job": job_file_path})
112
113
114 JOB_RUNNER_HANDLER = AtCommandHandler()