Files
37duAutomation/desktop/webhost/backend/core/winjob.py
T
puddy 0ab43d0ed8 refactor: 完成项目目录重构与代码迁移
本次提交完成了整体项目的目录梳理与代码迁移:
1.  将根级controlplane、webhost等目录的代码迁移至desktop/对应子目录,统一桌面端架构
2.  移动并整理了插件目录结构,将插件统一放入根级plugins/
3.  调整了前端项目的路径与配置,统一前后端开发构建流程
4.  清理了废弃的旧版app/目录与冗余的构建中间文件
5.  更新了.gitignore与README.md,适配新的项目结构
2026-09-14 17:46:09 +08:00

100 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""Windows Job Object 封装:插件执行子进程整树托管。
- KILL_ON_JOB_CLOSE:句柄关闭/宿主崩溃时,作业内全部进程(含 DrissionPage 拉起的
浏览器等孙进程)一并终止,杜绝超时强杀后残留孤儿进程;
- PROCESS_MEMORY_LIMIT:作业级内存上限兜底,防插件内存爬升拖垮底座。
非 Windows 平台所有方法安全降级为 no-op。
"""
from __future__ import annotations
import ctypes
import logging
import sys
_LOGGER = logging.getLogger(__name__)
_AVAILABLE = sys.platform == "win32"
if _AVAILABLE:
try:
_K32 = ctypes.WinDLL("kernel32", use_last_error=True)
except AttributeError: # 非 Windows 误导入
_AVAILABLE = False
_JOB_OBJECT_EXTENDED_LIMIT_INFORMATION = 9
_JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x0000_2000
_JOB_OBJECT_LIMIT_PROCESS_MEMORY = 0x0000_0100
_PROCESS_SET_QUOTA = 0x0100
_PROCESS_TERMINATE = 0x0001
if _AVAILABLE:
class _IO_COUNTERS(ctypes.Structure):
_fields_ = [(n, ctypes.c_uint64) for n in (
"ReadOperationCount", "WriteOperationCount", "OtherOperationCount",
"ReadTransferCount", "WriteTransferCount", "OtherTransferCount")]
class _BASIC_LIMIT_INFO(ctypes.Structure):
_fields_ = [
("PerProcessUserTimeLimit", ctypes.c_int64),
("PerJobUserTimeLimit", ctypes.c_int64),
("LimitFlags", ctypes.c_uint32),
("MinimumWorkingSetSize", ctypes.c_size_t),
("MaximumWorkingSetSize", ctypes.c_size_t),
("ActiveProcessLimit", ctypes.c_uint32),
("Affinity", ctypes.c_size_t),
("PriorityClass", ctypes.c_uint32),
("SchedulingClass", ctypes.c_uint32),
]
class _EXT_LIMIT_INFO(ctypes.Structure):
_fields_ = [
("BasicLimitInformation", _BASIC_LIMIT_INFO),
("IoInfo", _IO_COUNTERS),
("ProcessMemoryLimit", ctypes.c_size_t),
("JobMemoryLimit", ctypes.c_size_t),
("PeakProcessMemoryUsed", ctypes.c_size_t),
("PeakJobMemoryUsed", ctypes.c_size_t),
]
class Job:
"""一次性的插件子进程作业。用法:job = Job(); job.assign(proc); …; job.terminate()/close()。"""
def __init__(self, max_mem_mb: int = 0):
self._h = None
if not _AVAILABLE:
return
h = _K32.CreateJobObjectW(None, None)
if not h:
_LOGGER.warning("创建 Job Object 失败(winerror=%s),退化为无作业托管", ctypes.get_last_error())
return
info = _EXT_LIMIT_INFO()
info.BasicLimitInformation.LimitFlags = _JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
if max_mem_mb > 0:
info.BasicLimitInformation.LimitFlags |= _JOB_OBJECT_LIMIT_PROCESS_MEMORY
info.ProcessMemoryLimit = max_mem_mb * 1024 * 1024
if not _K32.SetInformationJobObject(
h, _JOB_OBJECT_EXTENDED_LIMIT_INFORMATION,
ctypes.byref(info), ctypes.sizeof(info)):
_LOGGER.warning("设置 Job Object 限制失败(winerror=%s)", ctypes.get_last_error())
self._h = h
def assign(self, proc) -> None:
"""把子进程(及其后代)纳入作业托管。"""
if not self._h:
return
if not _K32.AssignProcessToJobObject(self._h, int(proc._handle)):
_LOGGER.warning("子进程纳入 Job Object 失败(winerror=%s)", ctypes.get_last_error())
def terminate(self, exit_code: int = 1) -> None:
"""终止作业内全部进程(整树强杀,含浏览器等孙进程)。"""
if self._h:
_K32.TerminateJobObject(self._h, exit_code)
def close(self) -> None:
"""关闭作业句柄;KILL_ON_JOB_CLOSE 保证句柄消失后作业内进程全部退出。"""
if self._h:
_K32.CloseHandle(self._h)
self._h = None