Fix daily-log freezes and revert unrelated queue scope change
- Rewrite daily-log from synchronous fs.writeSync to async buffered writes (500ms flush interval), matching the main logger's pattern. The sync writes blocked the event loop on every log line. - Revert the stop() queue scope change from v1.7.114 which was intended for a different project. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
c51b52b86a
commit
45310f0bf7
@ -3,16 +3,24 @@ import path from "node:path";
|
|||||||
import { addLogListener, removeLogListener } from "./logger";
|
import { addLogListener, removeLogListener } from "./logger";
|
||||||
|
|
||||||
const DAILY_LOG_RETENTION_DAYS = 30;
|
const DAILY_LOG_RETENTION_DAYS = 30;
|
||||||
const CLEANUP_CHECK_INTERVAL_MS = 6 * 60 * 60 * 1000; // every 6 hours
|
const CLEANUP_CHECK_INTERVAL_MS = 6 * 60 * 60 * 1000;
|
||||||
|
const FLUSH_INTERVAL_MS = 500;
|
||||||
|
const BUFFER_LIMIT_CHARS = 500_000;
|
||||||
|
|
||||||
let dailyLogDir = "";
|
let dailyLogDir = "";
|
||||||
let currentDayKey = "";
|
let currentDayKey = "";
|
||||||
let currentLogFd: number | null = null;
|
|
||||||
let currentRenameFd: number | null = null;
|
|
||||||
let logListener: ((line: string) => void) | null = null;
|
let logListener: ((line: string) => void) | null = null;
|
||||||
let cleanupTimer: NodeJS.Timeout | null = null;
|
let cleanupTimer: NodeJS.Timeout | null = null;
|
||||||
let lastCleanupAt = 0;
|
let lastCleanupAt = 0;
|
||||||
|
|
||||||
|
// Async buffered writes — never blocks the event loop
|
||||||
|
let pendingLogLines: string[] = [];
|
||||||
|
let pendingLogChars = 0;
|
||||||
|
let pendingRenameLines: string[] = [];
|
||||||
|
let pendingRenameChars = 0;
|
||||||
|
let flushTimer: NodeJS.Timeout | null = null;
|
||||||
|
let flushInFlight = false;
|
||||||
|
|
||||||
function getDayKey(now = new Date()): string {
|
function getDayKey(now = new Date()): string {
|
||||||
const year = now.getFullYear();
|
const year = now.getFullYear();
|
||||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
const month = String(now.getMonth() + 1).padStart(2, "0");
|
||||||
@ -21,59 +29,84 @@ function getDayKey(now = new Date()): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getMonthDir(dayKey: string): string {
|
function getMonthDir(dayKey: string): string {
|
||||||
return dayKey.slice(0, 7); // "YYYY-MM"
|
return dayKey.slice(0, 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensureDayFile(dayKey: string): number | null {
|
function getDailyLogPath(dayKey: string): string {
|
||||||
if (currentDayKey === dayKey && currentLogFd !== null) {
|
return path.join(dailyLogDir, getMonthDir(dayKey), `${dayKey}.log`);
|
||||||
return currentLogFd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close previous day's fd
|
function getDailyRenameLogPath(dayKey: string): string {
|
||||||
if (currentLogFd !== null) {
|
return path.join(dailyLogDir, getMonthDir(dayKey), `${dayKey}-rename.log`);
|
||||||
try { fs.closeSync(currentLogFd); } catch { /* ignore */ }
|
|
||||||
currentLogFd = null;
|
|
||||||
}
|
|
||||||
if (currentRenameFd !== null) {
|
|
||||||
try { fs.closeSync(currentRenameFd); } catch { /* ignore */ }
|
|
||||||
currentRenameFd = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentDayKey = dayKey;
|
function scheduleFlush(): void {
|
||||||
const monthDir = path.join(dailyLogDir, getMonthDir(dayKey));
|
if (flushTimer || flushInFlight) return;
|
||||||
|
flushTimer = setTimeout(() => {
|
||||||
|
flushTimer = null;
|
||||||
|
void flushAsync();
|
||||||
|
}, FLUSH_INTERVAL_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function flushAsync(): Promise<void> {
|
||||||
|
if (flushInFlight) return;
|
||||||
|
flushInFlight = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.mkdirSync(monthDir, { recursive: true });
|
const dayKey = currentDayKey || getDayKey();
|
||||||
const filePath = path.join(monthDir, `${dayKey}.log`);
|
|
||||||
currentLogFd = fs.openSync(filePath, "a");
|
|
||||||
return currentLogFd;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function ensureRenameFd(dayKey: string): number | null {
|
if (pendingLogLines.length > 0) {
|
||||||
if (currentDayKey === dayKey && currentRenameFd !== null) {
|
const chunk = pendingLogLines.join("");
|
||||||
return currentRenameFd;
|
pendingLogLines = [];
|
||||||
}
|
pendingLogChars = 0;
|
||||||
|
const filePath = getDailyLogPath(dayKey);
|
||||||
// ensureDayFile handles day transitions
|
|
||||||
if (currentDayKey !== dayKey) {
|
|
||||||
ensureDayFile(dayKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currentRenameFd !== null) {
|
|
||||||
return currentRenameFd;
|
|
||||||
}
|
|
||||||
|
|
||||||
const monthDir = path.join(dailyLogDir, getMonthDir(dayKey));
|
|
||||||
try {
|
try {
|
||||||
fs.mkdirSync(monthDir, { recursive: true });
|
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
|
||||||
const filePath = path.join(monthDir, `${dayKey}-rename.log`);
|
await fs.promises.appendFile(filePath, chunk, "utf8");
|
||||||
currentRenameFd = fs.openSync(filePath, "a");
|
} catch { /* ignore */ }
|
||||||
return currentRenameFd;
|
}
|
||||||
} catch {
|
|
||||||
return null;
|
if (pendingRenameLines.length > 0) {
|
||||||
|
const chunk = pendingRenameLines.join("");
|
||||||
|
pendingRenameLines = [];
|
||||||
|
pendingRenameChars = 0;
|
||||||
|
const filePath = getDailyRenameLogPath(dayKey);
|
||||||
|
try {
|
||||||
|
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
|
||||||
|
await fs.promises.appendFile(filePath, chunk, "utf8");
|
||||||
|
} catch { /* ignore */ }
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
flushInFlight = false;
|
||||||
|
if (pendingLogLines.length > 0 || pendingRenameLines.length > 0) {
|
||||||
|
scheduleFlush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function flushSyncOnExit(): void {
|
||||||
|
const dayKey = currentDayKey || getDayKey();
|
||||||
|
|
||||||
|
if (pendingLogLines.length > 0) {
|
||||||
|
const chunk = pendingLogLines.join("");
|
||||||
|
pendingLogLines = [];
|
||||||
|
pendingLogChars = 0;
|
||||||
|
try {
|
||||||
|
const filePath = getDailyLogPath(dayKey);
|
||||||
|
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||||
|
fs.appendFileSync(filePath, chunk, "utf8");
|
||||||
|
} catch { /* ignore */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pendingRenameLines.length > 0) {
|
||||||
|
const chunk = pendingRenameLines.join("");
|
||||||
|
pendingRenameLines = [];
|
||||||
|
pendingRenameChars = 0;
|
||||||
|
try {
|
||||||
|
const filePath = getDailyRenameLogPath(dayKey);
|
||||||
|
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||||
|
fs.appendFileSync(filePath, chunk, "utf8");
|
||||||
|
} catch { /* ignore */ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,31 +114,43 @@ function writeToDailyLog(line: string): void {
|
|||||||
if (!dailyLogDir) return;
|
if (!dailyLogDir) return;
|
||||||
|
|
||||||
const dayKey = getDayKey();
|
const dayKey = getDayKey();
|
||||||
const fd = ensureDayFile(dayKey);
|
if (dayKey !== currentDayKey) {
|
||||||
if (fd === null) return;
|
// Day changed — flush previous day's buffer first
|
||||||
|
if (currentDayKey && (pendingLogLines.length > 0 || pendingRenameLines.length > 0)) {
|
||||||
try {
|
void flushAsync();
|
||||||
fs.writeSync(fd, line);
|
|
||||||
} catch {
|
|
||||||
// Close and retry on next write
|
|
||||||
try { fs.closeSync(fd); } catch { /* ignore */ }
|
|
||||||
currentLogFd = null;
|
|
||||||
}
|
}
|
||||||
|
currentDayKey = dayKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
pendingLogLines.push(line);
|
||||||
|
pendingLogChars += line.length;
|
||||||
|
|
||||||
|
// Shed oldest lines if buffer too large
|
||||||
|
while (pendingLogChars > BUFFER_LIMIT_CHARS && pendingLogLines.length > 1) {
|
||||||
|
const removed = pendingLogLines.shift();
|
||||||
|
if (removed) pendingLogChars -= removed.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleFlush();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeToDailyRenameLog(line: string): void {
|
export function writeToDailyRenameLog(line: string): void {
|
||||||
if (!dailyLogDir) return;
|
if (!dailyLogDir) return;
|
||||||
|
|
||||||
const dayKey = getDayKey();
|
const dayKey = getDayKey();
|
||||||
const fd = ensureRenameFd(dayKey);
|
if (dayKey !== currentDayKey) {
|
||||||
if (fd === null) return;
|
currentDayKey = dayKey;
|
||||||
|
|
||||||
try {
|
|
||||||
fs.writeSync(fd, line);
|
|
||||||
} catch {
|
|
||||||
try { fs.closeSync(fd); } catch { /* ignore */ }
|
|
||||||
currentRenameFd = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pendingRenameLines.push(line);
|
||||||
|
pendingRenameChars += line.length;
|
||||||
|
|
||||||
|
while (pendingRenameChars > BUFFER_LIMIT_CHARS && pendingRenameLines.length > 1) {
|
||||||
|
const removed = pendingRenameLines.shift();
|
||||||
|
if (removed) pendingRenameChars -= removed.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleFlush();
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanupOldDailyLogs(): void {
|
function cleanupOldDailyLogs(): void {
|
||||||
@ -136,7 +181,6 @@ function cleanupOldDailyLogs(): void {
|
|||||||
} catch { /* ignore */ }
|
} catch { /* ignore */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove empty month dirs
|
|
||||||
try {
|
try {
|
||||||
const remaining = fs.readdirSync(monthPath);
|
const remaining = fs.readdirSync(monthPath);
|
||||||
if (remaining.length === 0) {
|
if (remaining.length === 0) {
|
||||||
@ -156,16 +200,17 @@ export function initDailyLog(baseDir: string): void {
|
|||||||
fs.mkdirSync(dailyLogDir, { recursive: true });
|
fs.mkdirSync(dailyLogDir, { recursive: true });
|
||||||
} catch { /* ignore */ }
|
} catch { /* ignore */ }
|
||||||
|
|
||||||
// Attach listener to main logger
|
currentDayKey = getDayKey();
|
||||||
|
|
||||||
logListener = (line: string) => writeToDailyLog(line);
|
logListener = (line: string) => writeToDailyLog(line);
|
||||||
addLogListener(logListener);
|
addLogListener(logListener);
|
||||||
|
|
||||||
// Initial cleanup
|
|
||||||
cleanupOldDailyLogs();
|
cleanupOldDailyLogs();
|
||||||
|
|
||||||
// Periodic cleanup
|
|
||||||
cleanupTimer = setInterval(cleanupOldDailyLogs, CLEANUP_CHECK_INTERVAL_MS);
|
cleanupTimer = setInterval(cleanupOldDailyLogs, CLEANUP_CHECK_INTERVAL_MS);
|
||||||
if (cleanupTimer.unref) cleanupTimer.unref();
|
if (cleanupTimer.unref) cleanupTimer.unref();
|
||||||
|
|
||||||
|
process.once("exit", flushSyncOnExit);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function shutdownDailyLog(): void {
|
export function shutdownDailyLog(): void {
|
||||||
@ -177,14 +222,11 @@ export function shutdownDailyLog(): void {
|
|||||||
clearInterval(cleanupTimer);
|
clearInterval(cleanupTimer);
|
||||||
cleanupTimer = null;
|
cleanupTimer = null;
|
||||||
}
|
}
|
||||||
if (currentLogFd !== null) {
|
if (flushTimer) {
|
||||||
try { fs.closeSync(currentLogFd); } catch { /* ignore */ }
|
clearTimeout(flushTimer);
|
||||||
currentLogFd = null;
|
flushTimer = null;
|
||||||
}
|
|
||||||
if (currentRenameFd !== null) {
|
|
||||||
try { fs.closeSync(currentRenameFd); } catch { /* ignore */ }
|
|
||||||
currentRenameFd = null;
|
|
||||||
}
|
}
|
||||||
|
flushSyncOnExit();
|
||||||
currentDayKey = "";
|
currentDayKey = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4551,23 +4551,13 @@ export class DownloadManager extends EventEmitter {
|
|||||||
active.abortReason = "stop";
|
active.abortReason = "stop";
|
||||||
active.abortController.abort("stop");
|
active.abortController.abort("stop");
|
||||||
}
|
}
|
||||||
// Reset non-finished items. Items that were part of the current run
|
// Reset all non-finished items to clean "Wartet" / "Paket gestoppt" state
|
||||||
// (runItemIds) go back to "Wartet" so they are picked up by the next start().
|
|
||||||
// Items that were NOT in the run set are marked "Gestoppt" so a subsequent
|
|
||||||
// start() does not accidentally include the entire queue.
|
|
||||||
const hadRunItems = this.runItemIds.size > 0;
|
|
||||||
for (const item of Object.values(this.session.items)) {
|
for (const item of Object.values(this.session.items)) {
|
||||||
if (!isFinishedStatus(item.status)) {
|
if (!isFinishedStatus(item.status)) {
|
||||||
const pkg = this.session.packages[item.packageId];
|
|
||||||
const wasInRun = !hadRunItems || this.runItemIds.has(item.id);
|
|
||||||
if (wasInRun) {
|
|
||||||
item.status = "queued";
|
item.status = "queued";
|
||||||
item.fullStatus = pkg && !pkg.enabled ? "Paket gestoppt" : "Wartet";
|
|
||||||
} else {
|
|
||||||
item.status = "cancelled";
|
|
||||||
item.fullStatus = "Gestoppt";
|
|
||||||
}
|
|
||||||
item.speedBps = 0;
|
item.speedBps = 0;
|
||||||
|
const pkg = this.session.packages[item.packageId];
|
||||||
|
item.fullStatus = pkg && !pkg.enabled ? "Paket gestoppt" : "Wartet";
|
||||||
item.updatedAt = nowMs();
|
item.updatedAt = nowMs();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user