Merge pull request #1200 from bbassie/feature/log-retention

Delete log files older than a configurable number of days
This commit is contained in:
Bastiaan authored and GitHub committed 2026-11-13 19:30:24 +00:00
1 parent 9ec817c1c6
commit e37bf31cd3
6 files changed
+177 -1

No files matched your search

+5
View File
@@ -492,6 +492,11 @@ async function startup() {
controller.stop();
});
lib.startLogRetention(
[path.join(args.logDirectory, "cluster"), path.join(args.logDirectory, "controller")],
() => controllerConfig.get("controller.log_retention_days"),
);
await controller.start(args);
}
+5
View File
@@ -210,6 +210,11 @@ async function startHost() {
// From this point on the host should run
await hostConfigLock.acquire();
lib.startLogRetention(
[path.join(args.logDirectory, "host")],
() => hostConfig.get("host.log_retention_days"),
);
// If we get here the command was run
await fs.mkdir(hostConfig.get("host.instances_directory"), { recursive: true });
await fs.mkdir(hostConfig.get("host.mods_directory"), { recursive: true });
+16
View File
@@ -37,6 +37,7 @@ export interface ControllerConfigFields {
"controller.default_mod_pack_id": number | null;
"controller.default_role_id": number | null;
"controller.autosave_interval": number;
"controller.log_retention_days": number;
"controller.host_pairing_timeout": number;
"controller.host_pairing_max_pending": number;
"controller.mod_portal_cache_duration_minutes": number;
@@ -100,6 +101,13 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
initialValue: 60,
validator: validators.greaterThanZero,
},
"controller.log_retention_days": {
title: "Log Retention Days",
description: "Days to keep controller and cluster log files for, 0 to keep them forever.",
type: "number",
initialValue: 30,
validator: validators.all(validators.integer, validators.greaterThanEqualZero),
},
"controller.host_pairing_timeout": {
title: "Host Pairing Timeout",
description: "Time in seconds a host can wait for approval before it has to request again, 0 for no limit.",
@@ -349,6 +357,7 @@ export interface HostConfigFields {
"host.factorio_username": string | null,
"host.factorio_token": string | null,
"host.max_reconnect_delay": number;
"host.log_retention_days": number;
"host.allow_remote_updates": boolean;
"host.allow_plugin_updates": boolean;
"host.allow_plugin_install": boolean;
@@ -458,6 +467,13 @@ export class HostConfig extends classes.Config<HostConfigFields> {
initialValue: 60,
validator: validators.greaterThanZero,
},
"host.log_retention_days": {
title: "Log Retention Days",
description: "Days to keep host log files for, 0 to keep them forever.",
type: "number",
initialValue: 30,
validator: validators.all(validators.integer, validators.greaterThanEqualZero),
},
"host.allow_remote_updates": {
description: "When true, allows a remote event to trigger a clusterio update via npm",
type: "boolean",
+82 -1
View File
@@ -358,8 +358,15 @@ export class LogIndex {
*/
async buildIndex() {
let today = new Date(Date.now() - 300e3).toISOString().slice(0, 10);
let files = await fs.readdir(this.logDirectory);
for (let file of await fs.readdir(this.logDirectory)) {
for (let file of this.index.keys()) {
if (!files.includes(file)) {
this.index.delete(file);
}
}
for (let file of files) {
let match = logFileGlob.exec(file);
if (!match || match[1] >= today || this.index.has(file)) {
continue;
@@ -412,6 +419,80 @@ export class LogIndex {
}
}
/**
* Delete dated log files older than the given number of days
*
* Only files named like name-YYYY-MM-DD.log are deleted, and only once
* every entry they can hold is older than retentionDays.
*
* @param logDirectory - Directory to delete log files from.
* @param retentionDays - Days to keep log files for, 0 keeps them forever.
* @param now - Current time in milliseconds since the epoch.
* @returns names of the deleted files.
*/
export async function deleteOldLogs(logDirectory: string, retentionDays: number, now = Date.now()) {
if (!(retentionDays > 0)) {
return [];
}
let files: string[];
try {
files = await fs.readdir(logDirectory);
} catch (err: any) {
if (err.code === "ENOENT") {
return [];
}
throw err;
}
const cutoff = new Date(now - retentionDays * 86400e3).toISOString().slice(0, 10);
const deleted: string[] = [];
for (const file of files) {
const match = logFileGlob.exec(file);
if (!match || match[1] >= cutoff) {
continue;
}
try {
await fs.unlink(path.join(logDirectory, file));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
}
deleted.push(file);
}
return deleted;
}
/**
* Delete old logs now and then every hour
*
* @param logDirectories - Directories to delete old log files from.
* @param getRetentionDays - Called before each run to get the retention.
* @returns the interval timer, which does not keep the process alive.
*/
export function startLogRetention(logDirectories: string[], getRetentionDays: () => number) {
async function run() {
const retentionDays = getRetentionDays();
for (const logDirectory of logDirectories) {
try {
const deleted = await deleteOldLogs(logDirectory, retentionDays);
if (deleted.length) {
logger.info(
`Deleted ${deleted.length} log files older than ${retentionDays} days from ${logDirectory}`
);
}
} catch (err: any) {
logger.error(`Failed to delete old logs from ${logDirectory}:\n${err.stack}`);
}
}
}
run();
const interval = setInterval(run, 3600e3);
interval.unref();
return interval;
}
/**
* Filter object for querying logs
*/