Files
lc-mcp/lc_mcp/tasks.py
T
ywsandClaude Opus 5.5 a4b27f991c v0.7: 以 AALC 为底座重建 MCP server
- AALC 作为 submodule (vendor/aalc @ 7452fad), 不改其代码
- 低层工具走 AALC auto/ocr/模板匹配; 高层 run_task 跑 AALC 现成流程(子进程, 可强杀)
- 常驻服务跑在交互桌面会话 + stdio bridge (token 认证), 取代每次截图注册计划任务
- 删除自写的截图/输入/OCR 后端
- 测试: FakeEngine 覆盖协议、任务生命周期、TCP+bridge 端到端

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 01:39:06 +00:00

108 lines
4.2 KiB
Python

# -*- coding: utf-8 -*-
"""长任务管理: 同一时刻最多一个 AALC 任务子进程。"""
import glob
import json
import os
import signal
import subprocess
import sys
import threading
import time
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
IS_WIN = sys.platform.startswith('win')
def default_argv(task, params):
return [sys.executable, '-m', 'lc_mcp.aalc_task', task, json.dumps(params)]
def tail(path, n):
try:
with open(path, 'rb') as f:
f.seek(0, os.SEEK_END)
f.seek(max(0, f.tell() - 64 * 1024))
lines = f.read().decode('utf-8', 'replace').splitlines()
return lines[-n:]
except OSError:
return []
class TaskRunner:
def __init__(self, run_dir, argv_factory=default_argv, keep_logs=20):
self.run_dir = os.path.abspath(run_dir)
os.makedirs(self.run_dir, exist_ok=True)
self.argv_factory = argv_factory
self.keep_logs = keep_logs
self._lock = threading.Lock()
self.proc = None
self.info = None
def running(self):
return self.proc is not None and self.proc.poll() is None
def start(self, task, params):
with self._lock:
if self.running():
return {'ok': False, 'error': '已有任务在跑: %s (先 stop_task)' % self.info['task']}
stamp = time.strftime('%Y%m%d_%H%M%S')
log_path = os.path.join(self.run_dir, 'task_%s_%s.log' % (stamp, task))
env = dict(os.environ, PYTHONPATH=REPO + os.pathsep + os.environ.get('PYTHONPATH', ''),
PYTHONIOENCODING='utf-8', PYTHONUNBUFFERED='1')
kw = {'creationflags': subprocess.CREATE_NEW_PROCESS_GROUP} if IS_WIN else {'start_new_session': True}
with open(log_path, 'wb') as logf:
self.proc = subprocess.Popen(self.argv_factory(task, params), cwd=REPO, env=env,
stdin=subprocess.DEVNULL, stdout=logf, stderr=subprocess.STDOUT, **kw)
self.info = {'task': task, 'params': params, 'log': log_path.replace('\\', '/'),
'started': time.time(), 'stopped_by_user': False}
self._prune()
return {'ok': True, 'task': task, 'pid': self.proc.pid, 'log': self.info['log']}
def status(self, lines=30):
with self._lock:
if self.info is None:
return {'ok': True, 'state': 'idle'}
code = self.proc.poll()
if code is None:
state = 'running'
elif self.info['stopped_by_user']:
state = 'stopped'
else:
state = 'succeeded' if code == 0 else 'failed'
r = {'ok': True, 'state': state, 'task': self.info['task'], 'params': self.info['params'],
'elapsed_s': int((self.info.get('ended') or time.time()) - self.info['started']),
'log': self.info['log'], 'log_tail': tail(self.info['log'], max(1, min(int(lines), 200)))}
if code is not None:
r['exit_code'] = code
self.info.setdefault('ended', time.time())
return r
def stop(self):
with self._lock:
if not self.running():
return {'ok': True, 'state': 'idle', 'note': '没有在跑的任务'}
self.info['stopped_by_user'] = True
pid = self.proc.pid
if IS_WIN:
subprocess.run(['taskkill', '/PID', str(pid), '/T', '/F'], capture_output=True)
else:
try:
os.killpg(pid, signal.SIGKILL)
except ProcessLookupError:
pass
try:
self.proc.wait(timeout=10)
except subprocess.TimeoutExpired:
return {'ok': False, 'error': '进程 %d 10 秒内没退出' % pid}
self.info['ended'] = time.time()
return {'ok': True, 'state': 'stopped', 'task': self.info['task']}
def _prune(self):
logs = sorted(glob.glob(os.path.join(self.run_dir, 'task_*.log')), key=os.path.getmtime)
for p in logs[:-self.keep_logs]:
try:
os.remove(p)
except OSError:
pass