working commit

This commit is contained in:
2026-06-09 21:07:21 +02:00
parent 6e92720e69
commit f89340550f
+78 -2
View File
@@ -10,6 +10,8 @@ import (
"strconv" "strconv"
"syscall" "syscall"
"time" "time"
"context"
"sync"
"mbase/app/config" "mbase/app/config"
"mbase/app/maindb" "mbase/app/maindb"
@@ -39,12 +41,16 @@ type Server struct {
state descr.Server state descr.Server
sfile string sfile string
logf *os.File logf *os.File
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
} }
func NewServer() (*Server, error) { func NewServer() (*Server, error) {
var err error var err error
srv := &Server{} srv := &Server{}
srv.log = logger.NewLogger("server") srv.log = logger.NewLogger("server")
srv.ctx, srv.cancel = context.WithCancel(context.Background())
return srv, err return srv, err
} }
@@ -297,12 +303,42 @@ func (srv *Server) Run() error {
} }
srv.log.Debugf("Server configuration:\n%s\n", yamlConfig) srv.log.Debugf("Server configuration:\n%s\n", yamlConfig)
// Show current user if srv.conf.AsDaemon {
// Redirect stdin
nullFile, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
if err != nil {
return err
}
err = syscall.Dup2(int(nullFile.Fd()), int(os.Stdin.Fd()))
if err != nil {
return err
}
// Write process ID
pidFile, err := os.OpenFile(srv.conf.RunPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
return err
}
defer pidFile.Close()
currPid := os.Getpid()
_, err = pidFile.WriteString(strconv.Itoa(currPid))
if err != nil {
return err
}
// Start log rotator
srv.Rotator()
}
currUser, err := user.Current() currUser, err := user.Current()
if err != nil { if err != nil {
return err return err
} }
srv.log.Infof("Running server as user %s", currUser.Username) srv.log.Infof("Server started with user: %s", currUser.Username)
uidstr := strconv.FormatInt(int64(syscall.Geteuid()), 10)
usr, err := user.LookupId(uidstr)
if err != nil {
return err
}
srv.log.Infof("Server run with user: %s", usr.Username)
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
done := make(chan error, 1) done := make(chan error, 1)
@@ -421,3 +457,43 @@ func (srv *Server) Daemonize() error {
} }
return err return err
} }
func (srv *Server) Rotator() {
srv.wg.Add(1)
var counter uint64
logFunc := func() {
for {
counter += 1
select {
case <-srv.ctx.Done():
srv.wg.Done()
srv.log.Infof("Log file rotator done")
return
default:
}
if (counter % 60) == 1 {
stat, err := srv.logf.Stat()
if err == nil && stat.Size() > srv.conf.LogLimit {
srv.log.Infof("Rotate log file")
countFiles := 3
for i := 1; i < countFiles; i++ {
nextName := fmt.Sprintf("%s.%d", srv.conf.LogPath, i+1)
prevName := fmt.Sprintf("%s.%d", srv.conf.LogPath, i)
os.Rename(prevName, nextName)
}
os.Rename(srv.conf.LogPath, srv.conf.LogPath+".1")
logFile, err := os.OpenFile(srv.conf.LogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err == nil {
syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
srv.logf.Close()
srv.logf = logFile
}
}
}
time.Sleep(1 * time.Second)
}
}
go logFunc()
}