Merge branch 'cache2'
This commit is contained in:
commit
eb9cfaa63d
32 files changed
+1927
-87
No files matched your search
@@ -1,5 +1,11 @@
|
||||
import { createCipheriv, createDecipheriv, createHmac, randomBytes, randomUUID } from "node:crypto";
|
||||
import { DEFAULT_SETTINGS, GlobalSettings, type SecretPurpose, type SettingsKey } from "@umt/contracts";
|
||||
import {
|
||||
DEFAULT_SETTINGS,
|
||||
GlobalSettings,
|
||||
type SecretPurpose,
|
||||
type SettingsKey,
|
||||
withStorageDefaults,
|
||||
} from "@umt/contracts";
|
||||
import type { DB, Kysely } from "@umt/db";
|
||||
import type { Logger } from "pino";
|
||||
import type { EventBusPort } from "../../http/sse.ts";
|
||||
@@ -335,7 +341,13 @@ export function createSettingsReader(deps: {
|
||||
for (const key of Object.keys(DEFAULT_SETTINGS) as SettingsKey[]) {
|
||||
const def = DEFAULT_SETTINGS[key];
|
||||
const v = stored.get(key);
|
||||
const merged = v && typeof v === "object" && !Array.isArray(v) ? { ...def, ...(v as object) } : def;
|
||||
// storage 是两层结构:逐层补缺省,旧库里缺子字段也能整体通过校验
|
||||
const merged =
|
||||
key === "storage"
|
||||
? withStorageDefaults(v)
|
||||
: v && typeof v === "object" && !Array.isArray(v)
|
||||
? { ...def, ...(v as object) }
|
||||
: def;
|
||||
const parsed = GlobalSettings.shape[key].safeParse(merged);
|
||||
if (!parsed.success) deps.logger.warn({ key }, "settings 值不合法,回退缺省");
|
||||
out[key] = parsed.success ? parsed.data : def;
|
||||
|
||||
+51
-4
@@ -9,6 +9,8 @@ import { TMDB_IMAGE_SIZES } from "@umt/contracts";
|
||||
// - 读缓存只在 use_cache(GLOBAL_IMAGE_CACHE)开启时;拉到的合法图片总是写盘(与 MP 一致);
|
||||
// - 合法图片按内容识别 MIME,拒绝非图片与 SVG;ETag = md5(内容),Cache-Control: public, max-age=7 天,If-None-Match 相同回 304;
|
||||
// - 清理:删除 mtime 早于 GLOBAL_IMAGE_CACHE_DAYS 天的文件与空目录(MP clear_temp → SystemUtils.clear)。
|
||||
// - CACHE-2 容量 LRU(我们加的):读盘命中时把 atime 显式写成当前时间(不依赖挂载的 relatime),
|
||||
// 超过 storage.budgets.imagesGb 时按 atime 从旧到新删到预算的 90%;按天清理保留作兜底。
|
||||
// 我们只代理 TMDB 图片域(MP SECURITY_IMAGE_DOMAINS 的 TMDB 项),上游失败回 502。
|
||||
|
||||
export const TMDB_IMAGE_HOST = "image.tmdb.org";
|
||||
@@ -51,6 +53,13 @@ export function imageMimeType(b: Uint8Array): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
interface ImageFile {
|
||||
path: string;
|
||||
size: number;
|
||||
mtimeMs: number;
|
||||
atimeMs: number;
|
||||
}
|
||||
|
||||
export interface ImageCacheOptions {
|
||||
/** DATA_DIR/cache/images */
|
||||
dir: string;
|
||||
@@ -104,7 +113,10 @@ export class ImageCache {
|
||||
if (useCache) {
|
||||
const cached = await fs.readFile(path).catch(() => null);
|
||||
const mime = cached?.length ? imageMimeType(cached) : null;
|
||||
if (cached && mime) return { content: cached, mime };
|
||||
if (cached && mime) {
|
||||
void this.touch(path);
|
||||
return { content: cached, mime };
|
||||
}
|
||||
}
|
||||
let res: Response;
|
||||
try {
|
||||
@@ -130,8 +142,18 @@ export class ImageCache {
|
||||
return { content, mime };
|
||||
}
|
||||
|
||||
private async files(): Promise<{ path: string; size: number; mtimeMs: number }[]> {
|
||||
const out: { path: string; size: number; mtimeMs: number }[] = [];
|
||||
/** 记一次访问:atime = 现在,mtime 不变(按天清理仍按写入时间) */
|
||||
private async touch(path: string): Promise<void> {
|
||||
try {
|
||||
const st = await fs.stat(path);
|
||||
await fs.utimes(path, new Date(), st.mtime);
|
||||
} catch {
|
||||
// 文件刚被清理等:忽略
|
||||
}
|
||||
}
|
||||
|
||||
private async files(): Promise<ImageFile[]> {
|
||||
const out: ImageFile[] = [];
|
||||
const walk = async (d: string) => {
|
||||
let names: string[];
|
||||
try {
|
||||
@@ -144,7 +166,7 @@ export class ImageCache {
|
||||
const st = await fs.stat(p).catch(() => null);
|
||||
if (!st) continue;
|
||||
if (st.isDirectory()) await walk(p);
|
||||
else out.push({ path: p, size: st.size, mtimeMs: st.mtimeMs });
|
||||
else if (!n.endsWith(".tmp")) out.push({ path: p, size: st.size, mtimeMs: st.mtimeMs, atimeMs: st.atimeMs });
|
||||
}
|
||||
};
|
||||
await walk(this.opts.dir);
|
||||
@@ -156,6 +178,31 @@ export class ImageCache {
|
||||
return { bytes: f.reduce((a, x) => a + x.size, 0), files: f.length };
|
||||
}
|
||||
|
||||
/**
|
||||
* 容量 LRU:总量超过 maxBytes 时按最近访问(atime)从旧到新删除,直到不超过 maxBytes × lowWater;
|
||||
* 没超不动。返回删除数、释放字节与清理后用量。
|
||||
*/
|
||||
async enforceBudget(
|
||||
maxBytes: number,
|
||||
lowWater = 0.9,
|
||||
): Promise<{ deleted: number; freedBytes: number; usedBytes: number }> {
|
||||
const files = await this.files();
|
||||
let used = files.reduce((a, f) => a + f.size, 0);
|
||||
if (used <= maxBytes) return { deleted: 0, freedBytes: 0, usedBytes: used };
|
||||
const target = Math.floor(maxBytes * lowWater);
|
||||
files.sort((a, b) => a.atimeMs - b.atimeMs || a.mtimeMs - b.mtimeMs);
|
||||
let deleted = 0;
|
||||
let freed = 0;
|
||||
for (const f of files) {
|
||||
if (used <= target) break;
|
||||
await fs.rm(f.path, { force: true });
|
||||
used -= f.size;
|
||||
freed += f.size;
|
||||
deleted++;
|
||||
}
|
||||
return { deleted, freedBytes: freed, usedBytes: used };
|
||||
}
|
||||
|
||||
/** SystemUtils.clear(images, days):删 mtime 早于 days 天的文件,再删空目录;返回删除数 */
|
||||
async clean(days: number, now = Date.now()): Promise<number> {
|
||||
let deleted = 0;
|
||||
|
||||
+23
-2
@@ -12,7 +12,7 @@ import type { z } from "zod";
|
||||
import { dataPaths } from "../../config.ts";
|
||||
import { requireScope } from "../../http/auth.ts";
|
||||
import { errorBody } from "../../http/errors.ts";
|
||||
import { CACHE_REGIONS, sharedCache, tmdbCachePolicy } from "../../shared-cache.ts";
|
||||
import { CACHE_REGIONS, sharedCache, storageSettings, tmdbCachePolicy } from "../../shared-cache.ts";
|
||||
import { registerScheduledJob } from "../schedules/registry.ts";
|
||||
import type { ModuleApp, ServerContext, ServerModule } from "../types.ts";
|
||||
import { ImageCache, ImageProxyError } from "./images.ts";
|
||||
@@ -22,7 +22,8 @@ import { ImageCache, ImageProxyError } from "./images.ts";
|
||||
* - PG 缓存 kv_cache(UNLOGGED,移植 MP TTLCache)的过期行物理清理任务 cache.sweep(每 10 分钟);
|
||||
* - TMDB 缓存管理(对照 MP tmdb_cache_items / tmdb_cache_clear):GET /cache/tmdb、POST /cache/tmdb/clear;
|
||||
* - 图片代理 GET /img/tmdb/{size}/{file}(会话或 API Key read,移植 MP ImageHelper / fetch_image)+ DATA_DIR/cache/images 落盘,
|
||||
* 用量 / 清空 GET /cache/images、POST /cache/images/clear,按保留天数清理的任务 cache.images_clean(每天)。
|
||||
* 用量 / 清空 GET /cache/images、POST /cache/images/clear,按保留天数清理的任务 cache.images_clean(每天);
|
||||
* - CACHE-2:图片缓存容量 LRU 任务 cache.images_lru(30 分钟,预算 storage.budgets.imagesGb,超出删最久未访问)。
|
||||
*/
|
||||
export interface CacheModuleOptions {
|
||||
/** 图片上游 fetch(测试注入) */
|
||||
@@ -203,6 +204,26 @@ export function createCacheModule(opts: CacheModuleOptions = {}): ServerModule &
|
||||
return { summary: `删除 ${n} 张过期图片` };
|
||||
},
|
||||
});
|
||||
registerScheduledJob(ctx, {
|
||||
key: "cache.images_lru",
|
||||
defaults: {
|
||||
name: "图片缓存容量控制",
|
||||
description: "图片代理缓存超过容量预算(存储设置,缺省 20 GB)时,按最久未访问删除到预算的 90%",
|
||||
group: "maintain",
|
||||
schedule: { type: "interval", everyMs: 30 * 60_000 },
|
||||
firstRun: "after_interval",
|
||||
},
|
||||
run: async () => {
|
||||
const { imagesGb } = (await storageSettings(ctx)).budgets;
|
||||
const r = await images(ctx).enforceBudget(Math.round(imagesGb * 1024 ** 3));
|
||||
const gb = (b: number) => `${Math.round((b / 1024 ** 3) * 100) / 100} GB`;
|
||||
return {
|
||||
summary: r.deleted
|
||||
? `删除 ${r.deleted} 张最久未访问的图片,释放 ${gb(r.freedBytes)},现用 ${gb(r.usedBytes)}`
|
||||
: `现用 ${gb(r.usedBytes)},未超过预算 ${imagesGb} GB`,
|
||||
};
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -15,7 +15,14 @@ import {
|
||||
} from "@umt/adapters";
|
||||
import { DEFAULT_SETTINGS, type SitesSettings } from "@umt/contracts";
|
||||
import { pgTokenBucketFor, type SiteRow, sql } from "@umt/db";
|
||||
import { CACHE_REGIONS, sharedCache, type TmdbCachePolicy, tmdbCachePolicy } from "../../shared-cache.ts";
|
||||
import {
|
||||
CACHE_REGIONS,
|
||||
renewTrackedTmdbDetail,
|
||||
sharedCache,
|
||||
type TmdbCachePolicy,
|
||||
tmdbCachePolicy,
|
||||
tmdbDetailRef,
|
||||
} from "../../shared-cache.ts";
|
||||
import type { ServerContext } from "../types.ts";
|
||||
|
||||
// 模块运行时依赖:站点适配器与限流器(每站一个实例,令牌在 PG 跨进程共享)、RSS 抓取器、TMDB 客户端、时钟。
|
||||
@@ -108,7 +115,11 @@ export class SourcesRuntime {
|
||||
...(baseUrl ? { baseUrl } : {}),
|
||||
...(this.overrides.fetch ? { fetch: this.overrides.fetch } : {}),
|
||||
// 请求响应进 PG 缓存(region tmdb.request),三进程共享;TTL / 条数上限 / 空结果短 TTL 取设置(对照 MP)
|
||||
cache: { ttlMs: policy.ttl * 1000, emptyTtlMs: policy.emptyTtl * 1000 },
|
||||
cache: {
|
||||
ttlMs: policy.ttl * 1000,
|
||||
searchTtlMs: policy.searchTtl * 1000,
|
||||
emptyTtlMs: policy.emptyTtl * 1000,
|
||||
},
|
||||
sharedCache: tmdbSharedCache(this.ctx, policy),
|
||||
})
|
||||
: null;
|
||||
@@ -276,17 +287,27 @@ export class SourcesRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
/** TMDB 请求响应缓存:PG kv_cache region=tmdb.request;PG 出错按未命中 / 不写处理,不让 TMDB 请求失败 */
|
||||
/**
|
||||
* TMDB 请求响应缓存:PG kv_cache region=tmdb.request;PG 出错按未命中 / 不写处理,不让 TMDB 请求失败。
|
||||
* 详情命中且属于追踪中的作品时续期(CACHE-2),续期失败只记 debug。
|
||||
*/
|
||||
function tmdbSharedCache(ctx: ServerContext, policy: TmdbCachePolicy): TmdbSharedCache {
|
||||
const cache = sharedCache(ctx);
|
||||
const warn = (err: unknown) =>
|
||||
ctx.logger.warn({ err: { message: (err as Error).message } }, "TMDB 缓存读写失败,按未命中处理");
|
||||
return {
|
||||
get: (key) =>
|
||||
cache.get(CACHE_REGIONS.tmdbRequest, key).catch((e) => {
|
||||
get: async (key) => {
|
||||
const v = await cache.get(CACHE_REGIONS.tmdbRequest, key).catch((e) => {
|
||||
warn(e);
|
||||
return null;
|
||||
}),
|
||||
});
|
||||
if (v !== null && tmdbDetailRef(key)) {
|
||||
await renewTrackedTmdbDetail(ctx, key, policy.ttl).catch((e) =>
|
||||
ctx.logger.debug({ err: { message: (e as Error).message } }, "TMDB 详情缓存续期失败"),
|
||||
);
|
||||
}
|
||||
return v;
|
||||
},
|
||||
set: (key, value, ttlMs) =>
|
||||
cache.set(CACHE_REGIONS.tmdbRequest, key, value, { ttl: ttlMs / 1000, maxsize: policy.maxsize }).catch(warn),
|
||||
};
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import type { SiteDetailPage } from "@umt/adapters";
|
||||
import type { SiteReleasePage } from "@umt/contracts";
|
||||
import { sql } from "@umt/db";
|
||||
import { sharedCache } from "../../../shared-cache.ts";
|
||||
import { sharedCache, storageSettings } from "../../../shared-cache.ts";
|
||||
import type { SourcesRuntime } from "../runtime.ts";
|
||||
import type { Exec } from "./common.ts";
|
||||
|
||||
// 站点详情页扩展内容(简介 / MediaInfo / 截图 / 外部 ID / 文件 / 标签 / 上传者):
|
||||
// - 只在按需抓详情时得到(工作台打开详情、release.inspect 复核),放 PG kv_cache region site.detail,
|
||||
// key = source_release.id,TTL = 站点详情缓存期(至少 24 小时);不进 source_release 列,过期即丢。
|
||||
// key = source_release.id,TTL = storage.cacheTtl.siteDetailDays(CACHE-2,缺省 7 天;不短于站点详情缓存期);
|
||||
// 不进 source_release 列,过期即丢。促销 / H&R 不靠它:下载前复核走 refreshReleaseDetail(按站点详情缓存期判断是否重抓)。
|
||||
// - 外部 ID(IMDb / 豆瓣 / TMDB)另写 source_release.raw._detail(列表快照 upsert 不覆盖它),供身份解析用。
|
||||
// - 截图对外只给代理路径 /api/v1/img/site-detail/{releaseId}/{index},原始地址留在服务端。
|
||||
|
||||
export const DETAIL_PAGE_REGION = "site.detail";
|
||||
const MIN_TTL_SEC = 86_400;
|
||||
const MAX_ENTRIES = 5_000;
|
||||
|
||||
interface StoredPage extends SiteDetailPage {
|
||||
@@ -33,8 +33,9 @@ export async function saveDetailPage(
|
||||
cacheSec: number,
|
||||
): Promise<void> {
|
||||
const stored: StoredPage = { ...page, fetchedAt: rt.now().toISOString() };
|
||||
const { siteDetailDays } = (await storageSettings(rt.ctx)).cacheTtl;
|
||||
await sharedCache(rt.ctx)
|
||||
.set(DETAIL_PAGE_REGION, srId, stored, { ttl: Math.max(cacheSec, MIN_TTL_SEC), maxsize: MAX_ENTRIES })
|
||||
.set(DETAIL_PAGE_REGION, srId, stored, { ttl: Math.max(cacheSec, siteDetailDays * 86_400), maxsize: MAX_ENTRIES })
|
||||
.catch((e) => rt.ctx.logger.warn({ err: { message: (e as Error).message } }, "详情页缓存写入失败"));
|
||||
const ids: DetailIds = {
|
||||
...(page.imdbId ? { imdbId: page.imdbId } : {}),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { DecisionOption, PoolCandidate } from "@umt/contracts";
|
||||
import { sql } from "@umt/db";
|
||||
import { storageSettings } from "../../../shared-cache.ts";
|
||||
import type { ServerContext } from "../../types.ts";
|
||||
import { jsonb } from "../rows.ts";
|
||||
import type { SourcesRuntime } from "../runtime.ts";
|
||||
@@ -10,7 +11,7 @@ import { closeDecisions, type Exec, openDecision } from "./common.ts";
|
||||
// - 同一来源 + 同一归一化标题合并为一条事项(dedupeKey identity-group:<siteId>:<normalized>),成员 = 池里
|
||||
// decision_id 指向它且仍 pending 的条目;事项的 subject 与候选选项取自代表资源(最近一次进来的那条);
|
||||
// - 成员被识别 / 指定 / 忽略时从组里摘除,组空了才关闭事项;
|
||||
// - unmatched 池条目超过 identity.poolArchiveDays 没有新证据就归档。
|
||||
// - unmatched 池条目超过 storage.retentionDays.identityPoolArchive 天没有新证据就归档。
|
||||
|
||||
const OPEN = ["open", "investigating", "awaiting_user"] as const;
|
||||
/** 事项证据里最多列多少条成员资源(完整列表在待识别池按标题检索) */
|
||||
@@ -208,14 +209,15 @@ const ARCHIVE_EVERY_MS = 3600_000;
|
||||
let lastArchiveAt = 0;
|
||||
|
||||
/**
|
||||
* 过期归档:unmatched 池条目超过 identity.poolArchiveDays 没有新证据 → archived(仍可按状态筛出、可手动指定;
|
||||
* 过期归档:unmatched 池条目超过 storage.retentionDays.identityPoolArchive 天没有新证据 → archived(仍可按状态筛出、可手动指定;
|
||||
* 同一资源再被观察到或新增相近别名时回到 pending)。每进程每小时最多执行一次。
|
||||
*/
|
||||
export async function archiveStalePool(rt: SourcesRuntime, opts: { force?: boolean } = {}): Promise<number> {
|
||||
const now = rt.now();
|
||||
if (!opts.force && now.getTime() - lastArchiveAt < ARCHIVE_EVERY_MS) return 0;
|
||||
lastArchiveAt = now.getTime();
|
||||
const { poolArchiveDays } = await rt.ctx.settings.get("identity");
|
||||
// 与存储保留任务 storage.retention.identity_pool 同一条件(那边在 system 模块里按表直接执行)
|
||||
const poolArchiveDays = (await storageSettings(rt.ctx)).retentionDays.identityPoolArchive;
|
||||
const cutoff = new Date(now.getTime() - poolArchiveDays * 86_400_000);
|
||||
const r = await rt.ctx.db
|
||||
.updateTable("identity_candidate")
|
||||
|
||||
@@ -110,7 +110,8 @@ export async function saveRecognizeCache(
|
||||
poster_path: info.posterPath ?? null,
|
||||
backdrop_path: info.backdropPath ?? null,
|
||||
};
|
||||
await cache.set(CACHE_REGIONS.tmdbRecognize, key, entry, { ttl: policy.ttl, maxsize: policy.maxsize });
|
||||
// 识别结果 = 名称 → TMDB 条目,按搜索类 TTL(storage.cacheTtl.tmdbSearchDays)
|
||||
await cache.set(CACHE_REGIONS.tmdbRecognize, key, entry, { ttl: policy.searchTtl, maxsize: policy.maxsize });
|
||||
return;
|
||||
}
|
||||
// 负识别缓存用独立短 TTL(EMPTY_RESULT_CACHE_TTL):故障期间的空结果不被固化整个有效期
|
||||
|
||||
@@ -28,6 +28,8 @@ export interface HealthDeps {
|
||||
checkRoot?: typeof checkRoot;
|
||||
/** 单个路径 IO 检查超时(ms),缺省 3000 */
|
||||
ioTimeoutMs?: number;
|
||||
/** 存储段(CACHE-2);不给时该段为 unknown */
|
||||
storage?: () => Promise<{ level: HealthLevel; alerts: string[] }>;
|
||||
}
|
||||
|
||||
const LEVEL_ORDER: HealthLevel[] = ["ok", "warn", "critical"];
|
||||
@@ -384,7 +386,7 @@ export async function loadSchedules(db: Kysely<DB>, now: Date): Promise<SystemHe
|
||||
|
||||
export async function buildHealth(d: HealthDeps): Promise<SystemHealth> {
|
||||
const now = d.now?.() ?? new Date();
|
||||
const [{ processes, retiredProcesses }, db, paths, engine, schedules] = await Promise.all([
|
||||
const [{ processes, retiredProcesses }, db, paths, engine, schedules, storage] = await Promise.all([
|
||||
loadProcesses(d.db, now).catch(() => ({
|
||||
processes: [] as SystemProcess[],
|
||||
retiredProcesses: [] as SystemProcess[],
|
||||
@@ -408,6 +410,10 @@ export async function buildHealth(d: HealthDeps): Promise<SystemHealth> {
|
||||
failing: [],
|
||||
failedRuns24h: 0,
|
||||
})),
|
||||
(d.storage?.() ?? Promise.resolve({ level: "unknown" as HealthLevel, alerts: [] })).catch(() => ({
|
||||
level: "unknown" as HealthLevel,
|
||||
alerts: [] as string[],
|
||||
})),
|
||||
]);
|
||||
// SSE:存活的 api/all 进程上报的连接数合计;本进程若还没写过心跳,用实时值
|
||||
const selfCounted = processes.some((p) => !p.stale && p.instanceId === d.self.instanceId && p.sseConnections != null);
|
||||
@@ -423,6 +429,7 @@ export async function buildHealth(d: HealthDeps): Promise<SystemHealth> {
|
||||
mountLevel,
|
||||
engine.level,
|
||||
schedules.level,
|
||||
storage.level,
|
||||
]),
|
||||
checkedAt: now.toISOString(),
|
||||
api: {
|
||||
@@ -439,6 +446,7 @@ export async function buildHealth(d: HealthDeps): Promise<SystemHealth> {
|
||||
mounts: paths.mounts,
|
||||
engine,
|
||||
schedules,
|
||||
storage,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -130,11 +130,13 @@ export async function markHeartbeatStopped(db: Kysely<DB>, instanceId: string, a
|
||||
/** 裁剪:超过 24 小时没心跳的进程行、超过 7 天的采样;返回删除行数 */
|
||||
export async function pruneHeartbeats(
|
||||
db: Kysely<DB>,
|
||||
o: { now?: Date; rowRetentionMs?: number; sampleDays?: number } = {},
|
||||
o: { now?: Date; rowRetentionMs?: number; sampleDays?: number; sampleHours?: number } = {},
|
||||
): Promise<{ rows: number; samples: number }> {
|
||||
const now = o.now ?? new Date();
|
||||
const rowCutoff = new Date(now.getTime() - (o.rowRetentionMs ?? HEARTBEAT_ROW_RETENTION_MS));
|
||||
const sampleCutoff = new Date(now.getTime() - (o.sampleDays ?? SAMPLE_RETENTION_DAYS) * 24 * 3600_000);
|
||||
// CACHE-2:采样保留取 storage.retentionDays.heartbeatHours(调用方传 sampleHours);sampleDays 为旧参数
|
||||
const sampleHours = o.sampleHours ?? (o.sampleDays ?? SAMPLE_RETENTION_DAYS) * 24;
|
||||
const sampleCutoff = new Date(now.getTime() - sampleHours * 3600_000);
|
||||
const rows = await db.deleteFrom("process_heartbeat").where("last_beat_at", "<", rowCutoff).executeTakeFirst();
|
||||
const samples = await db.deleteFrom("process_heartbeat_sample").where("at", "<", sampleCutoff).executeTakeFirst();
|
||||
return { rows: Number(rows.numDeletedRows), samples: Number(samples.numDeletedRows) };
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
import { createRoute, type RouteConfig } from "@hono/zod-openapi";
|
||||
import { Api, apiResponse, DEFAULT_SETTINGS, type EndpointDef } from "@umt/contracts";
|
||||
import {
|
||||
type Actor,
|
||||
Api,
|
||||
apiResponse,
|
||||
DEFAULT_SETTINGS,
|
||||
type EndpointDef,
|
||||
type StorageCleanupTarget,
|
||||
} from "@umt/contracts";
|
||||
import { sql } from "@umt/db";
|
||||
import type { z } from "zod";
|
||||
import { dataPaths } from "../../config.ts";
|
||||
import { requireScope } from "../../http/auth.ts";
|
||||
import { ApiException } from "../../http/errors.ts";
|
||||
import { sseConnectionCount } from "../../http/sse.ts";
|
||||
import { storageSettings } from "../../shared-cache.ts";
|
||||
import { registerScheduledJob } from "../schedules/registry.ts";
|
||||
import type { ModuleApp, ServerContext, ServerModule } from "../types.ts";
|
||||
import { buildHealth, type HealthDeps, loadHistory } from "./health.ts";
|
||||
@@ -15,6 +25,8 @@ import {
|
||||
safeHostname,
|
||||
startHeartbeatWriter,
|
||||
} from "./heartbeat.ts";
|
||||
import { type BatchOptions, CLEANUP_JOB_IDS, RETENTION_JOBS } from "./retention.ts";
|
||||
import { buildStorage, type StoragePaths, storageHealth } from "./storage.ts";
|
||||
|
||||
export { HEARTBEAT_STALE_MS, HEARTBEAT_WRITE_MS, startHeartbeatWriter } from "./heartbeat.ts";
|
||||
|
||||
@@ -23,7 +35,13 @@ export { HEARTBEAT_STALE_MS, HEARTBEAT_WRITE_MS, startHeartbeatWriter } from "./
|
||||
* - GET /system/health:进程心跳 / 事件循环延迟 / SSE 连接 / DB / 磁盘 / 挂载根 / 引擎 / 定时任务,仅 admin;
|
||||
* - GET /system/health/history?hours=:心跳采样时间序列。
|
||||
* 心跳写入:worker 进程在 worker/index.ts 里起写入器;纯 api 进程(role=api)在注册路由时起(role=all 由 worker 写 kind=all)。
|
||||
* 清理:定时任务 system.heartbeat_cleanup(维护组,1 小时)删 24 小时没心跳的进程行与 7 天前的采样。
|
||||
* 清理:定时任务 system.heartbeat_cleanup(维护组,1 小时)删 24 小时没心跳的进程行与超过
|
||||
* storage.retentionDays.heartbeatHours 的采样。
|
||||
*
|
||||
* CACHE-2 存储(docs/runbook/storage.md):
|
||||
* - GET /system/storage:表体积 top 15、库体积、kv_cache 各 region、图片 / 备份 / 会话与证据对照预算、DATA_DIR 分目录、清理任务;
|
||||
* - POST /system/storage/cleanup/{target}:把对应的维护任务排入立即执行(同 schedules run_now 语义);
|
||||
* - 保留清理任务 storage.retention.* 与每周 storage.analyze(维护组,各自开关与周期在定时任务看板改)。
|
||||
*/
|
||||
export interface SystemModuleOptions {
|
||||
/** 引擎 schema(测试覆盖);缺省 openworkflow */
|
||||
@@ -34,6 +52,12 @@ export interface SystemModuleOptions {
|
||||
apiHeartbeat?: boolean;
|
||||
/** 定时任务 system.heartbeat_cleanup 的默认周期,默认 1 小时;<=0 不登记 */
|
||||
cleanupIntervalMs?: number;
|
||||
/** 存储面板路径(测试覆盖);缺省按 DATA_DIR / AGENT_SESSION_DIR */
|
||||
storagePaths?: StoragePaths;
|
||||
/** false 时不登记保留清理任务(测试);缺省 true */
|
||||
retentionJobs?: boolean;
|
||||
/** 保留清理的分批参数(测试) */
|
||||
retentionBatch?: BatchOptions;
|
||||
}
|
||||
|
||||
type Infer<T> = T extends z.ZodType ? z.infer<T> : Record<string, never>;
|
||||
@@ -41,10 +65,11 @@ type Infer<T> = T extends z.ZodType ? z.infer<T> : Record<string, never>;
|
||||
function route<D extends EndpointDef>(
|
||||
app: ModuleApp,
|
||||
def: D,
|
||||
handler: (i: { query: Infer<D["query"]> }) => Promise<unknown>,
|
||||
handler: (i: { query: Infer<D["query"]>; params: Infer<D["params"]>; actor: Actor | undefined }) => Promise<unknown>,
|
||||
) {
|
||||
const request: RouteConfig["request"] = {};
|
||||
if (def.query) request.query = def.query as never;
|
||||
if (def.params) request.params = def.params as never;
|
||||
const r = createRoute({
|
||||
method: def.method,
|
||||
path: def.path,
|
||||
@@ -58,7 +83,13 @@ function route<D extends EndpointDef>(
|
||||
});
|
||||
// biome-ignore lint/suspicious/noExplicitAny: 路由泛型在封装里擦除,入参已由 zod-openapi 校验
|
||||
app.openapi(r, (async (c: any) => {
|
||||
const data = def.response.parse(await handler({ query: def.query ? c.req.valid("query") : {} } as never));
|
||||
const data = def.response.parse(
|
||||
await handler({
|
||||
query: def.query ? c.req.valid("query") : {},
|
||||
params: def.params ? c.req.valid("param") : {},
|
||||
actor: c.get("actor"),
|
||||
} as never),
|
||||
);
|
||||
return c.json({ ok: true, data }, 200);
|
||||
// biome-ignore lint/suspicious/noExplicitAny: 同上
|
||||
}) as any);
|
||||
@@ -84,6 +115,11 @@ export function createSystemModule(opts: SystemModuleOptions = {}): ServerModule
|
||||
}
|
||||
const selfKind = ctx.role === "all" ? "all" : ctx.role === "api" ? "api" : null;
|
||||
const selfId = selfKind ? instanceIdOf(selfKind, safeHostname(), Deno.pid) : undefined;
|
||||
const paths = (): StoragePaths => {
|
||||
if (opts.storagePaths) return opts.storagePaths;
|
||||
const p = dataPaths(ctx.config);
|
||||
return { dataDir: opts.healthDeps?.dataDir ?? p.dataDir, sessionDir: p.sessionDir };
|
||||
};
|
||||
|
||||
route(app, Api.system.healthDetail, async () => {
|
||||
const cache = await ctx.settings.get("cache").catch(() => DEFAULT_SETTINGS.cache);
|
||||
@@ -98,25 +134,89 @@ export function createSystemModule(opts: SystemModuleOptions = {}): ServerModule
|
||||
...(selfId ? { instanceId: selfId } : {}),
|
||||
},
|
||||
thresholds: { warnPercent: cache.resumePercent, criticalPercent: cache.hardThresholdPercent },
|
||||
storage: async () =>
|
||||
await storageHealth({ db: ctx.db, paths: paths(), settings: await storageSettings(ctx) }),
|
||||
...(opts.healthDeps ?? {}),
|
||||
});
|
||||
});
|
||||
route(app, Api.system.healthHistory, async ({ query }) => await loadHistory(ctx.db, query.hours));
|
||||
route(app, Api.system.storage, async () =>
|
||||
buildStorage({
|
||||
db: ctx.db,
|
||||
paths: paths(),
|
||||
settings: await storageSettings(ctx),
|
||||
engineSchema: opts.engineSchema ?? "openworkflow",
|
||||
}),
|
||||
);
|
||||
route(app, Api.system.storageCleanup, async ({ params, actor }) => {
|
||||
const target = params.target as StorageCleanupTarget;
|
||||
const jobId = CLEANUP_JOB_IDS[target];
|
||||
const job = await ctx.db
|
||||
.selectFrom("scheduled_job")
|
||||
.select(["id", "available"])
|
||||
.where("id", "=", jobId)
|
||||
.executeTakeFirst();
|
||||
if (!job) throw new ApiException("not_found", `清理任务 ${jobId} 还没登记(调度器未同步)`);
|
||||
if (!job.available) throw new ApiException("conflict", "任务未配置,不能执行");
|
||||
// 同 schedules 的立即执行:只置 run_requested_at,已在排队或运行中则合并
|
||||
const r = await sql`update scheduled_job set run_requested_at = now()
|
||||
where id = ${jobId} and run_requested_at is null and running_since is null`.execute(ctx.db);
|
||||
const queued = Number(r.numAffectedRows ?? 0) > 0;
|
||||
if (queued) {
|
||||
await ctx.db
|
||||
.insertInto("audit_event")
|
||||
.values({
|
||||
actor_type: actor?.type === "api_key" ? "api_key" : actor ? "user" : "system",
|
||||
actor_id: actor?.id ?? null,
|
||||
action: "storage.cleanup",
|
||||
target_type: "scheduled_job",
|
||||
target_id: jobId,
|
||||
payload: JSON.stringify({ target }),
|
||||
})
|
||||
.execute()
|
||||
.catch((err) => ctx.logger.warn({ err }, "审计写入失败"));
|
||||
}
|
||||
return { target, jobId, queued };
|
||||
});
|
||||
},
|
||||
registerTasks() {},
|
||||
start(ctx) {
|
||||
if (opts.retentionJobs !== false) {
|
||||
for (const j of RETENTION_JOBS) {
|
||||
registerScheduledJob(ctx, {
|
||||
key: j.jobId,
|
||||
defaults: {
|
||||
name: j.name,
|
||||
description: j.description,
|
||||
group: "maintain",
|
||||
schedule: { type: "interval", everyMs: j.everyMs },
|
||||
firstRun: "after_interval",
|
||||
},
|
||||
run: async ({ now }) => ({
|
||||
summary: await j.run({
|
||||
db: ctx.db,
|
||||
engineSchema: opts.engineSchema ?? "openworkflow",
|
||||
settings: await storageSettings(ctx),
|
||||
now,
|
||||
...(opts.retentionBatch ? { batch: opts.retentionBatch } : {}),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
const everyMs = opts.cleanupIntervalMs ?? 3600_000;
|
||||
if (everyMs <= 0) return;
|
||||
registerScheduledJob(ctx, {
|
||||
key: "system.heartbeat_cleanup",
|
||||
defaults: {
|
||||
name: "进程心跳清理",
|
||||
description: "删除 24 小时没有心跳的进程行和 7 天前的心跳采样",
|
||||
description: "删除 24 小时没有心跳的进程行和超过保留期(存储设置,缺省 24 小时)的心跳采样",
|
||||
group: "maintain",
|
||||
schedule: { type: "interval", everyMs },
|
||||
},
|
||||
run: async ({ now }) => {
|
||||
const r = await pruneHeartbeats(ctx.db, { now });
|
||||
const sampleHours = (await storageSettings(ctx)).retentionDays.heartbeatHours;
|
||||
const r = await pruneHeartbeats(ctx.db, { now, sampleHours });
|
||||
return { summary: `删除 ${r.rows} 个进程行、${r.samples} 条采样` };
|
||||
},
|
||||
});
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
import type { StorageCleanupTarget, StorageSettings } from "@umt/contracts";
|
||||
import { type DB, type Kysely, type RawBuilder, sql } from "@umt/db";
|
||||
|
||||
// CACHE-2 保留策略清理(docs/runbook/storage.md):按表按时间列分批删除流水数据。
|
||||
// - 每批:单独事务,`set local statement_timeout / lock_timeout` 限时,按 ctid 取一批再删,不做整表长事务;
|
||||
// - 每次运行有总时限,到点就停,剩下的留给下次;批与批之间让出一小段时间给业务写入;
|
||||
// - 只删已结束的数据(任务 / Agent 会话 / 能力调用看状态),进行中的不动。
|
||||
// 待识别池不删:unmatched 条目到期归档(与 sources-identity archiveStalePool 同一条件)。
|
||||
|
||||
export interface BatchOptions {
|
||||
/** 每批行数,缺省 5000 */
|
||||
batchSize?: number;
|
||||
/** 每批语句超时(ms),缺省 15000 */
|
||||
statementTimeoutMs?: number;
|
||||
/** 等锁超时(ms),缺省 3000 */
|
||||
lockTimeoutMs?: number;
|
||||
/** 本次运行总时限(ms),缺省 5 分钟 */
|
||||
budgetMs?: number;
|
||||
/** 批间停顿(ms),缺省 50 */
|
||||
pauseMs?: number;
|
||||
/** 测试注入 */
|
||||
now?: () => number;
|
||||
}
|
||||
|
||||
export interface BatchResult {
|
||||
deleted: number;
|
||||
batches: number;
|
||||
/** 到总时限还没删完 */
|
||||
truncated: boolean;
|
||||
}
|
||||
|
||||
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
||||
|
||||
/**
|
||||
* 分批删除 `table` 中满足 `where` 的行:每批 `delete … where ctid = any(array(select ctid … limit n))`,
|
||||
* 单独事务 + 语句 / 锁超时。某批超时(57014)或等锁超时(55P03)时停止本次运行,不抛错。
|
||||
*/
|
||||
export async function deleteInBatches(
|
||||
db: Kysely<DB>,
|
||||
table: RawBuilder<unknown>,
|
||||
where: RawBuilder<unknown>,
|
||||
o: BatchOptions = {},
|
||||
): Promise<BatchResult> {
|
||||
const size = o.batchSize ?? 5000;
|
||||
const now = o.now ?? Date.now;
|
||||
const deadline = now() + (o.budgetMs ?? 5 * 60_000);
|
||||
let deleted = 0;
|
||||
let batches = 0;
|
||||
for (;;) {
|
||||
let n: number;
|
||||
try {
|
||||
n = await db.transaction().execute(async (trx) => {
|
||||
await sql`select set_config('statement_timeout', ${String(o.statementTimeoutMs ?? 15_000)}, true),
|
||||
set_config('lock_timeout', ${String(o.lockTimeoutMs ?? 3000)}, true)`.execute(trx);
|
||||
const r = await sql`delete from ${table} where ctid = any(array(
|
||||
select ctid from ${table} where ${where} limit ${size}))`.execute(trx);
|
||||
return Number(r.numAffectedRows ?? 0);
|
||||
});
|
||||
} catch (e) {
|
||||
const code = (e as { code?: string }).code;
|
||||
if (code === "57014" || code === "55P03") return { deleted, batches, truncated: true };
|
||||
throw e;
|
||||
}
|
||||
batches++;
|
||||
deleted += n;
|
||||
if (n < size) return { deleted, batches, truncated: false };
|
||||
if (now() >= deadline) return { deleted, batches, truncated: true };
|
||||
if ((o.pauseMs ?? 50) > 0) await sleep(o.pauseMs ?? 50);
|
||||
}
|
||||
}
|
||||
|
||||
export interface RetentionJob {
|
||||
target: StorageCleanupTarget;
|
||||
/** scheduled_job key */
|
||||
jobId: string;
|
||||
name: string;
|
||||
description: string;
|
||||
everyMs: number;
|
||||
/** 保留期说明(存储面板) */
|
||||
retention(s: StorageSettings): string | null;
|
||||
run(c: RetentionRunContext): Promise<string>;
|
||||
}
|
||||
|
||||
export interface RetentionRunContext {
|
||||
db: Kysely<DB>;
|
||||
engineSchema: string;
|
||||
settings: StorageSettings;
|
||||
now: Date;
|
||||
batch?: BatchOptions;
|
||||
}
|
||||
|
||||
const DAY_MS = 86_400_000;
|
||||
const cutoffOf = (now: Date, days: number) => new Date(now.getTime() - days * DAY_MS);
|
||||
const t = (name: string) => sql.raw(`"${name}"`);
|
||||
const summaryOf = (label: string, r: BatchResult) =>
|
||||
`${label}删除 ${r.deleted} 行(${r.batches} 批)${r.truncated ? ",未删完,下次继续" : ""}`;
|
||||
|
||||
const HOUR = 3600_000;
|
||||
|
||||
/**
|
||||
* 保留清理任务(维护组;heartbeat 目标由 system.heartbeat_cleanup 承担,images / kv_cache 由 cache 模块承担)。
|
||||
* 顺序即存储面板的展示顺序。
|
||||
*/
|
||||
export const RETENTION_JOBS: RetentionJob[] = [
|
||||
{
|
||||
target: "observation",
|
||||
jobId: "storage.retention.observation",
|
||||
name: "观察记录清理",
|
||||
description: "删除超过保留期的 Feed 观察记录(observation,按 created_at)",
|
||||
everyMs: 6 * HOUR,
|
||||
retention: (s) => `${s.retentionDays.observation} 天`,
|
||||
async run(c) {
|
||||
const cutoff = cutoffOf(c.now, c.settings.retentionDays.observation);
|
||||
const r = await deleteInBatches(c.db, t("observation"), sql`created_at < ${cutoff}`, c.batch);
|
||||
return summaryOf("观察记录", r);
|
||||
},
|
||||
},
|
||||
{
|
||||
target: "sse_event",
|
||||
jobId: "storage.retention.sse_event",
|
||||
name: "SSE 事件清理",
|
||||
description: "删除超过保留期的 SSE 补读事件(sse_event,按 at;发布方另按条数裁剪)",
|
||||
everyMs: HOUR,
|
||||
retention: (s) => `${s.retentionDays.sseEvent} 天`,
|
||||
async run(c) {
|
||||
const cutoff = cutoffOf(c.now, c.settings.retentionDays.sseEvent);
|
||||
const r = await deleteInBatches(c.db, t("sse_event"), sql`at < ${cutoff}`, c.batch);
|
||||
return summaryOf("SSE 事件", r);
|
||||
},
|
||||
},
|
||||
{
|
||||
target: "task_run",
|
||||
jobId: "storage.retention.task_run",
|
||||
name: "任务运行清理",
|
||||
description: "删除超过保留期、已结束的业务任务(task)与引擎 run(含步骤、运行元数据)",
|
||||
everyMs: 6 * HOUR,
|
||||
retention: (s) => `${s.retentionDays.taskRun} 天`,
|
||||
async run(c) {
|
||||
const cutoff = cutoffOf(c.now, c.settings.retentionDays.taskRun);
|
||||
const e = (n: string) => sql.raw(`"${c.engineSchema}"."${n}"`);
|
||||
const task = await deleteInBatches(
|
||||
c.db,
|
||||
t("task"),
|
||||
sql`created_at < ${cutoff} and status in ('succeeded', 'failed', 'cancelled', 'dead')`,
|
||||
c.batch,
|
||||
);
|
||||
// 引擎 run:step_attempts 随 run 级联删除
|
||||
const runs = await deleteInBatches(
|
||||
c.db,
|
||||
e("workflow_runs"),
|
||||
sql`created_at < ${cutoff} and status in ('completed', 'succeeded', 'failed', 'canceled')`,
|
||||
c.batch,
|
||||
);
|
||||
// 运行元数据与 run 同时写入:早于保留期的 run 已结束(上一步删掉了),元数据按时间直接删
|
||||
const meta = await deleteInBatches(c.db, t("tasks_run_meta"), sql`created_at < ${cutoff}`, c.batch);
|
||||
const facts = await deleteInBatches(c.db, t("tasks_fact"), sql`created_at < ${cutoff}`, c.batch);
|
||||
const ledger = await deleteInBatches(
|
||||
c.db,
|
||||
t("tasks_op_ledger"),
|
||||
sql`status = 'succeeded' and started_at < ${cutoff}`,
|
||||
c.batch,
|
||||
);
|
||||
const truncated = [task, runs, meta, facts, ledger].some((x) => x.truncated);
|
||||
return `业务任务 ${task.deleted}、引擎 run ${runs.deleted}、运行元数据 ${meta.deleted + facts.deleted + ledger.deleted} 行已删除${truncated ? ",未删完,下次继续" : ""}`;
|
||||
},
|
||||
},
|
||||
{
|
||||
target: "agent_event",
|
||||
jobId: "storage.retention.agent_event",
|
||||
name: "Agent 事件清理",
|
||||
description: "删除超过保留期、会话已结束的 Agent 事件流与消息(agent_run_event / agent_run_message;会话本身保留)",
|
||||
everyMs: 6 * HOUR,
|
||||
retention: (s) => `${s.retentionDays.agentEvent} 天`,
|
||||
async run(c) {
|
||||
const cutoff = cutoffOf(c.now, c.settings.retentionDays.agentEvent);
|
||||
const ended = sql`exists (select 1 from agent_run r where r.id = run_id
|
||||
and r.status in ('succeeded', 'failed', 'cancelled', 'budget_exceeded'))`;
|
||||
const ev = await deleteInBatches(c.db, t("agent_run_event"), sql`at < ${cutoff} and ${ended}`, c.batch);
|
||||
const msg = await deleteInBatches(
|
||||
c.db,
|
||||
t("agent_run_message"),
|
||||
sql`created_at < ${cutoff} and status = 'consumed' and ${ended}`,
|
||||
c.batch,
|
||||
);
|
||||
return `Agent 事件 ${ev.deleted}、消息 ${msg.deleted} 行已删除${ev.truncated || msg.truncated ? ",未删完,下次继续" : ""}`;
|
||||
},
|
||||
},
|
||||
{
|
||||
target: "audit",
|
||||
jobId: "storage.retention.audit",
|
||||
name: "审计清理",
|
||||
description: "删除超过保留期的审计事件(audit_event,能力调用审计按能力统计的保留期另算)",
|
||||
everyMs: 24 * HOUR,
|
||||
retention: (s) => `${s.retentionDays.audit} 天`,
|
||||
async run(c) {
|
||||
const cutoff = cutoffOf(c.now, c.settings.retentionDays.audit);
|
||||
const r = await deleteInBatches(
|
||||
c.db,
|
||||
t("audit_event"),
|
||||
sql`at < ${cutoff} and target_type is distinct from 'capability'`,
|
||||
c.batch,
|
||||
);
|
||||
return summaryOf("审计事件", r);
|
||||
},
|
||||
},
|
||||
{
|
||||
target: "capability_stats",
|
||||
jobId: "storage.retention.capability_stats",
|
||||
name: "能力统计清理",
|
||||
description: "删除超过保留期的能力调用审计(audit_event target_type=capability)与已结束的能力调用记录",
|
||||
everyMs: 24 * HOUR,
|
||||
retention: (s) => `${s.retentionDays.capabilityStats} 天`,
|
||||
async run(c) {
|
||||
const cutoff = cutoffOf(c.now, c.settings.retentionDays.capabilityStats);
|
||||
const a = await deleteInBatches(
|
||||
c.db,
|
||||
t("audit_event"),
|
||||
sql`at < ${cutoff} and target_type = 'capability'`,
|
||||
c.batch,
|
||||
);
|
||||
const inv = await deleteInBatches(
|
||||
c.db,
|
||||
t("capability_invocation"),
|
||||
sql`created_at < ${cutoff} and status <> 'running'`,
|
||||
c.batch,
|
||||
);
|
||||
return `能力审计 ${a.deleted}、调用记录 ${inv.deleted} 行已删除${a.truncated || inv.truncated ? ",未删完,下次继续" : ""}`;
|
||||
},
|
||||
},
|
||||
{
|
||||
target: "identity_pool",
|
||||
jobId: "storage.retention.identity_pool",
|
||||
name: "待识别池归档",
|
||||
description: "待识别池里 unmatched 条目超过保留期没有新证据就归档(不删除,仍可按状态筛出)",
|
||||
everyMs: 6 * HOUR,
|
||||
retention: (s) => `${s.retentionDays.identityPoolArchive} 天`,
|
||||
async run(c) {
|
||||
const cutoff = cutoffOf(c.now, c.settings.retentionDays.identityPoolArchive);
|
||||
let archived = 0;
|
||||
const size = c.batch?.batchSize ?? 5000;
|
||||
for (;;) {
|
||||
const r = await sql`update identity_candidate set status = 'archived', next_review_at = null
|
||||
where id in (select id from identity_candidate where status = 'pending' and reason = 'unmatched'
|
||||
and coalesce(last_attempt_at, created_at) < ${cutoff} limit ${size})`.execute(c.db);
|
||||
const n = Number(r.numAffectedRows ?? 0);
|
||||
archived += n;
|
||||
if (n < size) break;
|
||||
}
|
||||
return `归档 ${archived} 条待识别条目`;
|
||||
},
|
||||
},
|
||||
{
|
||||
target: "analyze",
|
||||
jobId: "storage.analyze",
|
||||
name: "统计信息更新",
|
||||
description: "对全库执行 ANALYZE,刷新查询规划用的统计信息(每周)",
|
||||
everyMs: 7 * 24 * HOUR,
|
||||
retention: () => null,
|
||||
async run(c) {
|
||||
const started = Date.now();
|
||||
await sql`analyze`.execute(c.db);
|
||||
return `ANALYZE 完成,用时 ${Math.round((Date.now() - started) / 100) / 10} 秒`;
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
/** 清理目标 → 定时任务 key(heartbeat / images / kv_cache 由其它任务承担) */
|
||||
export const CLEANUP_JOB_IDS: Record<StorageCleanupTarget, string> = {
|
||||
...(Object.fromEntries(RETENTION_JOBS.map((j) => [j.target, j.jobId])) as Record<string, string>),
|
||||
heartbeat: "system.heartbeat_cleanup",
|
||||
images: "cache.images_lru",
|
||||
kv_cache: "cache.sweep",
|
||||
} as Record<StorageCleanupTarget, string>;
|
||||
@@ -0,0 +1,347 @@
|
||||
import { promises as fs } from "node:fs";
|
||||
import { join, relative } from "node:path";
|
||||
import type { HealthLevel, StorageBudgetUsage, StorageSettings, SystemStorage } from "@umt/contracts";
|
||||
import { type DB, type Kysely, sql } from "@umt/db";
|
||||
import { worst } from "./health.ts";
|
||||
import { CLEANUP_JOB_IDS, RETENTION_JOBS } from "./retention.ts";
|
||||
|
||||
// CACHE-2 存储面板(GET /system/storage)与 health 的存储档位。
|
||||
// 目录遍历可能很慢(图片缓存几十万个文件),结果按目录集合缓存 5 分钟;health 只用缓存值,过期时后台刷新,不等。
|
||||
|
||||
const GB = 1024 ** 3;
|
||||
const DIR_TTL_MS = 5 * 60_000;
|
||||
|
||||
export interface StoragePaths {
|
||||
dataDir: string;
|
||||
/** Agent 会话目录(可能在 DATA_DIR 之外) */
|
||||
sessionDir: string;
|
||||
/** 图片缓存目录;缺省 DATA_DIR/cache/images */
|
||||
imagesDir?: string;
|
||||
}
|
||||
|
||||
export interface DirUsage {
|
||||
name: string;
|
||||
path: string;
|
||||
usedBytes: number;
|
||||
files: number;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
interface DirScan {
|
||||
at: Date;
|
||||
dirs: DirUsage[];
|
||||
images: DirUsage;
|
||||
sessions: DirUsage;
|
||||
evidence: DirUsage;
|
||||
backups: DirUsage & { count: number };
|
||||
}
|
||||
|
||||
async function walk(path: string): Promise<{ bytes: number; files: number; error: string | null }> {
|
||||
let bytes = 0;
|
||||
let files = 0;
|
||||
const go = async (d: string, top: boolean): Promise<string | null> => {
|
||||
let names: string[];
|
||||
try {
|
||||
names = await fs.readdir(d);
|
||||
} catch (e) {
|
||||
const code = (e as { code?: string }).code;
|
||||
// 目录不存在按 0 计
|
||||
return top && code !== "ENOENT" ? (code ?? (e as Error).message) : null;
|
||||
}
|
||||
for (const n of names) {
|
||||
const p = join(d, n);
|
||||
const st = await fs.lstat(p).catch(() => null);
|
||||
if (!st) continue;
|
||||
if (st.isDirectory()) await go(p, false);
|
||||
else {
|
||||
bytes += st.size;
|
||||
files++;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const error = await go(path, true);
|
||||
return { bytes, files, error };
|
||||
}
|
||||
|
||||
const usageOf = async (name: string, path: string): Promise<DirUsage> => {
|
||||
const w = await walk(path);
|
||||
return { name, path, usedBytes: w.bytes, files: w.files, error: w.error };
|
||||
};
|
||||
|
||||
/** DATA_DIR 一级目录(cache / tmp 再细分一层)+ 会话目录 + 备份份数 */
|
||||
export async function scanDirs(p: StoragePaths): Promise<DirScan> {
|
||||
const dirs: DirUsage[] = [];
|
||||
const top = await fs.readdir(p.dataDir, { withFileTypes: true }).catch(() => []);
|
||||
for (const e of top.sort((a, b) => a.name.localeCompare(b.name))) {
|
||||
if (!e.isDirectory()) continue;
|
||||
const path = join(p.dataDir, e.name);
|
||||
if (e.name === "cache" || e.name === "tmp") {
|
||||
const sub = await fs.readdir(path, { withFileTypes: true }).catch(() => []);
|
||||
for (const s of sub.sort((a, b) => a.name.localeCompare(b.name))) {
|
||||
if (s.isDirectory()) dirs.push(await usageOf(`${e.name}/${s.name}`, join(path, s.name)));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
dirs.push(await usageOf(e.name, path));
|
||||
}
|
||||
const find = async (path: string, name: string) => dirs.find((d) => d.path === path) ?? (await usageOf(name, path));
|
||||
const imagesDir = p.imagesDir ?? join(p.dataDir, "cache", "images");
|
||||
const backupsDir = join(p.dataDir, "backups");
|
||||
const backupNames = await fs.readdir(backupsDir, { withFileTypes: true }).catch(() => []);
|
||||
const sessionsInData = !relative(p.dataDir, p.sessionDir).startsWith("..");
|
||||
const sessions = await find(p.sessionDir, "agent-sessions");
|
||||
if (!sessionsInData) dirs.push(sessions);
|
||||
return {
|
||||
at: new Date(),
|
||||
dirs,
|
||||
images: await find(imagesDir, "cache/images"),
|
||||
sessions,
|
||||
evidence: await find(join(p.dataDir, "tmp", "evidence"), "tmp/evidence"),
|
||||
backups: {
|
||||
...(await find(backupsDir, "backups")),
|
||||
count: backupNames.filter((b) => b.isDirectory() && !b.name.startsWith(".")).length,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const scans = new Map<string, { at: number; value: Promise<DirScan>; last: DirScan | null }>();
|
||||
|
||||
/**
|
||||
* 目录用量(缓存 5 分钟)。wait=false 时不等遍历:有旧值返回旧值(过期则后台刷新),没有返回 null。
|
||||
*/
|
||||
export async function dirScan(p: StoragePaths, o: { wait: boolean; force?: boolean }): Promise<DirScan | null> {
|
||||
const key = `${p.dataDir}\u0000${p.sessionDir}\u0000${p.imagesDir ?? ""}`;
|
||||
const cur = scans.get(key);
|
||||
if (!cur || o.force || Date.now() - cur.at > DIR_TTL_MS) {
|
||||
const value = scanDirs(p);
|
||||
const entry = { at: Date.now(), value, last: cur?.last ?? null };
|
||||
scans.set(key, entry);
|
||||
value.then(
|
||||
(v) => {
|
||||
entry.last = v;
|
||||
},
|
||||
() => {
|
||||
if (scans.get(key) === entry) scans.delete(key);
|
||||
},
|
||||
);
|
||||
if (o.wait) return await value;
|
||||
return entry.last;
|
||||
}
|
||||
if (o.wait) return await cur.value;
|
||||
return cur.last;
|
||||
}
|
||||
|
||||
/** 测试用 */
|
||||
export function resetDirScans(): void {
|
||||
scans.clear();
|
||||
}
|
||||
|
||||
/** 用量对照预算:≥ budget critical,≥ warn warn */
|
||||
export function budgetLevel(used: number | null, budget: number | null, warn: number | null): HealthLevel {
|
||||
if (used === null) return "unknown";
|
||||
if (budget !== null && used >= budget) return "critical";
|
||||
if (warn !== null && used >= warn) return "warn";
|
||||
return "ok";
|
||||
}
|
||||
|
||||
const gb = (v: number) => Math.round(v * GB);
|
||||
|
||||
function usage(used: number | null, budgetGb: number, warnGb: number, error: string | null = null): StorageBudgetUsage {
|
||||
const budgetBytes = gb(budgetGb);
|
||||
const warnBytes = gb(warnGb);
|
||||
return {
|
||||
usedBytes: used,
|
||||
budgetBytes,
|
||||
warnBytes,
|
||||
level: error ? "unknown" : budgetLevel(used, budgetBytes, warnBytes),
|
||||
error,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片缓存有容量 LRU(到预算就按最久未访问删),接近预算是常态:超过预算 10% 才告警(说明 LRU 没跟上),不给 critical。
|
||||
*/
|
||||
function imagesUsage(used: number | null, budgetGb: number, error: string | null): StorageBudgetUsage {
|
||||
const budgetBytes = gb(budgetGb);
|
||||
const warnBytes = Math.round(budgetBytes * 1.1);
|
||||
return {
|
||||
usedBytes: used,
|
||||
budgetBytes,
|
||||
warnBytes,
|
||||
level: error ? "unknown" : used === null ? "unknown" : used >= warnBytes ? "warn" : "ok",
|
||||
error,
|
||||
};
|
||||
}
|
||||
|
||||
const fmtGb = (b: number | null) => (b === null ? "?" : `${Math.round((b / GB) * 10) / 10} GB`);
|
||||
|
||||
export interface StorageDeps {
|
||||
db: Kysely<DB>;
|
||||
paths: StoragePaths;
|
||||
settings: StorageSettings;
|
||||
}
|
||||
|
||||
async function dbSize(db: Kysely<DB>): Promise<number> {
|
||||
const r = await sql<{ n: string }>`select pg_database_size(current_database())::text as n`.execute(db);
|
||||
return Number(r.rows[0]?.n ?? 0);
|
||||
}
|
||||
|
||||
/** 汇总档位 + 告警文案(health 与存储面板共用) */
|
||||
function levelsOf(
|
||||
s: StorageSettings,
|
||||
dbBytes: number | null,
|
||||
scan: DirScan | null,
|
||||
): {
|
||||
db: StorageBudgetUsage;
|
||||
images: StorageBudgetUsage;
|
||||
backups: StorageBudgetUsage;
|
||||
sessionsEvidence: StorageBudgetUsage;
|
||||
level: HealthLevel;
|
||||
alerts: string[];
|
||||
} {
|
||||
const b = s.budgets;
|
||||
const db = usage(dbBytes, b.pgDataGb, b.pgDataWarnGb, dbBytes === null ? "db_unavailable" : null);
|
||||
const images = imagesUsage(scan ? scan.images.usedBytes : null, b.imagesGb, scan?.images.error ?? null);
|
||||
const backups = usage(
|
||||
scan ? scan.backups.usedBytes : null,
|
||||
b.backupsGb,
|
||||
b.backupsGb * 0.8,
|
||||
scan?.backups.error ?? null,
|
||||
);
|
||||
const se = scan ? scan.sessions.usedBytes + scan.evidence.usedBytes : null;
|
||||
const sessionsEvidence = usage(
|
||||
se,
|
||||
b.sessionsEvidenceGb,
|
||||
b.sessionsEvidenceGb * 0.8,
|
||||
scan?.sessions.error ?? scan?.evidence.error ?? null,
|
||||
);
|
||||
const alerts: string[] = [];
|
||||
const say = (label: string, u: StorageBudgetUsage) => {
|
||||
if (u.level === "warn" || u.level === "critical") {
|
||||
alerts.push(
|
||||
`${label} ${fmtGb(u.usedBytes)},${u.level === "critical" ? "已超过预算" : "超过告警线"} ${fmtGb(u.level === "critical" ? u.budgetBytes : u.warnBytes)}`,
|
||||
);
|
||||
}
|
||||
};
|
||||
say("数据库", db);
|
||||
say("图片缓存", images);
|
||||
say("备份", backups);
|
||||
say("会话与证据", sessionsEvidence);
|
||||
return {
|
||||
db,
|
||||
images,
|
||||
backups,
|
||||
sessionsEvidence,
|
||||
level: worst([db.level, images.level, backups.level, sessionsEvidence.level]),
|
||||
alerts,
|
||||
};
|
||||
}
|
||||
|
||||
/** health 的存储段:库体积实时取,目录用量只用缓存(不等遍历) */
|
||||
export async function storageHealth(d: StorageDeps): Promise<{ level: HealthLevel; alerts: string[] }> {
|
||||
const [bytes, scan] = await Promise.all([
|
||||
dbSize(d.db).catch(() => null),
|
||||
dirScan(d.paths, { wait: false }).catch(() => null),
|
||||
]);
|
||||
const l = levelsOf(d.settings, bytes, scan);
|
||||
return { level: l.level, alerts: l.alerts };
|
||||
}
|
||||
|
||||
export async function buildStorage(
|
||||
d: StorageDeps & { engineSchema: string; refresh?: boolean },
|
||||
): Promise<SystemStorage> {
|
||||
const [bytes, scan, tables, kv, jobs] = await Promise.all([
|
||||
dbSize(d.db).catch(() => null),
|
||||
dirScan(d.paths, { wait: true, force: d.refresh ?? false }).catch(() => null),
|
||||
sql<{
|
||||
schema: string;
|
||||
name: string;
|
||||
total: string;
|
||||
tbl: string;
|
||||
idx: string;
|
||||
rows: number | null;
|
||||
dead: number | null;
|
||||
vac: Date | null;
|
||||
ana: Date | null;
|
||||
}>`select n.nspname as schema, c.relname as name,
|
||||
pg_total_relation_size(c.oid)::text as total, pg_relation_size(c.oid)::text as tbl,
|
||||
pg_indexes_size(c.oid)::text as idx,
|
||||
case when c.reltuples < 0 then null else c.reltuples::bigint end as rows,
|
||||
s.n_dead_tup as dead, s.last_autovacuum as vac,
|
||||
greatest(s.last_analyze, s.last_autoanalyze) as ana
|
||||
from pg_class c join pg_namespace n on n.oid = c.relnamespace
|
||||
left join pg_stat_all_tables s on s.relid = c.oid
|
||||
where c.relkind in ('r', 'm', 'p') and n.nspname not in ('pg_catalog', 'information_schema')
|
||||
and n.nspname not like 'pg_toast%'
|
||||
order by pg_total_relation_size(c.oid) desc limit 15`.execute(d.db),
|
||||
sql<{ region: string; entries: number; expired: number; bytes: string }>`
|
||||
select region, count(*)::int as entries, count(*) filter (where expires_at <= now())::int as expired,
|
||||
coalesce(sum(pg_column_size(value) + pg_column_size(key)), 0)::text as bytes
|
||||
from kv_cache group by region order by region`
|
||||
.execute(d.db)
|
||||
.catch(() => ({ rows: [] })),
|
||||
d.db
|
||||
.selectFrom("scheduled_job")
|
||||
.select(["id", "enabled", "last_run_at", "last_status", "next_run_at"])
|
||||
.where("id", "in", Object.values(CLEANUP_JOB_IDS))
|
||||
.execute()
|
||||
.catch(() => []),
|
||||
]);
|
||||
const lastSummaries = new Map<string, string | null>();
|
||||
if (jobs.length) {
|
||||
const r = await sql<{ job_id: string; summary: string | null; error: string | null }>`
|
||||
select distinct on (job_id) job_id, summary, error from scheduled_job_run
|
||||
where job_id in (${sql.join(jobs.map((j) => j.id))}) and status <> 'running'
|
||||
order by job_id, started_at desc`
|
||||
.execute(d.db)
|
||||
.catch(() => ({ rows: [] as { job_id: string; summary: string | null; error: string | null }[] }));
|
||||
for (const x of r.rows) lastSummaries.set(x.job_id, x.summary ?? x.error);
|
||||
}
|
||||
const l = levelsOf(d.settings, bytes, scan);
|
||||
const s = d.settings;
|
||||
const retentionOf: Record<string, string | null> = {
|
||||
...Object.fromEntries(RETENTION_JOBS.map((j) => [j.target, j.retention(s)])),
|
||||
heartbeat: `${s.retentionDays.heartbeatHours} 小时`,
|
||||
images: `容量 ${s.budgets.imagesGb} GB`,
|
||||
kv_cache: null,
|
||||
};
|
||||
const iso = (v: Date | string | null) => (v ? new Date(v).toISOString() : null);
|
||||
return {
|
||||
level: l.level,
|
||||
checkedAt: new Date().toISOString(),
|
||||
dirsScannedAt: scan ? scan.at.toISOString() : null,
|
||||
db: {
|
||||
...l.db,
|
||||
tables: tables.rows.map((t) => ({
|
||||
schema: t.schema,
|
||||
name: t.name,
|
||||
totalBytes: Number(t.total),
|
||||
tableBytes: Number(t.tbl),
|
||||
indexBytes: Number(t.idx),
|
||||
rowsEstimate: t.rows === null ? null : Number(t.rows),
|
||||
deadRows: t.dead === null ? null : Number(t.dead),
|
||||
lastAutovacuumAt: iso(t.vac),
|
||||
lastAnalyzeAt: iso(t.ana),
|
||||
})),
|
||||
},
|
||||
kvCache: kv.rows.map((r) => ({ region: r.region, entries: r.entries, expired: r.expired, bytes: Number(r.bytes) })),
|
||||
images: { ...l.images, files: scan ? scan.images.files : null },
|
||||
backups: { ...l.backups, count: scan ? scan.backups.count : null },
|
||||
sessionsEvidence: l.sessionsEvidence,
|
||||
dataDir: scan?.dirs ?? [],
|
||||
cleanup: (Object.entries(CLEANUP_JOB_IDS) as [keyof typeof CLEANUP_JOB_IDS, string][]).map(([target, jobId]) => {
|
||||
const j = jobs.find((x) => x.id === jobId);
|
||||
return {
|
||||
target,
|
||||
jobId,
|
||||
retention: retentionOf[target] ?? null,
|
||||
enabled: j ? j.enabled : null,
|
||||
lastRunAt: iso(j?.last_run_at ?? null),
|
||||
lastStatus: j?.last_status ?? null,
|
||||
lastSummary: lastSummaries.get(jobId) ?? null,
|
||||
nextRunAt: iso(j?.next_run_at ?? null),
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
@@ -17,10 +17,11 @@ import {
|
||||
type WebSearchTestResult,
|
||||
} from "@umt/contracts";
|
||||
import { cachedCall, pgCacheFor, pgTokenBucketFor } from "@umt/db";
|
||||
import { storageSettings } from "../../shared-cache.ts";
|
||||
import type { ServerContext } from "../types.ts";
|
||||
|
||||
/**
|
||||
* WEB-1 网页搜索服务:Brave 客户端之上的 Key 读取、每分钟上限(跨进程令牌桶)、10 分钟 PG 缓存(kv_cache region websearch)、
|
||||
* WEB-1 网页搜索服务:Brave 客户端之上的 Key 读取、每分钟上限(跨进程令牌桶)、PG 缓存(kv_cache region websearch,TTL = storage.cacheTtl.webSearchDays)、
|
||||
* 以及 web.fetch 的白名单——按调用方(Agent 会话 / 其它调用者)记录最近一次 web.search 返回的 URL,只允许读取这些 URL。
|
||||
*/
|
||||
|
||||
@@ -36,8 +37,6 @@ export interface WebSearchModuleOptions {
|
||||
export const WEBSEARCH_CACHE_REGION = "websearch";
|
||||
/** 白名单:key = 调用方,value = 最近一次搜索的 URL */
|
||||
export const WEBSEARCH_ALLOW_REGION = "websearch.allow";
|
||||
/** 搜索结果缓存 10 分钟 */
|
||||
export const SEARCH_CACHE_TTL_SEC = 600;
|
||||
const SEARCH_CACHE_MAX = 1000;
|
||||
/** 白名单保留 6 小时(会话时长上限远小于此) */
|
||||
const ALLOW_TTL_SEC = 6 * 3600;
|
||||
@@ -142,7 +141,7 @@ export class WebSearchService {
|
||||
key,
|
||||
{
|
||||
region: WEBSEARCH_CACHE_REGION,
|
||||
ttl: SEARCH_CACHE_TTL_SEC,
|
||||
ttl: (await storageSettings(this.ctx)).cacheTtl.webSearchDays * 86_400,
|
||||
maxsize: SEARCH_CACHE_MAX,
|
||||
...(o.fresh ? { fresh: true } : {}),
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user