update log rotator
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -25,6 +28,10 @@ type Server struct {
|
||||
log *logger.Logger
|
||||
x509cert []byte
|
||||
x509key []byte
|
||||
logf *os.File
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func NewServer() (*Server, error) {
|
||||
@@ -117,6 +124,8 @@ func (srv *Server) Run() error {
|
||||
}
|
||||
srv.log.Debugf("Server configuration:\n%s\n", yamlConfig)
|
||||
|
||||
srv.ctx, srv.cancel = context.WithCancel(context.Background())
|
||||
|
||||
currUser, err := user.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -141,7 +150,9 @@ func (srv *Server) Run() error {
|
||||
select {
|
||||
case signal = <-sigs:
|
||||
srv.log.Infof("Services stopped by signal: %v", signal)
|
||||
srv.cancel()
|
||||
srv.svc.Stop()
|
||||
srv.wg.Wait()
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -155,7 +166,6 @@ func (srv *Server) PseudoFork() error {
|
||||
switch {
|
||||
case !isChild:
|
||||
os.Setenv(keyEnv, "TRUE")
|
||||
|
||||
procAttr := syscall.ProcAttr{}
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
@@ -220,27 +230,7 @@ func (srv *Server) Daemonize() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Log file rotator
|
||||
logFunc := func() {
|
||||
for {
|
||||
stat, err := os.Stat(srv.conf.LogPath)
|
||||
if err == nil && stat.Size() > srv.conf.LogLimit {
|
||||
os.Rename(srv.conf.LogPath+".2", srv.conf.LogPath+".3")
|
||||
os.Rename(srv.conf.LogPath+".1", srv.conf.LogPath+".2")
|
||||
os.Rename(srv.conf.LogPath, srv.conf.LogPath+".1")
|
||||
prevLogFile := logFile
|
||||
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()))
|
||||
prevLogFile.Close()
|
||||
}
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
go logFunc()
|
||||
|
||||
srv.logf = logFile
|
||||
// Write process ID
|
||||
rundir := filepath.Dir(srv.conf.RunPath)
|
||||
err = os.MkdirAll(rundir, 0750)
|
||||
@@ -260,3 +250,38 @@ func (srv *Server) Daemonize() error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (srv *Server) Rotator() {
|
||||
srv.wg.Add(1)
|
||||
logFunc := func() {
|
||||
for {
|
||||
select {
|
||||
case <-srv.ctx.Done():
|
||||
srv.wg.Done()
|
||||
srv.log.Infof("Log file rotator done")
|
||||
return
|
||||
default:
|
||||
}
|
||||
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(3 * time.Second)
|
||||
}
|
||||
}
|
||||
go logFunc()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user