update log rotator
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,6 +9,7 @@ Makefile
|
||||
autom4te.cache
|
||||
*.deb
|
||||
*.tar.*
|
||||
*.service
|
||||
tmp*
|
||||
DIST
|
||||
TMP*
|
||||
|
||||
@@ -36,7 +36,6 @@ type Config struct {
|
||||
RunPath string `json:"runfile" yaml:"runfile"`
|
||||
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
|
||||
LogLimit int64 `json:"logLimit" yaml:logLimit`
|
||||
|
||||
}
|
||||
|
||||
func NewConfig() (*Config, error) {
|
||||
@@ -45,7 +44,7 @@ func NewConfig() (*Config, error) {
|
||||
Port: client.DefaultServicePort,
|
||||
},
|
||||
AsDaemon: false,
|
||||
LogLimit: 1024 * 1024 * 10, // 10 Mb
|
||||
LogLimit: 1024 * 1024 * 10, // 10 Mb
|
||||
}
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package config
|
||||
|
||||
const (
|
||||
confdirPath = "/home/ziggi/Projects/sys2agent/etc/minilb"
|
||||
rundirPath = "/home/ziggi/Projects/sys2agent/tmp/run"
|
||||
logdirPath = "/home/ziggi/Projects/sys2agent/tmp/log"
|
||||
datadirPath = "/home/ziggi/Projects/sys2agent/tmp/data"
|
||||
confdirPath = "/home/ziggi/Projects/gserver/etc/minilb"
|
||||
rundirPath = "/home/ziggi/Projects/gserver/tmp/run"
|
||||
logdirPath = "/home/ziggi/Projects/gserver/tmp/log"
|
||||
datadirPath = "/home/ziggi/Projects/gserver/tmp/data"
|
||||
packageVersion = "0.0.1"
|
||||
)
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -80,10 +80,10 @@ func (svc *Service) Run() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
interceptors := []grpc.UnaryServerInterceptor{
|
||||
svc.authInterceptor,
|
||||
svc.logInterceptor,
|
||||
}
|
||||
interceptors := []grpc.UnaryServerInterceptor{
|
||||
svc.authInterceptor,
|
||||
svc.logInterceptor,
|
||||
}
|
||||
gsrvOpts := []grpc.ServerOption{
|
||||
grpc.Creds(tlsCredentials),
|
||||
grpc.ChainUnaryInterceptor(interceptors...),
|
||||
|
||||
@@ -6,7 +6,7 @@ Type=forking
|
||||
ExecStart=@srv_sbindir@/minilbd -asDaemon=true
|
||||
ExecReload=/bin/kill -HUP $MAINPID
|
||||
ExecRestart=/bin/kill -HUP $MAINPID
|
||||
ExecStartPre=/usr/bin/install -d -o root -g root /home/ziggi/Projects/sys2agent/tmp/run /home/ziggi/Projects/sys2agent/tmp/log
|
||||
ExecStartPre=/usr/bin/install -d -o root -g root /home/ziggi/Projects/gserver/tmp/run /home/ziggi/Projects/gserver/tmp/log
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
@@ -2,8 +2,8 @@ package client
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Referer struct {
|
||||
@@ -27,10 +27,10 @@ func NewReferer(hostname string) (*Referer, error) {
|
||||
urlobj.User = nil
|
||||
}
|
||||
ref.urlobj = urlobj
|
||||
if !strings.Contains(ref.urlobj.Host, ":") {
|
||||
portstr := strconv.FormatInt(int64(DefaultServicePort), 10)
|
||||
ref.urlobj.Host = ref.urlobj.Host + ":" + portstr
|
||||
}
|
||||
if !strings.Contains(ref.urlobj.Host, ":") {
|
||||
portstr := strconv.FormatInt(int64(DefaultServicePort), 10)
|
||||
ref.urlobj.Host = ref.urlobj.Host + ":" + portstr
|
||||
}
|
||||
return ref, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user