Debian Codegraph @

cylc/flow/job_runner_handlers/background.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 """Runs job scripts as Unix background processes.
17
18 .. cylc-scope:: flow.cylc[runtime][<namespace>]
19
20 If an :cylc:conf:`execution time limit` is specified for a task, its job will
21 be wrapped by the ``timeout`` command.
22
23 .. cylc-scope::
24
25 """
26
27 import errno
28 import os
29 import re
30 from subprocess import Popen, STDOUT, DEVNULL
31
32
33 class BgCommandHandler:
34 """Background job submission and manipulation.
35
36 Run a task job as a nohup background process in its own process group.
37
38 """
39
40 SHOULD_KILL_PROC_GROUP = True
41 POLL_CMD = "ps"
42 REC_ID_FROM_SUBMIT_OUT = re.compile(r"""\A(?P<id>\d+)\Z""")
43
44 @classmethod
45 def submit(cls, job_file_path, submit_opts):
46 """Submit "job_file_path"."""
47 # Check access permission here because we are unable to check the
48 # result of the nohup command.
49 if not os.access(job_file_path, os.R_OK | os.X_OK):
50 exc = OSError(
51 errno.EACCES, os.strerror(errno.EACCES), job_file_path)
52 return (1, None, str(exc))
53 job_file_path_dir = os.path.dirname(job_file_path)
54 if not os.access(job_file_path_dir, os.W_OK):
55 exc = OSError(
56 errno.EACCES, os.strerror(errno.EACCES), job_file_path_dir)
57 return (1, None, str(exc))
58 try:
59 # Run command with "timeout" if execution time limit set
60 execution_time_limit = submit_opts.get("execution_time_limit")
61 timeout_str = ""
62 if execution_time_limit:
63 timeout_str = (
64 " timeout --signal=XCPU %d" % execution_time_limit)
65 # This is essentially a double fork to ensure that the child
66 # process can detach as a process group leader and not subjected to
67 # SIGHUP from the current process.
68 # TODO: close stdout? Maybe atexit?
69 proc = Popen( # nosec
70 [
71 "nohup",
72 "bash",
73 "-c",
74 (r'''exec%s "$0" <'/dev/null' >"$0.out" 2>"$0.err"''' %
75 timeout_str),
76 job_file_path,
77 ],
78 env=submit_opts.get('env'),
79 preexec_fn=os.setpgrp,
80 stdin=DEVNULL,
81 stdout=DEVNULL,
82 stderr=STDOUT
83 )
84 # * the purpose of this call is to run user defined code
85 except OSError as exc:
86 # subprocess.Popen has a bad habit of not setting the
87 # filename of the executable when it raises an OSError.
88 if not exc.filename:
89 exc.filename = "nohup"
90 return (1, None, str(exc))
91 else:
92 return (0, str(proc.pid), None)
93
94
95 JOB_RUNNER_HANDLER = BgCommandHandler()