update log rotator
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,6 +9,7 @@ Makefile
|
|||||||
autom4te.cache
|
autom4te.cache
|
||||||
*.deb
|
*.deb
|
||||||
*.tar.*
|
*.tar.*
|
||||||
|
*.service
|
||||||
tmp*
|
tmp*
|
||||||
DIST
|
DIST
|
||||||
TMP*
|
TMP*
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ type Config struct {
|
|||||||
RunPath string `json:"runfile" yaml:"runfile"`
|
RunPath string `json:"runfile" yaml:"runfile"`
|
||||||
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
|
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
|
||||||
LogLimit int64 `json:"logLimit" yaml:logLimit`
|
LogLimit int64 `json:"logLimit" yaml:logLimit`
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() (*Config, error) {
|
func NewConfig() (*Config, error) {
|
||||||
@@ -45,7 +44,7 @@ func NewConfig() (*Config, error) {
|
|||||||
Port: client.DefaultServicePort,
|
Port: client.DefaultServicePort,
|
||||||
},
|
},
|
||||||
AsDaemon: false,
|
AsDaemon: false,
|
||||||
LogLimit: 1024 * 1024 * 10, // 10 Mb
|
LogLimit: 1024 * 1024 * 10, // 10 Mb
|
||||||
}
|
}
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
const (
|
const (
|
||||||
confdirPath = "/home/ziggi/Projects/sys2agent/etc/minilb"
|
confdirPath = "/home/ziggi/Projects/gserver/etc/minilb"
|
||||||
rundirPath = "/home/ziggi/Projects/sys2agent/tmp/run"
|
rundirPath = "/home/ziggi/Projects/gserver/tmp/run"
|
||||||
logdirPath = "/home/ziggi/Projects/sys2agent/tmp/log"
|
logdirPath = "/home/ziggi/Projects/gserver/tmp/log"
|
||||||
datadirPath = "/home/ziggi/Projects/sys2agent/tmp/data"
|
datadirPath = "/home/ziggi/Projects/gserver/tmp/data"
|
||||||
packageVersion = "0.0.1"
|
packageVersion = "0.0.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -25,6 +28,10 @@ type Server struct {
|
|||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
x509cert []byte
|
x509cert []byte
|
||||||
x509key []byte
|
x509key []byte
|
||||||
|
logf *os.File
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() (*Server, error) {
|
func NewServer() (*Server, error) {
|
||||||
@@ -117,6 +124,8 @@ func (srv *Server) Run() error {
|
|||||||
}
|
}
|
||||||
srv.log.Debugf("Server configuration:\n%s\n", yamlConfig)
|
srv.log.Debugf("Server configuration:\n%s\n", yamlConfig)
|
||||||
|
|
||||||
|
srv.ctx, srv.cancel = context.WithCancel(context.Background())
|
||||||
|
|
||||||
currUser, err := user.Current()
|
currUser, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -141,7 +150,9 @@ func (srv *Server) Run() error {
|
|||||||
select {
|
select {
|
||||||
case signal = <-sigs:
|
case signal = <-sigs:
|
||||||
srv.log.Infof("Services stopped by signal: %v", signal)
|
srv.log.Infof("Services stopped by signal: %v", signal)
|
||||||
|
srv.cancel()
|
||||||
srv.svc.Stop()
|
srv.svc.Stop()
|
||||||
|
srv.wg.Wait()
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -155,7 +166,6 @@ func (srv *Server) PseudoFork() error {
|
|||||||
switch {
|
switch {
|
||||||
case !isChild:
|
case !isChild:
|
||||||
os.Setenv(keyEnv, "TRUE")
|
os.Setenv(keyEnv, "TRUE")
|
||||||
|
|
||||||
procAttr := syscall.ProcAttr{}
|
procAttr := syscall.ProcAttr{}
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -220,27 +230,7 @@ func (srv *Server) Daemonize() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Log file rotator
|
srv.logf = logFile
|
||||||
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()
|
|
||||||
|
|
||||||
// Write process ID
|
// Write process ID
|
||||||
rundir := filepath.Dir(srv.conf.RunPath)
|
rundir := filepath.Dir(srv.conf.RunPath)
|
||||||
err = os.MkdirAll(rundir, 0750)
|
err = os.MkdirAll(rundir, 0750)
|
||||||
@@ -260,3 +250,38 @@ func (srv *Server) Daemonize() error {
|
|||||||
}
|
}
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
interceptors := []grpc.UnaryServerInterceptor{
|
interceptors := []grpc.UnaryServerInterceptor{
|
||||||
svc.authInterceptor,
|
svc.authInterceptor,
|
||||||
svc.logInterceptor,
|
svc.logInterceptor,
|
||||||
}
|
}
|
||||||
gsrvOpts := []grpc.ServerOption{
|
gsrvOpts := []grpc.ServerOption{
|
||||||
grpc.Creds(tlsCredentials),
|
grpc.Creds(tlsCredentials),
|
||||||
grpc.ChainUnaryInterceptor(interceptors...),
|
grpc.ChainUnaryInterceptor(interceptors...),
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ Type=forking
|
|||||||
ExecStart=@srv_sbindir@/minilbd -asDaemon=true
|
ExecStart=@srv_sbindir@/minilbd -asDaemon=true
|
||||||
ExecReload=/bin/kill -HUP $MAINPID
|
ExecReload=/bin/kill -HUP $MAINPID
|
||||||
ExecRestart=/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]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Referer struct {
|
type Referer struct {
|
||||||
@@ -27,10 +27,10 @@ func NewReferer(hostname string) (*Referer, error) {
|
|||||||
urlobj.User = nil
|
urlobj.User = nil
|
||||||
}
|
}
|
||||||
ref.urlobj = urlobj
|
ref.urlobj = urlobj
|
||||||
if !strings.Contains(ref.urlobj.Host, ":") {
|
if !strings.Contains(ref.urlobj.Host, ":") {
|
||||||
portstr := strconv.FormatInt(int64(DefaultServicePort), 10)
|
portstr := strconv.FormatInt(int64(DefaultServicePort), 10)
|
||||||
ref.urlobj.Host = ref.urlobj.Host + ":" + portstr
|
ref.urlobj.Host = ref.urlobj.Host + ":" + portstr
|
||||||
}
|
}
|
||||||
return ref, err
|
return ref, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user