Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Fix auto-save ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Dalby committed Nov 20, 2023
1 parent 5807a7d commit 33acc93
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions server/server_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *ServerController) Startup(ctx context.Context) {
c.serverDir = serverDir

c.StartServersWithApplication()
c.RunAutoSaveTimers()
c.RunAutoSaveTimer()
}

// endregion
Expand All @@ -87,18 +87,25 @@ func (c *ServerController) StartServersWithApplication() {
// having one timer that manages all servers is advantageous:
// - catches conditions where a user enables auto-save after the timer has started
// - catches interval changes made after the timer has started
func (c *ServerController) RunAutoSaveTimers() {
func (c *ServerController) RunAutoSaveTimer() {
c.autoSaveIterations = 0

autoSave := time.NewTicker(time.Minute)
for {
select {
case <-autoSave.C:
c.AutoSaveServers()
default:
continue
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case <-autoSave.C:
c.AutoSaveServers()
}
}
}
}()

autoSave.Stop()
done <- true
runtime.LogInfof(c.ctx, "Auto-Save Stopped")
}

func (c *ServerController) AutoSaveServers() {
Expand All @@ -122,16 +129,16 @@ func (c *ServerController) AutoSaveServers() {
if server.AutoSaveEnabled {
// if interval is multiple of iterations
if server.AutoSaveInterval <= 0 {
runtime.LogError(c.ctx, "Server auto-save interval set 0 or below")
//runtime.LogError(c.ctx, "Server auto-save interval set 0 or below")
continue
}
if c.autoSaveIterations%server.AutoSaveInterval == 0 {
runtime.LogInfo(c.ctx, "Running autosave for "+server.ServerName)
//runtime.LogInfo(c.ctx, "Running autosave for "+server.ServerName)

err = server.SaveWorld()
if err != nil {
newErr := fmt.Errorf("Server auto-save created an error: " + err.Error())
runtime.LogErrorf(c.ctx, newErr.Error())
//newErr := fmt.Errorf("Server auto-save created an error: " + err.Error())
//runtime.LogErrorf(c.ctx, newErr.Error())
}
}
}
Expand Down

0 comments on commit 33acc93

Please sign in to comment.