mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 23:33:12 +09:00
Keep auto paused instances paused
Every RCON command makes an auto paused Factorio server run one tick, so subspace_storage's 1 s inventory update and 5 s ping, plus a statistics_exporter gather on every Prometheus scrape, kept a server with nobody online running at about 1.3 ticks per second. The instance now records the auto_pause server setting it was started with and exposes autoPaused, true while that setting is on and no players are online, along with an autoPauseChanged hook. subspace_storage skips its timers while paused and sends a ping and inventory as soon as a player joins. statistics_exporter gathers once after the game pauses and keeps serving those values until it resumes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ebdbb1fe56
commit
11e067a933
11 files changed
+227
-19
No files matched your search
@@ -23,6 +23,7 @@ export class InstanceHooks extends lib.AsyncHookCollection {
|
||||
this.controllerConnectionEvent = this.newHook();
|
||||
this.prepareControllerDisconnect = this.newHook();
|
||||
this.playerEvent = this.newHook();
|
||||
this.autoPauseChanged = this.newHook();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,6 +148,19 @@ export class InstanceHooks extends lib.AsyncHookCollection {
|
||||
* @param event - Information about the event.
|
||||
*/
|
||||
readonly playerEvent: lib.AsyncHook<[event: lib.PlayerEvent]>;
|
||||
|
||||
/**
|
||||
* Called when Factorio auto pauses or unpauses the game
|
||||
*
|
||||
* Invoked when the last player leaves or the first player joins an
|
||||
* instance started with the auto_pause server setting on. Every RCON
|
||||
* command makes a paused game run one tick, so periodic commands
|
||||
* should be skipped while paused. See `Instance.autoPaused`. It is
|
||||
* not invoked on start, read `autoPaused` in the start hook instead.
|
||||
*
|
||||
* @param paused - true if the game is now paused.
|
||||
*/
|
||||
readonly autoPauseChanged: lib.AsyncHook<[paused: boolean]>;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
||||
@@ -160,6 +174,7 @@ export interface BaseInstancePlugin {
|
||||
onControllerConnectionEvent?(event: "connect" | "drop" | "resume" | "close"): void;
|
||||
onPrepareControllerDisconnect?(connection: Instance): Promise<void>;
|
||||
onPlayerEvent?(event: lib.PlayerEvent): Promise<void>;
|
||||
onAutoPauseChanged?(paused: boolean): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,6 +237,7 @@ export class BaseInstancePlugin {
|
||||
attach(instance.hooks.controllerConnectionEvent, this.onControllerConnectionEvent);
|
||||
attach(instance.hooks.prepareControllerDisconnect, this.onPrepareControllerDisconnect);
|
||||
attach(instance.hooks.playerEvent, this.onPlayerEvent);
|
||||
attach(instance.hooks.autoPauseChanged, this.onAutoPauseChanged);
|
||||
}
|
||||
|
||||
static fromContext(context: InstancePluginContext) {
|
||||
|
||||
@@ -190,6 +190,7 @@ export default class Instance extends lib.Link {
|
||||
_loadedSave: string | null = null;
|
||||
_playerCheckInterval: ReturnType<typeof setInterval> | undefined;
|
||||
_hadPlayersOnline = false;
|
||||
_autoPause = false;
|
||||
_playerAutosaveSlot = 1;
|
||||
_expectedUserUpdates: { action: string, name: string, reason: string }[] = [];
|
||||
_settleWaiters: (() => void)[] = [];
|
||||
@@ -388,10 +389,19 @@ export default class Instance extends lib.Link {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True while Factorio has paused the game because auto_pause is on
|
||||
* and no players are online.
|
||||
*/
|
||||
get autoPaused() {
|
||||
return this._autoPause && this.playersOnline.size === 0;
|
||||
}
|
||||
|
||||
_recordPlayerJoin(name: string) {
|
||||
if (this.playersOnline.has(name)) {
|
||||
return;
|
||||
}
|
||||
const wasPaused = this.autoPaused;
|
||||
this.playersOnline.add(name);
|
||||
|
||||
let stats = this.playerStats.get(name);
|
||||
@@ -411,6 +421,9 @@ export default class Instance extends lib.Link {
|
||||
stats,
|
||||
};
|
||||
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("join", name, stats));
|
||||
if (wasPaused) {
|
||||
this.hooks.autoPauseChanged.invoke(false);
|
||||
}
|
||||
this.hooks.playerEvent.invoke(event);
|
||||
}
|
||||
|
||||
@@ -432,6 +445,9 @@ export default class Instance extends lib.Link {
|
||||
stats,
|
||||
};
|
||||
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("leave", name, stats, reason));
|
||||
if (this.autoPaused) {
|
||||
this.hooks.autoPauseChanged.invoke(true);
|
||||
}
|
||||
this.hooks.playerEvent.invoke(event);
|
||||
}
|
||||
|
||||
@@ -628,6 +644,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
|
||||
notifyExit() {
|
||||
this._loadedSave = null;
|
||||
this._autoPause = false;
|
||||
this.notifyStatus("stopped");
|
||||
this.connector.emit("close");
|
||||
|
||||
@@ -857,6 +874,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
this.server.writePath("server-settings.json"),
|
||||
JSON.stringify(serverSettings, null, "\t")
|
||||
);
|
||||
return serverSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -971,7 +989,9 @@ end`.replace(/\r?\n/g, " ");
|
||||
*/
|
||||
async prepare() {
|
||||
this.logger.verbose("Writing server-settings.json");
|
||||
await this.writeServerSettings(true);
|
||||
// auto_pause is only read on startup
|
||||
const serverSettings = await this.writeServerSettings(true);
|
||||
this._autoPause = serverSettings.auto_pause === true;
|
||||
this._expectedUserUpdates = [];
|
||||
|
||||
if (this.config.get("factorio.sync_adminlist") !== "disabled") {
|
||||
|
||||
Reference in new issue
Block a user