mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
hotfix: fix log rotate (#286)
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon"
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/action"
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/util"
|
||||
)
|
||||
|
||||
@@ -37,7 +38,7 @@ func CmdDaemon(_ cmdutil.Factory) *cobra.Command {
|
||||
} else {
|
||||
go util.StartupPProf(config.PProfPort)
|
||||
}
|
||||
return nil
|
||||
return initLogfile(action.GetDaemonLogPath())
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
defer opt.Stop()
|
||||
@@ -69,3 +70,17 @@ func CmdDaemon(_ cmdutil.Factory) *cobra.Command {
|
||||
cmd.Flags().BoolVar(&opt.IsSudo, "sudo", false, "is sudo or not")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func initLogfile(path string) error {
|
||||
_, err := os.Lstat(path)
|
||||
if os.IsNotExist(err) {
|
||||
var f *os.File
|
||||
f, err = os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = f.Close()
|
||||
return os.Chmod(path, 0644)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -56,19 +56,7 @@ func (o *SvrOption) Start(ctx context.Context) error {
|
||||
klog.LogToStderr(false)
|
||||
rest.SetDefaultWarningHandler(rest.NoWarnings{})
|
||||
// every day 00:00:00 rotate log
|
||||
if !o.IsSudo {
|
||||
go func() {
|
||||
for {
|
||||
nowTime := time.Now()
|
||||
nowTimeStr := nowTime.Format("2006-01-02")
|
||||
t2, _ := time.ParseInLocation("2006-01-02", nowTimeStr, time.Local)
|
||||
next := t2.AddDate(0, 0, 1)
|
||||
after := next.UnixNano() - nowTime.UnixNano() - 1
|
||||
<-time.After(time.Duration(after) * time.Nanosecond)
|
||||
_ = l.Rotate()
|
||||
}
|
||||
}()
|
||||
}
|
||||
go rotateLog(l, o.IsSudo)
|
||||
|
||||
sockPath := config.GetSockPath(o.IsSudo)
|
||||
err := os.Remove(sockPath)
|
||||
@@ -124,7 +112,6 @@ func (o *SvrOption) Start(ctx context.Context) error {
|
||||
o.svr.Cancel = nil
|
||||
_ = o.svr.Quit(&rpc.QuitRequest{}, nil)
|
||||
_ = downgradingServer.Close()
|
||||
_ = l.Rotate()
|
||||
_ = l.Close()
|
||||
}
|
||||
o.svr = &action.Server{Cancel: cancel, IsSudo: o.IsSudo, GetClient: GetClient, LogFile: l, ID: o.ID}
|
||||
@@ -228,3 +215,25 @@ func writePIDToFile(isSudo bool) error {
|
||||
err = os.Chmod(pidPath, 0644)
|
||||
return err
|
||||
}
|
||||
|
||||
// let daemon process to Rotate log. create new log file
|
||||
// sudo daemon process then use new log file
|
||||
func rotateLog(l *lumberjack.Logger, isSudo bool) {
|
||||
sec := time.Duration(0)
|
||||
if isSudo {
|
||||
sec = time.Second
|
||||
}
|
||||
for {
|
||||
nowTime := time.Now()
|
||||
nowTimeStr := nowTime.Format("2006-01-02")
|
||||
t2, _ := time.ParseInLocation("2006-01-02", nowTimeStr, time.Local)
|
||||
next := t2.AddDate(0, 0, 1).Add(sec)
|
||||
after := next.UnixNano() - nowTime.UnixNano()
|
||||
<-time.After(time.Duration(after) * time.Nanosecond)
|
||||
if isSudo {
|
||||
_ = l.Close()
|
||||
} else {
|
||||
_ = l.Rotate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user