mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 01:39:52 +08:00
perf(cron): share global concurrency budget and streamline execution
Share durable permits across manual, scheduled and recovered runs, prioritize short ready tasks with aging, and record global queue occupancy. Reduce launch, log polling, dashboard and SQLite overhead while preserving transactional receipts, cancellation, recovery and script hook compatibility. Keep ordinary CRUD in ORM and bound SQL on measured execution hot paths. Fix supervisor descendant tracking, asynchronous log draining, concurrent log directory creation, Linux fallback launches and snapshot clock rollback. Include regression coverage, SQL audits and evolution/review documentation. Validation: five review rounds; local full suite 1478 passed, 28 platform skips; dev suites 415 passed, no failures/skips; backend types and formatting passed.
This commit is contained in:
1 parent
cddabbaf0e
commit
2f01f756d7
74 files changed
+6891
-837
No files matched your search
+195
-8
@@ -1,7 +1,6 @@
|
||||
"""Wait for launch authorization, then preserve a command's exit across worker restarts."""
|
||||
import errno
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
@@ -58,6 +57,195 @@ def sync_directory(directory):
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def start_log_relay(source, log_path):
|
||||
# Keep draining after the supervisor or its task tree is terminated. The
|
||||
# relay owns no execution permission and exits at pipe EOF. Double-fork
|
||||
# before launching Bash so it is outside the task's cancellation tree.
|
||||
ready, done = os.pipe()
|
||||
try:
|
||||
intermediate = os.fork()
|
||||
except BaseException:
|
||||
os.close(ready)
|
||||
os.close(done)
|
||||
raise
|
||||
if intermediate == 0:
|
||||
try:
|
||||
os.close(ready)
|
||||
os.setsid()
|
||||
if os.fork():
|
||||
os._exit(0)
|
||||
# Never retain the authorization/telemetry pipes, stdout, or the
|
||||
# write end of our own input. EOF must mean all writers have gone.
|
||||
keep = {source.fileno(), done, 2}
|
||||
if sys.platform == 'linux':
|
||||
for entry in os.listdir('/proc/self/fd'):
|
||||
fd = int(entry)
|
||||
if fd not in keep:
|
||||
try:
|
||||
os.close(fd)
|
||||
except OSError as error:
|
||||
if error.errno != errno.EBADF:
|
||||
raise
|
||||
else:
|
||||
import resource
|
||||
previous = 0
|
||||
for fd in sorted(keep):
|
||||
os.closerange(previous, fd)
|
||||
previous = fd + 1
|
||||
maximum = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
|
||||
if maximum == resource.RLIM_INFINITY:
|
||||
maximum = os.sysconf('SC_OPEN_MAX')
|
||||
os.closerange(previous, maximum)
|
||||
os.write(done, b'R')
|
||||
relay_log(source, log_path)
|
||||
# EPIPE is expected if the supervisor was killed while we drained.
|
||||
try:
|
||||
os.write(done, b'D')
|
||||
except BrokenPipeError:
|
||||
pass
|
||||
except BaseException as error:
|
||||
report_log_error(error)
|
||||
finally:
|
||||
os._exit(0)
|
||||
os.close(done)
|
||||
source.close()
|
||||
os.waitpid(intermediate, 0)
|
||||
if os.read(ready, 1) != b'R':
|
||||
os.close(ready)
|
||||
raise OSError('Could not start command log relay')
|
||||
|
||||
def join():
|
||||
try:
|
||||
if os.read(ready, 1) != b'D':
|
||||
raise OSError('Command log relay ended before draining output')
|
||||
finally:
|
||||
os.close(ready)
|
||||
|
||||
return join
|
||||
|
||||
|
||||
def spawn_command(command, merged, log_path=None):
|
||||
if not log_path:
|
||||
return spawn_command_pipes(command, merged)
|
||||
read_fd, write_fd = os.pipe()
|
||||
source = os.fdopen(read_fd, 'rb')
|
||||
join = None
|
||||
try:
|
||||
# Ready handshake precedes command side effects, including fast writers.
|
||||
join = start_log_relay(source, log_path)
|
||||
output, error, wait_command = spawn_command_pipes(command, True, write_fd)
|
||||
except BaseException:
|
||||
os.close(write_fd)
|
||||
source.close()
|
||||
if join:
|
||||
join()
|
||||
raise
|
||||
os.close(write_fd)
|
||||
|
||||
def wait():
|
||||
status = wait_command()
|
||||
# A background writer is still part of this run. Keep the supervisor's
|
||||
# birth identity, global permit and cancellation target until EOF; only
|
||||
# then may main publish a terminal receipt.
|
||||
join()
|
||||
return status
|
||||
|
||||
return output, error, wait
|
||||
|
||||
|
||||
def spawn_command_pipes(command, merged, destination=None):
|
||||
libc = None
|
||||
if sys.platform == 'linux':
|
||||
import ctypes
|
||||
# Both launch paths must adopt orphaned grandchildren. Output EOF is
|
||||
# insufficient: a background command can redirect or close its output.
|
||||
# The log relay detached before this flag is enabled.
|
||||
libc = ctypes.CDLL(None, use_errno=True)
|
||||
previous_reaper = ctypes.c_int()
|
||||
if libc.prctl(37, ctypes.byref(previous_reaper), 0, 0, 0) != 0 or libc.prctl(36, 1, 0, 0, 0) != 0:
|
||||
raise OSError(ctypes.get_errno(), 'Could not supervise command descendants')
|
||||
# Keep native Linux launch without importing subprocess/locale/selectors.
|
||||
# File writes belong to the relay: ENOSPC/EFBIG must not reach the script.
|
||||
if (sys.platform != 'linux' or not hasattr(os, 'posix_spawn')
|
||||
or not hasattr(os, 'waitstatus_to_exitcode')):
|
||||
import subprocess
|
||||
try:
|
||||
child = subprocess.Popen(['/bin/bash', '-c', command], stdin=subprocess.DEVNULL,
|
||||
stdout=destination if destination is not None else subprocess.PIPE,
|
||||
stderr=destination if destination is not None else (subprocess.STDOUT if merged else subprocess.PIPE))
|
||||
except BaseException:
|
||||
if libc is not None:
|
||||
libc.prctl(36, previous_reaper.value, 0, 0, 0)
|
||||
raise
|
||||
|
||||
def wait_fallback():
|
||||
status = child.wait()
|
||||
if libc is not None:
|
||||
while True:
|
||||
try:
|
||||
os.waitpid(-1, 0)
|
||||
except ChildProcessError:
|
||||
break
|
||||
return status
|
||||
|
||||
return child.stdout, child.stderr, wait_fallback
|
||||
|
||||
import signal
|
||||
descriptors = []
|
||||
try:
|
||||
if destination is not None:
|
||||
output = error = None
|
||||
output_write = error_write = destination
|
||||
else:
|
||||
output, output_write = os.pipe()
|
||||
descriptors.extend((output, output_write))
|
||||
if merged:
|
||||
error, error_write = None, output_write
|
||||
else:
|
||||
error, error_write = os.pipe()
|
||||
descriptors.extend((error, error_write))
|
||||
actions = [(os.POSIX_SPAWN_OPEN, 0, os.devnull, os.O_RDONLY, 0),
|
||||
(os.POSIX_SPAWN_DUP2, output_write, 1),
|
||||
(os.POSIX_SPAWN_DUP2, error_write, 2)]
|
||||
# Include the start telemetry pipe and any descriptors inherited from the
|
||||
# caller. The readdir descriptor is already closed; skip that stale entry.
|
||||
for entry in os.listdir('/proc/self/fd'):
|
||||
fd = int(entry)
|
||||
if fd < 3:
|
||||
continue
|
||||
try:
|
||||
os.fstat(fd)
|
||||
except OSError as exc:
|
||||
if exc.errno == errno.EBADF:
|
||||
continue
|
||||
raise
|
||||
actions.append((os.POSIX_SPAWN_CLOSE, fd))
|
||||
defaults = tuple(getattr(signal, name) for name in ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ')
|
||||
if hasattr(signal, name))
|
||||
pid = os.posix_spawn('/bin/bash', ['/bin/bash', '-c', command], os.environ,
|
||||
file_actions=actions, setsigdef=defaults)
|
||||
except BaseException:
|
||||
libc.prctl(36, previous_reaper.value, 0, 0, 0)
|
||||
for fd in descriptors:
|
||||
os.close(fd)
|
||||
raise
|
||||
if destination is None:
|
||||
os.close(output_write)
|
||||
if error_write != output_write:
|
||||
os.close(error_write)
|
||||
|
||||
def wait():
|
||||
status = os.waitstatus_to_exitcode(os.waitpid(pid, 0)[1])
|
||||
while True:
|
||||
try:
|
||||
os.waitpid(-1, 0)
|
||||
except ChildProcessError:
|
||||
break
|
||||
return status
|
||||
|
||||
return os.fdopen(output, 'rb') if output is not None else None, os.fdopen(error, 'rb') if error is not None else None, wait
|
||||
|
||||
|
||||
def main():
|
||||
command, owner, marker, node, reporter = sys.argv[1:6]
|
||||
if sys.stdin.buffer.read() != b'run\n':
|
||||
@@ -67,21 +255,19 @@ def main():
|
||||
if report_start:
|
||||
options = options[1:]
|
||||
log_path = options[1] if len(options) == 2 and options[0] == 'log-output' else None
|
||||
child = subprocess.Popen(['/bin/bash', '-c', command], stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT if log_path else subprocess.PIPE)
|
||||
output, error, wait = spawn_command(command, bool(log_path), log_path)
|
||||
if report_start:
|
||||
try:
|
||||
with os.fdopen(3, 'wb', buffering=0) as timing:
|
||||
timing.write(str(time.time_ns() // 1000000).encode('ascii'))
|
||||
except OSError:
|
||||
pass
|
||||
streams = ([threading.Thread(target=relay_log, args=(child.stdout, log_path))]
|
||||
if log_path else [threading.Thread(target=relay, args=(child.stdout, 1)),
|
||||
threading.Thread(target=relay, args=(child.stderr, 2))])
|
||||
streams = (([] if output is None else [threading.Thread(target=relay_log, args=(output, log_path))])
|
||||
if log_path else [threading.Thread(target=relay, args=(output, 1)),
|
||||
threading.Thread(target=relay, args=(error, 2))])
|
||||
for stream in streams:
|
||||
stream.start()
|
||||
status = child.wait()
|
||||
status = wait()
|
||||
finished = time.time_ns() // 1000000
|
||||
|
||||
import hashlib
|
||||
@@ -143,6 +329,7 @@ def main():
|
||||
needs_report = True
|
||||
print('[scheduler] Could not save command exit:', error, file=sys.stderr)
|
||||
if needs_report or os.getppid() != int(owner):
|
||||
import subprocess
|
||||
env = os.environ.copy()
|
||||
env.pop('NODE_OPTIONS', None)
|
||||
# Even with a live parent its child-close callback has no exit timestamp.
|
||||
|
||||
Reference in new issue
Block a user