added simple log rotator

This commit is contained in:
2026-03-24 10:51:00 +02:00
parent b443292720
commit 6f66d14329
7 changed files with 44 additions and 10 deletions

View File

@@ -102,7 +102,7 @@ DIST_COMMON = $(srcdir)/Makefile.am $(top_srcdir)/configure \
am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log \
configure.lineno config.status.lineno
mkinstalldirs = $(install_sh) -d
CONFIG_CLEAN_FILES = app/config/path.go initrc/minilbd.service \
CONFIG_CLEAN_FILES = app/config/variant.go initrc/minilbd.service \
debian/control debian/changelog
CONFIG_CLEAN_VPATH_FILES =
am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(sbindir)"
@@ -173,7 +173,7 @@ am__define_uniq_tagged_files = \
done | $(am__uniquify_input)`
AM_RECURSIVE_TARGETS = cscope
am__DIST_COMMON = $(srcdir)/Makefile.in \
$(top_srcdir)/app/config/path.go.in \
$(top_srcdir)/app/config/variant.go.in \
$(top_srcdir)/debian/changelog.in \
$(top_srcdir)/debian/control.in \
$(top_srcdir)/initrc/minilbd.service.in README.md config.guess \
@@ -405,7 +405,7 @@ $(top_srcdir)/configure: $(am__configure_deps)
$(ACLOCAL_M4): $(am__aclocal_m4_deps)
$(am__cd) $(srcdir) && $(ACLOCAL) $(ACLOCAL_AMFLAGS)
$(am__aclocal_m4_deps):
app/config/path.go: $(top_builddir)/config.status $(top_srcdir)/app/config/path.go.in
app/config/variant.go: $(top_builddir)/config.status $(top_srcdir)/app/config/variant.go.in
cd $(top_builddir) && $(SHELL) ./config.status $@
initrc/minilbd.service: $(top_builddir)/config.status $(top_srcdir)/initrc/minilbd.service.in
cd $(top_builddir) && $(SHELL) ./config.status $@

View File

@@ -35,6 +35,8 @@ type Config struct {
LogPath string `json:"logfile" yaml:"logfile"`
RunPath string `json:"runfile" yaml:"runfile"`
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
LogLimit int64 `json:"logLimit" yaml:logLimit`
}
func NewConfig() (*Config, error) {
@@ -43,6 +45,7 @@ func NewConfig() (*Config, error) {
Port: client.DefaultServicePort,
},
AsDaemon: false,
LogLimit: 1024 * 1024 * 10, // 10 Mb
}
hostname, err := os.Hostname()
if err != nil {

10
app/config/variant.go Normal file
View File

@@ -0,0 +1,10 @@
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"
packageVersion = "0.0.1"
)

View File

@@ -3,11 +3,11 @@ package server
import (
"os"
"os/signal"
"syscall"
"os/user"
"path/filepath"
"strconv"
"syscall"
"time"
"helmet/app/config"
"helmet/app/handler"
@@ -88,13 +88,13 @@ func (srv *Server) Build() error {
if err != nil {
return err
}
// Create ghandler
// Create handler
handlerConfig := &handler.HandlerConfig{
Operator: srv.oper,
}
srv.hand = handler.NewHandler(handlerConfig)
// Create gservice
// Create service
serviceConfig := &service.ServiceConfig{
PortNum: srv.conf.Service.Port,
Hostname: srv.conf.Hostname,
@@ -220,6 +220,27 @@ 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()
// Write process ID
rundir := filepath.Dir(srv.conf.RunPath)
err = os.MkdirAll(rundir, 0750)

4
configure vendored
View File

@@ -3864,7 +3864,7 @@ printf "%s\n" "$as_me: srv_datadir set as ${SRV_DATADIR}" >&6;}
ac_config_files="$ac_config_files Makefile app/config/path.go initrc/minilbd.service debian/control debian/changelog"
ac_config_files="$ac_config_files Makefile app/config/variant.go initrc/minilbd.service debian/control debian/changelog"
cat >confcache <<\_ACEOF
# This file is a shell script that caches the results of configure
@@ -4616,7 +4616,7 @@ for ac_config_target in $ac_config_targets
do
case $ac_config_target in
"Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;;
"app/config/path.go") CONFIG_FILES="$CONFIG_FILES app/config/path.go" ;;
"app/config/variant.go") CONFIG_FILES="$CONFIG_FILES app/config/variant.go" ;;
"initrc/minilbd.service") CONFIG_FILES="$CONFIG_FILES initrc/minilbd.service" ;;
"debian/control") CONFIG_FILES="$CONFIG_FILES debian/control" ;;
"debian/changelog") CONFIG_FILES="$CONFIG_FILES debian/changelog" ;;

View File

@@ -243,7 +243,7 @@ dnl ----------------------------------------------------------------------------
AC_CONFIG_FILES([
Makefile
app/config/path.go
app/config/variant.go
initrc/minilbd.service
debian/control
debian/changelog