Skip to content

Commit

Permalink
Support error log rotate and notification log rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Mar 6, 2024
1 parent b2bd7b7 commit b17cde8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
13 changes: 11 additions & 2 deletions fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ func main() {
log.Fatalf("Failed to open or create error log file: %v", err)
}
syscall.Dup3(int(fp.Fd()), int(os.Stderr.Fd()), 0)
fp.Close()
}

repomgr.Init(seafileDB)
Expand Down Expand Up @@ -390,7 +391,7 @@ func main() {
router := newHTTPRouter()

go handleSignals()
go handleUser1Singal()
go handleUser1Signal()

log.Print("Seafile file server started.")

Expand All @@ -409,7 +410,7 @@ func handleSignals() {
os.Exit(0)
}

func handleUser1Singal() {
func handleUser1Signal() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGUSR1)
<-signalChan
Expand All @@ -433,6 +434,14 @@ func logRotate() {
logFp.Close()
logFp = fp
}

errorLogFile := filepath.Join(filepath.Dir(absLogFile), "fileserver-error.log")
errFp, err := os.OpenFile(errorLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Failed to reopen fileserver error log: %v", err)
}
syscall.Dup3(int(errFp.Fd()), int(os.Stderr.Fd()), 0)
errFp.Close()
}

var rpcclient *searpc.Client
Expand Down
40 changes: 40 additions & 0 deletions notification-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
Expand All @@ -26,6 +27,7 @@ var logFile, absLogFile string
var privateKey string
var host string
var port uint32
var logFp *os.File

var ccnetDB *sql.DB

Expand Down Expand Up @@ -171,6 +173,7 @@ func main() {
if err != nil {
log.Fatalf("Failed to open or create log file: %v", err)
}
logFp = fp
log.SetOutput(fp)
} else if logFile != "-" {
absLogFile, err = filepath.Abs(logFile)
Expand All @@ -181,6 +184,7 @@ func main() {
if err != nil {
log.Fatalf("Failed to open or create log file: %v", err)
}
logFp = fp
log.SetOutput(fp)
}

Expand All @@ -191,13 +195,16 @@ func main() {
log.Fatalf("Failed to open or create error log file: %v", err)
}
syscall.Dup3(int(fp.Fd()), int(os.Stderr.Fd()), 0)
fp.Close()
}

loadNotifConfig()
loadCcnetDB()

Init()

go handleUser1Signal()

router := newHTTPRouter()

log.Info("notification server started.")
Expand All @@ -209,6 +216,39 @@ func main() {
}
}

func handleUser1Signal() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGUSR1)
<-signalChan

for {
select {
case <-signalChan:
logRotate()
}
}
}

func logRotate() {
fp, err := os.OpenFile(absLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Failed to reopen notification log: %v", err)
}
log.SetOutput(fp)
if logFp != nil {
logFp.Close()
logFp = fp
}

errorLogFile := filepath.Join(filepath.Dir(absLogFile), "notification_server_error.log")
errFp, err := os.OpenFile(errorLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Failed to reopen notification error log: %v", err)
}
syscall.Dup3(int(errFp.Fd()), int(os.Stderr.Fd()), 0)
errFp.Close()
}

func newHTTPRouter() *mux.Router {
r := mux.NewRouter()
r.Handle("/", appHandler(messageCB))
Expand Down

0 comments on commit b17cde8

Please sign in to comment.