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

Commit

Permalink
Remove auto-save ticker (#129)
Browse files Browse the repository at this point in the history
Co-authored-by: Jordan Dalby <[email protected]>
  • Loading branch information
jordan-dalby and Jordan Dalby authored Nov 21, 2023
1 parent 289415f commit e8f3aa6
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 145 deletions.
53 changes: 0 additions & 53 deletions frontend/src/pages/server/Administration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,58 +213,6 @@ function ServerStartupCard({setServ, serv}: {setServ: React.Dispatch<React.SetSt
)
}

function AutoSaveSettingsCard({ setServ, serv }: {setServ: React.Dispatch<React.SetStateAction<server.Server>>, serv: server.Server}) {
const {addAlert} = useAlert();

const handleAutoSaveIntervalChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newAutoSaveInterval = parseInt(e.target.value, 10);

if (newAutoSaveInterval > 0) {
setServ((prev) => ({
...prev,
autoSaveInterval: newAutoSaveInterval,
convertValues: prev.convertValues,
}));
} else {
addAlert("'Auto-Save Interval' must be greater than 0", "danger")
}
};
return (
<Card variant="soft" className={''}>
<Typography level="title-md">
Auto-Save Settings
</Typography>
<Divider className={'mx-2'}/>

<div className={'space-x-4 w-full flex'}>
<div className={'inline-block'}>
<Checkbox label="Enable Auto-Save" checked={serv?.autoSaveEnabled}
onChange={(e) => setServ((p) => ({
...p,
autoSaveEnabled: e.target.checked,
convertValues: p.convertValues
}))}/><br/>

<FormLabel>Auto-Save Interval (minutes)</FormLabel>
<Input className={''} type={'number'} required value={serv?.autoSaveInterval} disabled={!serv?.autoSaveEnabled}
onChange={(e) => {
if (parseInt(e.target.value) < 1) {
e.target.value = "1"
return
}
setServ((p) => ({
...p,
autoSaveInterval: parseInt(e.target.value),
convertValues: p.convertValues
}))
}}>
</Input>
</div>
</div>
</Card>
)
}

