fix: 安全性与代码质量加固(异常保护、外键级联、竞态修复、优雅关机)
This commit is contained in:
@@ -218,7 +218,12 @@ function createTargetsResponse(store: ProbeStore): TargetStatus[] {
|
||||
function mapCheckResult(row: StoredCheckResult): CheckResult {
|
||||
let failure: CheckFailure | null = null;
|
||||
if (row.failure) {
|
||||
failure = JSON.parse(row.failure) as CheckFailure;
|
||||
try {
|
||||
failure = JSON.parse(row.failure) as CheckFailure;
|
||||
} catch {
|
||||
console.warn(`无法解析 failure 数据: target_id=${row.target_id}, timestamp=${row.timestamp}`);
|
||||
failure = null;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -9,7 +9,7 @@ import type {
|
||||
ResolvedCommandTarget,
|
||||
ResolvedHttpTarget,
|
||||
ResolvedTarget,
|
||||
RuntimeConfig,
|
||||
EngineRuntimeConfig,
|
||||
TargetConfig,
|
||||
TargetType,
|
||||
} from "./types";
|
||||
@@ -77,7 +77,7 @@ export async function loadConfig(configPath: string): Promise<ResolvedConfig> {
|
||||
return { host, port, dataDir, configDir, maxConcurrentChecks, targets };
|
||||
}
|
||||
|
||||
function validateRuntime(runtime: RuntimeConfig): number {
|
||||
function validateRuntime(runtime: EngineRuntimeConfig): number {
|
||||
if (runtime.maxConcurrentChecks === undefined) return DEFAULT_MAX_CONCURRENT_CHECKS;
|
||||
|
||||
if (
|
||||
|
||||
@@ -15,7 +15,7 @@ export class ProbeEngine {
|
||||
constructor(store: ProbeStore, targets: ResolvedTarget[], maxConcurrentChecks?: number) {
|
||||
this.store = store;
|
||||
this.targets = targets;
|
||||
this.maxConcurrentChecks = maxConcurrentChecks ?? 10;
|
||||
this.maxConcurrentChecks = maxConcurrentChecks ?? 20;
|
||||
this.refreshCache();
|
||||
}
|
||||
|
||||
@@ -86,6 +86,8 @@ export class ProbeEngine {
|
||||
for (const result of results) {
|
||||
if (result.status === "fulfilled") {
|
||||
this.writeResult(result.value);
|
||||
} else {
|
||||
console.warn("探针执行失败:", result.reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,3 @@ export function parseSize(value: string | number): number {
|
||||
if (unit === "MB") return num * 1024 * 1024;
|
||||
return num * 1024 * 1024 * 1024;
|
||||
}
|
||||
|
||||
export const DEFAULT_MAX_BODY_BYTES = parseSize("100MB");
|
||||
export const DEFAULT_MAX_OUTPUT_BYTES = parseSize("100MB");
|
||||
export const DEFAULT_MAX_CONCURRENT_CHECKS = 20;
|
||||
|
||||
@@ -26,7 +26,7 @@ CREATE TABLE IF NOT EXISTS check_results (
|
||||
duration_ms REAL,
|
||||
status_detail TEXT,
|
||||
failure TEXT,
|
||||
FOREIGN KEY (target_id) REFERENCES targets(id)
|
||||
FOREIGN KEY (target_id) REFERENCES targets(id) ON DELETE CASCADE
|
||||
)
|
||||
`;
|
||||
|
||||
@@ -43,6 +43,7 @@ export class ProbeStore {
|
||||
ensureDir(dirname(dbPath));
|
||||
this.db = new Database(dbPath, { create: true });
|
||||
this.db.run("PRAGMA journal_mode = WAL");
|
||||
this.db.run("PRAGMA foreign_keys = ON");
|
||||
this.db.run(CREATE_TARGETS_TABLE);
|
||||
this.db.run(CREATE_CHECK_RESULTS_TABLE);
|
||||
this.db.run(CREATE_INDEX);
|
||||
|
||||
@@ -2,7 +2,7 @@ export type TargetType = "http" | "command";
|
||||
|
||||
export interface ProbeConfig {
|
||||
server?: ServerConfig;
|
||||
runtime?: RuntimeConfig;
|
||||
runtime?: EngineRuntimeConfig;
|
||||
defaults?: DefaultsConfig;
|
||||
targets: TargetConfig[];
|
||||
}
|
||||
@@ -13,7 +13,7 @@ export interface ServerConfig {
|
||||
dataDir?: string;
|
||||
}
|
||||
|
||||
export interface RuntimeConfig {
|
||||
export interface EngineRuntimeConfig {
|
||||
maxConcurrentChecks?: number;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,14 @@ async function main() {
|
||||
const engine = new ProbeEngine(store, config.targets, config.maxConcurrentChecks);
|
||||
engine.start();
|
||||
|
||||
const shutdown = () => {
|
||||
engine.stop();
|
||||
store.close();
|
||||
process.exit(0);
|
||||
};
|
||||
process.on("SIGINT", shutdown);
|
||||
process.on("SIGTERM", shutdown);
|
||||
|
||||
startServer({
|
||||
config: { host: config.host, port: config.port },
|
||||
mode: "development",
|
||||
|
||||
Reference in New Issue
Block a user