feat: add automation features (auto-update, auto-start, scheduled updates)
This commit is contained in:
@@ -12,6 +12,7 @@ type Handler struct {
|
||||
modlists *services.ModlistManager
|
||||
process *services.ProcessManager
|
||||
steamcmd *services.SteamCmdManager
|
||||
scheduler *services.Scheduler
|
||||
streamer *services.LogStreamer
|
||||
serverfileDir string
|
||||
modsDir string
|
||||
@@ -25,6 +26,7 @@ func New(
|
||||
modlists *services.ModlistManager,
|
||||
process *services.ProcessManager,
|
||||
steamcmd *services.SteamCmdManager,
|
||||
scheduler *services.Scheduler,
|
||||
streamer *services.LogStreamer,
|
||||
serverfileDir, modsDir, cfgDir, profilesDir string,
|
||||
) *Handler {
|
||||
@@ -34,6 +36,7 @@ func New(
|
||||
modlists: modlists,
|
||||
process: process,
|
||||
steamcmd: steamcmd,
|
||||
scheduler: scheduler,
|
||||
streamer: streamer,
|
||||
serverfileDir: serverfileDir,
|
||||
modsDir: modsDir,
|
||||
|
||||
@@ -7,16 +7,20 @@ import (
|
||||
)
|
||||
|
||||
type updateSettingsInput struct {
|
||||
IPPort *string `json:"ip_port"`
|
||||
ServerParameters *string `json:"server_parameters"`
|
||||
SteamBranch *string `json:"steam_branch"`
|
||||
SteamUser *string `json:"steam_user"`
|
||||
Platform *string `json:"platform"`
|
||||
CBASettings *string `json:"cba_settings"`
|
||||
AILevelPresets *string `json:"ai_level_presets"`
|
||||
DifficultyPresets *string `json:"difficulty_presets"`
|
||||
ActiveConfig *string `json:"active_config"`
|
||||
ActiveModlist *string `json:"active_modlist"`
|
||||
IPPort *string `json:"ip_port"`
|
||||
ServerParameters *string `json:"server_parameters"`
|
||||
SteamBranch *string `json:"steam_branch"`
|
||||
SteamUser *string `json:"steam_user"`
|
||||
Platform *string `json:"platform"`
|
||||
CBASettings *string `json:"cba_settings"`
|
||||
AILevelPresets *string `json:"ai_level_presets"`
|
||||
DifficultyPresets *string `json:"difficulty_presets"`
|
||||
ActiveConfig *string `json:"active_config"`
|
||||
ActiveModlist *string `json:"active_modlist"`
|
||||
AutoUpdateOnStartup *bool `json:"auto_update_on_startup"`
|
||||
AutoStartOnStartup *bool `json:"auto_start_on_startup"`
|
||||
AutoUpdateModsOnStartup *bool `json:"auto_update_mods_on_startup"`
|
||||
ScheduledUpdate *string `json:"scheduled_update"`
|
||||
}
|
||||
|
||||
func (h *Handler) GetSettings(c *gin.Context) {
|
||||
@@ -51,6 +55,10 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
|
||||
if input.DifficultyPresets != nil { s.DifficultyPresets = *input.DifficultyPresets }
|
||||
if input.ActiveConfig != nil { s.ActiveConfig = *input.ActiveConfig }
|
||||
if input.ActiveModlist != nil { s.ActiveModlist = *input.ActiveModlist }
|
||||
if input.AutoUpdateOnStartup != nil { s.AutoUpdateOnStartup = *input.AutoUpdateOnStartup }
|
||||
if input.AutoStartOnStartup != nil { s.AutoStartOnStartup = *input.AutoStartOnStartup }
|
||||
if input.AutoUpdateModsOnStartup != nil { s.AutoUpdateModsOnStartup = *input.AutoUpdateModsOnStartup }
|
||||
if input.ScheduledUpdate != nil { s.ScheduledUpdate = *input.ScheduledUpdate }
|
||||
|
||||
if err := h.process.WriteUserconfigFiles(s); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "write userconfig: " + err.Error()})
|
||||
@@ -61,6 +69,11 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if h.scheduler != nil {
|
||||
h.scheduler.Refresh()
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, s)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,15 +3,20 @@ package models
|
||||
import "time"
|
||||
|
||||
type ServerSettings struct {
|
||||
IPPort string `json:"ip_port"`
|
||||
ServerParameters string `json:"server_parameters"`
|
||||
SteamBranch string `json:"steam_branch"`
|
||||
SteamUser string `json:"steam_user"`
|
||||
Platform string `json:"platform"`
|
||||
CBASettings string `json:"cba_settings"`
|
||||
AILevelPresets string `json:"ai_level_presets"`
|
||||
DifficultyPresets string `json:"difficulty_presets"`
|
||||
ActiveConfig string `json:"active_config"`
|
||||
ActiveModlist string `json:"active_modlist"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
IPPort string `json:"ip_port"`
|
||||
ServerParameters string `json:"server_parameters"`
|
||||
SteamBranch string `json:"steam_branch"`
|
||||
SteamUser string `json:"steam_user"`
|
||||
Platform string `json:"platform"`
|
||||
CBASettings string `json:"cba_settings"`
|
||||
AILevelPresets string `json:"ai_level_presets"`
|
||||
DifficultyPresets string `json:"difficulty_presets"`
|
||||
ActiveConfig string `json:"active_config"`
|
||||
ActiveModlist string `json:"active_modlist"`
|
||||
AutoUpdateOnStartup bool `json:"auto_update_on_startup"`
|
||||
AutoStartOnStartup bool `json:"auto_start_on_startup"`
|
||||
AutoUpdateModsOnStartup bool `json:"auto_update_mods_on_startup"`
|
||||
WasRunning bool `json:"was_running"`
|
||||
ScheduledUpdate string `json:"scheduled_update"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
92
backend/internal/services/scheduler.go
Normal file
92
backend/internal/services/scheduler.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
type Scheduler struct {
|
||||
cron *cron.Cron
|
||||
entryID cron.EntryID
|
||||
settings *SettingsManager
|
||||
modlists *ModlistManager
|
||||
steamcmd *SteamCmdManager
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewScheduler(settings *SettingsManager, modlists *ModlistManager, steamcmd *SteamCmdManager) *Scheduler {
|
||||
return &Scheduler{
|
||||
cron: cron.New(),
|
||||
settings: settings,
|
||||
modlists: modlists,
|
||||
steamcmd: steamcmd,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) Start() {
|
||||
s.cron.Start()
|
||||
s.Refresh()
|
||||
}
|
||||
|
||||
func (s *Scheduler) Stop() {
|
||||
s.cron.Stop()
|
||||
}
|
||||
|
||||
func (s *Scheduler) Refresh() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if s.entryID != 0 {
|
||||
s.cron.Remove(s.entryID)
|
||||
s.entryID = 0
|
||||
}
|
||||
|
||||
sett, err := s.settings.Load()
|
||||
if err != nil || sett.ScheduledUpdate == "" {
|
||||
return
|
||||
}
|
||||
|
||||
id, err := s.cron.AddFunc(sett.ScheduledUpdate, s.runScheduledUpdate)
|
||||
if err != nil {
|
||||
log.Printf("scheduler: invalid cron expression %q: %v", sett.ScheduledUpdate, err)
|
||||
return
|
||||
}
|
||||
s.entryID = id
|
||||
log.Printf("scheduler: scheduled update with cron %q", sett.ScheduledUpdate)
|
||||
}
|
||||
|
||||
func (s *Scheduler) runScheduledUpdate() {
|
||||
sett, err := s.settings.Load()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if sett.SteamUser != "" && sett.SteamUser != "anonymous" {
|
||||
log.Print("scheduler: running gameserver update")
|
||||
if err := s.steamcmd.UpdateGame(sett.SteamBranch, sett.SteamUser); err != nil {
|
||||
log.Printf("scheduler: game update failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if sett.ActiveModlist != "" {
|
||||
ml, err := s.modlists.Get(sett.ActiveModlist)
|
||||
if err != nil {
|
||||
log.Printf("scheduler: load modlist %s: %v", sett.ActiveModlist, err)
|
||||
return
|
||||
}
|
||||
var modIDs []string
|
||||
for _, m := range ml.Mods {
|
||||
if m.Enabled {
|
||||
modIDs = append(modIDs, m.ID)
|
||||
}
|
||||
}
|
||||
if len(modIDs) > 0 {
|
||||
log.Printf("scheduler: updating %d mod(s)", len(modIDs))
|
||||
if err := s.steamcmd.DownloadMods(modIDs); err != nil {
|
||||
log.Printf("scheduler: mod update failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,11 @@ func (pm *ProcessManager) Start() error {
|
||||
return fmt.Errorf("load settings: %w", err)
|
||||
}
|
||||
|
||||
s.WasRunning = true
|
||||
if err := pm.settings.Save(s); err != nil {
|
||||
return fmt.Errorf("save was_running: %w", err)
|
||||
}
|
||||
|
||||
binName := "arma3server_x64"
|
||||
if s.Platform == "windows" {
|
||||
binName = "arma3server_x64.exe"
|
||||
@@ -141,6 +146,13 @@ func (pm *ProcessManager) Stop() error {
|
||||
|
||||
rp.cancel()
|
||||
<-rp.exited
|
||||
|
||||
s, err := pm.settings.Load()
|
||||
if err == nil {
|
||||
s.WasRunning = false
|
||||
pm.settings.Save(s)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -54,13 +54,18 @@ func (sm *SettingsManager) Save(s *models.ServerSettings) error {
|
||||
|
||||
func (sm *SettingsManager) defaults() *models.ServerSettings {
|
||||
return &models.ServerSettings{
|
||||
IPPort: "0.0.0.0:2302",
|
||||
ServerParameters: "-server -world=empty -loadMissionToMemory -noPause",
|
||||
SteamBranch: "stable",
|
||||
SteamUser: "anonymous",
|
||||
Platform: "linux",
|
||||
ActiveConfig: "",
|
||||
ActiveModlist: "",
|
||||
UpdatedAt: time.Now().UTC(),
|
||||
IPPort: "0.0.0.0:2302",
|
||||
ServerParameters: "-server -world=empty -loadMissionToMemory -noPause",
|
||||
SteamBranch: "stable",
|
||||
SteamUser: "anonymous",
|
||||
Platform: "linux",
|
||||
ActiveConfig: "",
|
||||
ActiveModlist: "",
|
||||
AutoUpdateOnStartup: false,
|
||||
AutoStartOnStartup: false,
|
||||
AutoUpdateModsOnStartup: false,
|
||||
WasRunning: false,
|
||||
ScheduledUpdate: "",
|
||||
UpdatedAt: time.Now().UTC(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user