function ExtraSettingsCard({setServ, serv}: {setServ: React.Dispatch<React.SetStateAction<server.Server>>, serv: server.Server}) {
return (
<Card variant="soft" className={''}>
Expand Down Expand Up @@ -315,7 +263,6 @@ export function Administration({setServ, serv, onServerFilesDeleted}: Props) {
<TabPanel value={4} className={'space-y-8'}>
<ServerAdministrationCard serv={serv} setServ={setServ} onServerFilesDeleted={onServerFilesDeleted}/>
<ServerStartupCard serv={serv} setServ={setServ} />
<AutoSaveSettingsCard setServ={setServ} serv={serv}/>
<ExtraSettingsCard setServ={setServ} serv={serv}/>
</TabPanel>
);
Expand Down
16 changes: 5 additions & 11 deletions server/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package server

import (
"fmt"
"github.com/JensvandeWiel/ArkAscendedServerManager/helpers"
"github.com/go-ini/ini"
"github.com/sethvargo/go-password/password"
"github.com/wailsapp/wails/v2/pkg/runtime"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/JensvandeWiel/ArkAscendedServerManager/helpers"
"github.com/go-ini/ini"
"github.com/sethvargo/go-password/password"
"github.com/wailsapp/wails/v2/pkg/runtime"
)

var iniOpts = ini.LoadOptions{
Expand Down Expand Up @@ -103,9 +104,6 @@ func generateNewDefaultServer(id int) Server {
MaxPlayers: 70,

StartWithApplication: false,

AutoSaveEnabled: true,
AutoSaveInterval: 15,
}
}

Expand Down Expand Up @@ -164,10 +162,6 @@ func CheckIfServerCorrect(server *Server) error {
return fmt.Errorf("server.serverMap is empty")
}

if server.AutoSaveInterval <= 0 {
return fmt.Errorf("server.AutoSaveInterval is negative or zero, it must be higher than zero")
}

return nil
}

Expand Down
3 changes: 0 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ type Server struct {
MaxPlayers int `json:"maxPlayers"`

StartWithApplication bool `json:"startWithApplication"`

AutoSaveEnabled bool `json:"autoSaveEnabled"`
AutoSaveInterval int `json:"autoSaveInterval"`
}

// UpdateConfig updates the configuration files for the server e.g.: GameUserSettings.ini
Expand Down
82 changes: 4 additions & 78 deletions server/server_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"os"
"path"
"strconv"
"time"

"github.com/JensvandeWiel/ArkAscendedServerManager/helpers"
"github.com/adrg/xdg"
Expand All @@ -31,11 +30,10 @@ const (

// ServerController struct
type ServerController struct {
ctx context.Context
Servers map[int]*Server
helpers *helpers.HelpersController
autoSaveIterations int
serverDir string
ctx context.Context
Servers map[int]*Server
helpers *helpers.HelpersController
serverDir string
}

//region Struct Initialization and Creation
Expand Down Expand Up @@ -82,67 +80,6 @@ func (c *ServerController) StartServersWithApplication() {
}
}

// TODO Remove auto-save feature, it's built into ARK
func (c *ServerController) RunAutoSaveTimer() {
c.autoSaveIterations = 0

autoSave := time.NewTicker(time.Minute)
done := make(chan bool)

runtime.LogInfof(c.ctx, "Server Auto-Save Started")

go func() {
for {
select {
case <-c.ctx.Done(): // Check if the context is canceled
return
case <-done:
return
case <-autoSave.C:
c.AutoSaveServers()
}
}
}()
}

func (c *ServerController) AutoSaveServers() {
servers, err := c.getAllServers()
if err != nil {
newErr := fmt.Errorf("Error getting all servers " + err.Error())
runtime.LogErrorf(c.ctx, newErr.Error())
return
}

c.autoSaveIterations += 1
// seconds in a minute * hours in a day * days, increase days if more days are required
// d
if c.autoSaveIterations == (60 * 24 * 7) { // one week in seconds
// very early max int catch (definitely extendable in the future if necessary)
c.autoSaveIterations = 0
}

for id := range servers {
server := c.Servers[id]
if !server.IsServerRunning() || !server.AutoSaveEnabled {
continue
}

// if interval is multiple of iterations
if server.AutoSaveInterval <= 0 {
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)
err = server.SaveWorld()
if err != nil {
newErr := fmt.Errorf("Server auto-save created an error: " + err.Error())
runtime.LogErrorf(c.ctx, newErr.Error())
}
}
}
}

//endregion

//region Frontend functions
Expand Down Expand Up @@ -299,10 +236,6 @@ func (c *ServerController) getServerFromDir(id int, shouldReturnNew bool) (Serve
return Server{}, fmt.Errorf("Error unmarshalling server config file: " + err.Error())
}

if serv.AutoSaveInterval <= 0 {
serv.AutoSaveInterval = 15
}

// Check if server is correct.
if err := CheckIfServerCorrect(&serv); err != nil {
return Server{}, fmt.Errorf("Parsing server instance failed: " + err.Error())
Expand Down Expand Up @@ -351,10 +284,6 @@ func (c *ServerController) getServer(id int, addToMap bool) (*Server, error) {
return nil, fmt.Errorf("Error getting server instance: %s", err.Error())
}

if s.AutoSaveInterval <= 0 {
s.AutoSaveInterval = 15
}

if addToMap {
c.Servers[id] = &s
}
Expand All @@ -364,9 +293,6 @@ func (c *ServerController) getServer(id int, addToMap bool) (*Server, error) {
return &s, nil
} else {
runtime.EventsEmit(c.ctx, "gotServer", server.Id)
if server.AutoSaveInterval <= 0 {
server.AutoSaveInterval = 15
}
server.ctx = c.ctx
return server, nil
}
Expand Down

0 comments on commit e8f3aa6

Please sign in to comment.