Debian Codegraph @

gavo/rscdef/executing.py

raw
1 """
2 The execute element and related stuff.
3 """
4
5 #c Copyright 2008-2025, the GAVO project <[email protected]>
6 #c
7 #c This program is free software, covered by the GNU GPL. See the
8 #c COPYING file in the source distribution.
9
10
11 import os
12 import re
13 import subprocess
14 import sys
15 import threading
16 import traceback
17
18 from gavo import base
19 from gavo.base import cron
20 from gavo.rscdef import common
21 from gavo.rscdef import procdef
22
23
24 class GuardedFunctionFactory(object):
25 """a class for making functions safe for cron-like executions.
26
27 The main method is makeGuarded. It introduces a lock protecting against
28 double execution (if that were to happen, the execution is suppressed with a
29 warning; of course, if you fork something into the background, that mechanism
30 no longer works). The stuff is run in a thread, and exceptions caught. If
31 anything goes wrong during execution, a mail is sent to the administrator.
32
33 Note that, in contrast to cron, I/O is not captured (that would
34 be difficult for threads; we don't want processes because of
35 the potential trouble with database connections).
36
37 There's a module-private instance of this that's used by Execute.
38 """
39 def __init__(self):
40 self.threadsCurrentlyActive = []
41 self.activeListLock = threading.Lock()
42
43 def _reapOldThreads(self):
44 if len(self.threadsCurrentlyActive)>10:
45 base.ui.notifyWarning("There's a suspicious number of cron"
46 " threads active (%d). You should check what's going on."%
47 len(self.threadsCurrentlyActive))
48
49 newThreads = []
50 with self.activeListLock:
51 for t in self.threadsCurrentlyActive:
52 if t.is_alive():
53 newThreads.append(t)
54 else:
55 t.join(timeout=0.001)
56 self.threadsCurrentlyActive = newThreads
57
58 def makeGuardedThreaded(self, callable, execDef):
59 """returns callable ready for safe cron-like execution.
60
61 execDef is an Execute instance.
62 """
63 serializingLock = threading.Lock()
64
65 def innerFunction():
66 try:
67 try:
68 execDef.outputAccum = []
69 callable(execDef.rd, execDef)
70 except Exception:
71 base.ui.notifyError("Uncaught exception in timed job %s."
72 " Trying to send traceback to the maintainer."%execDef.jobName)
73 cron.sendMailToAdmin("DaCHS Job %s failed"%execDef.jobName,
74 "".join(traceback.format_exception(*sys.exc_info())))
75 finally:
76 serializingLock.release()
77 if execDef.debug and execDef.outputAccum:
78 cron.sendMailToAdmin("Debug output of DaCHS Job %s"%execDef.jobName,
79 "\n".join(execDef.outputAccum), debug=True)
80 del execDef.outputAccum
81
82 def cronFunction():
83 self._reapOldThreads()
84 if not serializingLock.acquire(False):
85 base.ui.notifyWarning("Timed job %s has not finished"
86 " before next instance came around"%execDef.jobName)
87 return
88 t = threading.Thread(name=execDef.title, target=innerFunction)
89 base.ui.notifyInfo("Spawning thread for cron job %s"%execDef.title)
90 t.daemon = True
91 t.start()
92
93 with self.activeListLock:
94 self.threadsCurrentlyActive.append(t)
95
96 return t
97
98 return cronFunction
99
100 _guardedFunctionFactory = GuardedFunctionFactory()
101
102
103 class CronJob(procdef.ProcApp):
104 """Python code for use within execute.
105
106 The resource descriptor this runs at is available as rd, the execute
107 definition (having such attributes as title, job, plus any
108 properties given in the RD) as execDef.
109
110 Note that no I/O capturing takes place (that's impossible since in
111 general the jobs run within the server). To have actual cron jobs,
112 use ``execDef.spawn(["cmd", "arg1"...])``. This will send a mail on failed
113 execution and also raise a ReportableError in that case.
114
115 In the frequent use case of a resdir-relative python program, you
116 can use the ``execDef.spawnPython(modulePath)`` function.
117
118 If you must stay within the server process, you can do something like::
119
120 mod, _ = utils.loadPythonModule(rd.getAbsPath("bin/coverageplot"))
121 mod.makePlot()
122
123 -- in that way, your code can sit safely within the resource directory
124 and you still don't have to manipulate the module path.
125 """
126 name_ = "job"
127 formalArgs = "rd, execDef"
128
129
130 class Execute(base.Structure, base.ExpansionDelegator):
131 """a container for calling code.
132
133 This is a cron-like functionality. The jobs are run in separate
134 threads, so they need to be thread-safe with respect to the
135 rest of DaCHS. DaCHS serializes calls, though, so that your
136 code should never run twice at the same time.
137
138 At least on CPython, you must make sure your code does not
139 block with the GIL held; this is still in the server process.
140 If you do daring things, fork off (note that you must not use
141 any database connections you may have after forking, which means
142 you can't safely use the RD passed in). See the docs on `Element job`_.
143
144 Then testing/debugging such code, use ``gavo admin execute rd#id``
145 to immediately run the jobs.
146 """
147 name_ = "execute"
148
149 _title = base.UnicodeAttribute("title",
150 default = base.Undefined,
151 description="Some descriptive title for the job; this is used"
152 " in diagnostics.",
153 copyable=False,)
154
155 _at = base.StringListAttribute("at",
156 default=base.NotGiven,
157 description="One or more hour:minute pairs at which to run"
158 " the code each day. This conflicts with every. Optionally,"
159 " you can prefix each time by one of m<dom> or w<dow> for"
160 " jobs only to be executed at some day of the month or week, both"
161 " counted from 1. So, 'm22 7:30, w3 15:02' would execute on"
162 " the 22nd of each month at 7:30 UTC and on every wednesday at 15:02.",
163 copyable=True,)
164
165 _every = base.IntAttribute("every",
166 default=base.NotGiven,
167 description="Run the job roughly every this many seconds."
168 " This conflicts with at. Note that the first execution of"
169 " such a job is after every/10 seconds, and that the timers"
170 " start anew at every server restart. So, if you restart"
171 " often, these jobs may run much more frequently or not at all"
172 " if the interval is large. If every is smaller than zero, the"
173 " job will be executed immediately when the RD is being loaded and is"
174 " then run every abs(every) seconds",
175 copyable=True,)
176
177 _on = base.EnumeratedUnicodeAttribute("on",
178 default=base.NotGiven,
179 validValues=["loaded"],
180 description="Run the job when a certain event happens. Right now,"
181 " only 'loaded' is supported: the code is executed when the RD"
182 " has finished parsing (since 2.11).",
183 copyable=True)
184
185 _job = base.StructAttribute("job",
186 childFactory=CronJob,
187 default=base.Undefined,
188 description="The code to run.",
189 copyable=True,)
190
191 _debug = base.BooleanAttribute("debug",
192 description="If true, on execution of external processes (span or"
193 " spawnPython), the output will be accumulated and mailed to"
194 " the administrator. Note that output of the actual cron job"
195 " itself is not caught (it might turn up in serverStderr)."
196 " You could use execDef.outputAccum.append(<stuff>) to have"
197 " information from within the code included.", default=False)
198
199 _properties = base.PropertyAttribute()
200
201 _rd = common.RDAttribute()
202
203 def spawn(self, cliList):
204 """spawns an external command, capturing the output and mailing it
205 to the admin if it failed.
206
207 Output is buffered and mailed, so it shouldn't be too large.
208
209 This does not raise an exception if the command fails (in normal usage,
210 this would cause two mails to be sent). Instead, it returns the
211 returncode of the spawned process; if that's 0, you're ok. But
212 in general, you wouldn't want to check it, as on failure you'll
213 already receive a (more informative) mail.
214 """
215 p = subprocess.Popen(cliList,
216 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
217 stderr=subprocess.STDOUT, close_fds=True)
218 childOutput, _ = p.communicate()
219 if p.returncode:
220 cron.sendMailToAdmin("A process spawned by %s failed with %s"%(
221 self.title, p.returncode),
222 "Output of %s:\n\n%s"%(cliList, childOutput.decode("ascii", "ignore")))
223
224 elif self.debug:
225 if childOutput:
226 self.outputAccum.append("\n\n%s -> %s\n"%(cliList, p.returncode))
227 self.outputAccum.append(childOutput)
228
229 return p.returncode
230
231 def spawnPython(self, pythonFile):
232 """spawns a new python interpreter executing pythonFile.
233
234 pythonFile may be resdir-relative.
235 """
236 self.spawn(["python3", os.path.join(self.rd.resdir, pythonFile)])
237
238 def _parseAt(self, atSpec, ctx):
239 """returns a tuple ready for cron.repeatAt from atSpec.
240
241 see the at StringListAttribute for how it would look like; this
242 parses one element of that string list.
243 """
244 mat = re.match(r"(?P<dow>w\d\s+)?"
245 r"(?P<dom>m\d\d?\s+)?"
246 r"(?P<hr>\d+):(?P<min>\d+)", atSpec)
247 if not mat:
248 raise base.LiteralParseError("at", atSpec, pos=ctx.pos, hint=
249 "This is hour:minute optionally prefixed by either w<weekday> or"\
250 " m<day of month>, each counted from 1.")
251
252 hour, minute = int(mat.group("hr")), int(mat.group("min"))
253 if not (0<=hour<=23 and 0<=minute<=59):
254 raise base.LiteralParseError("at", atSpec, pos=ctx.pos, hint=
255 "This must be hour:minute with 0<=hour<=23 or 0<=minute<=59")
256
257 dom = None
258 if mat.group("dom"):
259 dom = int(mat.group("dom")[1:])
260 if not 1<=dom<=28:
261 raise base.LiteralParseError("at", atSpec, pos=ctx.pos, hint=
262 "day-of-month in at must be between 1 and 28.")
263
264 dow = None
265 if mat.group("dow"):
266 dow = int(mat.group("dow")[1:])
267 if not 1<=dow<=7:
268 raise base.LiteralParseError("at", atSpec, pos=ctx.pos, hint=
269 "day-of-week in at must be between 1 and 7.")
270
271 return (dom, dow, hour, minute)
272
273 def completeElement(self, ctx):
274 if self.at is not base.NotGiven:
275 self.parsedAt = []
276 for literal in self.at:
277 self.parsedAt.append(self._parseAt(literal, ctx))
278
279 # we need the parse context for on, which onElementComplete does
280 # not normally have. We cheat it in there for now; on the long
281 # run, I think we want to unify completeElement, validate,
282 # onElementComplete, and the callCompletedCallbacks anyway.
283 self.ctx = ctx
284
285 def onElementComplete(self):
286 super().onElementComplete()
287
288 self.jobName = "%s in %s"%(self.title, self.rd.sourceId)
289
290 self.callable = _guardedFunctionFactory.makeGuardedThreaded(
291 self.job.compile(), self)
292
293 if self.at is not base.NotGiven:
294 cron.repeatAt(self.parsedAt, self.jobName, self.callable)
295
296 elif self.every is not base.NotGiven:
297 cron.runEvery(self.every, self.jobName, self.callable)
298
299 elif self.on=="loaded":
300 # self.ctx set in completeElement
301 del self.callable
302 self.ctx.addExitFunc(lambda rd, ctx: self.job.compile()(rd, self))
303
304 else:
305 raise NotImplementedError("This cannot happen")
306
307 del self.ctx
308
309 def validate(self):
310 super().validate()
311 if len([s for s in [self.at, self.every, self.on]
312 if s is not base.NotGiven])!=1:
313 raise base.StructureError("Exactly one of {at, on, every} required"
314 " for execute")
315
316 def runImmediate(self):
317 """compiles and executes the job without any scaffolding.
318
319 That's for debugging only, don't do it in a running server.
320 """
321 callable = self.job.compile()
322 callable(self.rd, self)