mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 23:24:52 +09:00
Implement autosaving of data held in memory
Add controller.autosave_interval config that specifies the frequency which controller config, hosts, instances, users, mod packs and plugin specific data which is held in memory should be automatically flushed to disk if they have changed since the last time they were saved. This prevents data loss in the event of a server crash. Plugins can use the onSaveData hook to save its own data at the same time as the controller does and this obsoletes making an autosave interval in the plugin. Resolves #523.
This commit is contained in:
1 parent
faadf873fb
commit
ffe5040644
14 files changed
+168
-57
No files matched your search
@@ -51,6 +51,7 @@ async function saveTechnologies(
|
||||
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
technologies!: Map<string, Technology>;
|
||||
technologiesDirty = true;
|
||||
progressRateLimiter!: lib.RateLimiter;
|
||||
progressBroadcastId!: ReturnType<typeof setInterval> | null;
|
||||
progressToBroadcast!: Set<string>;
|
||||
@@ -70,9 +71,15 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
this.controller.handle(SyncTechnologiesRequest, this.handleSyncTechnologiesRequest.bind(this));
|
||||
}
|
||||
|
||||
async onSaveData() {
|
||||
if (this.technologiesDirty) {
|
||||
this.technologiesDirty = false;
|
||||
await saveTechnologies(this.controller.config, this.technologies, this.logger);
|
||||
}
|
||||
}
|
||||
|
||||
async onShutdown() {
|
||||
this.progressRateLimiter.cancel();
|
||||
await saveTechnologies(this.controller.config, this.technologies, this.logger);
|
||||
}
|
||||
|
||||
broadcastProgress() {
|
||||
@@ -96,6 +103,7 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
if (!tech) {
|
||||
tech = { level, progress: 0, researched: false };
|
||||
this.technologies.set(name, tech);
|
||||
this.technologiesDirty = true;
|
||||
|
||||
// Ignore contribution to already researched technologies
|
||||
} else if (tech.level > level || tech.level === level && tech.researched) {
|
||||
@@ -126,6 +134,7 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
|
||||
this.controller.sendTo("allInstances", new FinishedEvent(name, tech.level));
|
||||
}
|
||||
this.technologiesDirty = true;
|
||||
}
|
||||
|
||||
async handleFinishedEvent(event: FinishedEvent) {
|
||||
@@ -134,6 +143,7 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
if (!tech || tech.level <= level) {
|
||||
this.progressToBroadcast.delete(name);
|
||||
this.technologies.set(name, { level, progress: null, researched: true });
|
||||
this.technologiesDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,6 +161,7 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
let tech = this.technologies.get(name);
|
||||
if (!tech) {
|
||||
this.technologies.set(name, { level, progress, researched });
|
||||
this.technologiesDirty = true;
|
||||
if (progress) {
|
||||
this.progressToBroadcast.add(name);
|
||||
} else if (researched || baseLevel(name) !== level) {
|
||||
@@ -176,10 +187,12 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
} else {
|
||||
this.progressToBroadcast.delete(name);
|
||||
}
|
||||
this.technologiesDirty = true;
|
||||
|
||||
} else if (tech.progress && progress && tech.progress < progress) {
|
||||
tech.progress = progress;
|
||||
this.progressToBroadcast.add(name);
|
||||
this.technologiesDirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user