Files
37duAutomation/webhost/backend/winjob.py
T
puddy 42c7cf24f2 feat(controlplane): 完成多页面管理台重构与平台权限控制
1. 重构控制面前端为多页面路由架构,拆分出概览/插件/账号/权限管理页面
2. 引入Element Plus组件库优化UI样式与交互
3. 新增平台登录权限校验,支持按账号授权HR/OA平台访问
4. 新增平台登出接口与个人中心功能
5. 完善账号管理、插件授权相关API与UI交互
6. 新增防火墙、启停、自启动脚本与部署文档
2026-09-10 15:03:21 +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