Skip to content

Commit

Permalink
feat: Add accserver auto update before start (#220)
Browse files Browse the repository at this point in the history
Co-authored-by: Pedro Faria De Miranda Pinto <[email protected]>
  • Loading branch information
pedrofaria and Pedro Faria De Miranda Pinto authored Jul 7, 2022
1 parent 0451f31 commit 3eb1791
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Fix some typo errors. #210 #212
* Fix float point handling for rain and cloud. #211
* Introducing the advance windows features as Firewall management, custom core affinity and cpu priority. #213

* Add accserver auto update before start instance.

## 1.18.0
* Add sorting by number of players.
Expand Down
14 changes: 5 additions & 9 deletions internal/app/handler_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,11 @@ func (h *Handler) DeleteInstance(c *gin.Context) {
// @Router /instance/{id}/start [post]
// @Security JWT
func (h *Handler) StartInstance(c *gin.Context) {
id := c.Param("id")

srv, err := h.sm.GetServerByID(id)
if err != nil {
c.JSON(http.StatusNotFound, nil)
return
}

if err := srv.Start(); err != nil {
if err := h.sm.Start(c.Param("id")); err != nil {
if errors.Is(err, server_manager.ErrServerNotFound) {
c.JSON(http.StatusNotFound, nil)
return
}
if errors.Is(err, instance.ErrServerCantBeRunning) {
c.JSON(http.StatusBadRequest, newAccWError(err.Error()))
return
Expand Down
6 changes: 6 additions & 0 deletions internal/pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"time"

"golang.org/x/text/encoding/charmap"
Expand Down Expand Up @@ -50,13 +51,18 @@ type Instance struct {
cmd *exec.Cmd
logFile *os.File
cmdOut io.ReadCloser

lock sync.Mutex
}

func (s *Instance) GetID() string {
return s.Cfg.ID
}

func (s *Instance) Start() error {
s.lock.Lock()
defer s.lock.Unlock()

if s.IsRunning() {
return ErrServerCantBeRunning
}
Expand Down
55 changes: 44 additions & 11 deletions internal/pkg/server_manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type Config struct {
AccServerMd5Sum string
}

func (c Config) AccServerFullPath() string {
return path.Join(c.AccServerPath, c.AccServerExe)
}

type Service struct {
config *Config
servers map[string]*instance.Instance
Expand Down Expand Up @@ -103,7 +107,7 @@ func (s *Service) StopAll() error {
}

func (s *Service) GetAccServerExeMd5Sum() error {
sum, err := helper.CheckMd5Sum(path.Join(s.config.AccServerPath, s.config.AccServerExe))
sum, err := helper.CheckMd5Sum(s.config.AccServerFullPath())
if err != nil {
return err
}
Expand All @@ -117,18 +121,26 @@ func (s *Service) GetAccServerExeMd5Sum() error {

func (s *Service) UpdateServersServerExeFile() error {
for _, srv := range s.servers {
if srv.Cfg.Md5Sum == s.config.AccServerMd5Sum {
continue
if err := s.updateAccServerExeIfDifferent(srv); err != nil {
return err
}
}

if ok, err := srv.UpdateAccServerExe(path.Join(s.config.AccServerPath, s.config.AccServerExe)); err != nil {
return err
} else if ok {
srv.Cfg.SetUpdateAt()
return nil
}

if err := srv.Save(); err != nil {
return err
}
func (s *Service) updateAccServerExeIfDifferent(srv *instance.Instance) error {
if srv.Cfg.Md5Sum == s.config.AccServerMd5Sum {
return nil
}

if ok, err := srv.UpdateAccServerExe(s.config.AccServerFullPath()); err != nil {
return err
} else if ok {
srv.Cfg.SetUpdateAt()

if err := srv.Save(); err != nil {
return err
}
}

Expand Down Expand Up @@ -214,7 +226,7 @@ func (s *Service) Create(accConfig *instance.AccConfigFiles, accWebSettings inst
Live: instance.NewLiveState(),
}

if _, err := srv.UpdateAccServerExe(path.Join(s.config.AccServerPath, s.config.AccServerExe)); err != nil {
if _, err := srv.UpdateAccServerExe(s.config.AccServerFullPath()); err != nil {
return nil, err
}

Expand Down Expand Up @@ -261,3 +273,24 @@ func (s *Service) Duplicate(srcId string) (*instance.Instance, error) {

return s.Create(&cfg, srcSrv.Cfg.Settings)
}

func (s *Service) Start(id string) error {
srv, err := s.GetServerByID(id)
if err != nil {
return err
}

if err := s.GetAccServerExeMd5Sum(); err != nil {
return err
}

if err := s.updateAccServerExeIfDifferent(srv); err != nil {
return err
}

if err := srv.Start(); err != nil {
return err
}

return nil
}

0 comments on commit 3eb1791

Please sign in to comment.