mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Merge branch 'master' into fix/stream-reject-closes-connection
This commit is contained in:
21 files changed
+465
-31
No files matched your search
@@ -38,6 +38,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;
|
||||
@@ -101,6 +102,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.",
|
||||
@@ -357,6 +365,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;
|
||||
@@ -466,6 +475,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",
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -460,6 +460,39 @@ export interface SubscribableValue {
|
||||
* Multiple handlers can be subscribed at the same time
|
||||
*/
|
||||
export class EventSubscriber<E, S = null> {
|
||||
/** Subscribers created on each connector, they share one connect and one close listener on it */
|
||||
private static connectorSubscribers = new WeakMap<object, Set<EventSubscriber<any, any>>>();
|
||||
|
||||
/**
|
||||
* Pass connect and close events of a connector on to a subscriber
|
||||
* Uses one listener for all subscribers, so that plugins creating many subscribers do not go past the
|
||||
* listener limit set on the connector.
|
||||
* @param connector - connector of the link the subscriber was created on
|
||||
* @param subscriber - subscriber to pass the events to
|
||||
*/
|
||||
private static addConnectorSubscriber(
|
||||
connector: WebSocketBaseConnector,
|
||||
subscriber: EventSubscriber<any, any>,
|
||||
) {
|
||||
let subscribers = EventSubscriber.connectorSubscribers.get(connector);
|
||||
if (!subscribers) {
|
||||
const created = new Set<EventSubscriber<any, any>>();
|
||||
EventSubscriber.connectorSubscribers.set(connector, created);
|
||||
connector.on("connect", () => {
|
||||
for (const entry of created) {
|
||||
entry.handleConnectionEvent("connect");
|
||||
}
|
||||
});
|
||||
connector.on("close", () => {
|
||||
for (const entry of created) {
|
||||
entry.handleConnectionEvent("close");
|
||||
}
|
||||
});
|
||||
subscribers = created;
|
||||
}
|
||||
subscribers.add(subscriber);
|
||||
}
|
||||
|
||||
/** The time at which an event was last received, is less than 0 when there have been no events */
|
||||
lastUpdatedMs = -1;
|
||||
/** True if this subscriber is currently synced with the source */
|
||||
@@ -485,13 +518,7 @@ export class EventSubscriber<E, S = null> {
|
||||
) {
|
||||
control.handle(Event, this._handleEvent.bind(this));
|
||||
// The events below exist only on websocket connectors, we can't early return because of test mocking
|
||||
const webSocketConnector = control.connector as WebSocketBaseConnector;
|
||||
webSocketConnector.on("connect", () => {
|
||||
this.handleConnectionEvent("connect");
|
||||
});
|
||||
webSocketConnector.on("close", () => {
|
||||
this.handleConnectionEvent("close");
|
||||
});
|
||||
EventSubscriber.addConnectorSubscriber(control.connector as WebSocketBaseConnector, this);
|
||||
// Only the web Control reports account changes
|
||||
const accountControl = control as Link & { onAccountUpdate?: (handler: () => void) => void };
|
||||
if (typeof accountControl.onAccountUpdate === "function") {
|
||||
|
||||
Reference in new issue
Block a user