Skip to content

Commit

Permalink
Support error log rotate and notification log rotate (#649)
Browse files Browse the repository at this point in the history
* Support error log rotate and notification log rotate

* Add comment

* Modify log name

---------

Co-authored-by: 杨赫然 <[email protected]>
  • Loading branch information
feiniks and 杨赫然 authored Mar 12, 2024
1 parent ef239b8 commit bd6b87c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
14 changes: 12 additions & 2 deletions fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ func main() {
log.Fatalf("Failed to open or create error log file: %v", err)
}
syscall.Dup3(int(fp.Fd()), int(os.Stderr.Fd()), 0)
// We need to close the old fp, because it has beed duped.
fp.Close()
}

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

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

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

Expand All @@ -409,7 +411,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 +435,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
44 changes: 42 additions & 2 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 @@ -166,11 +168,12 @@ func main() {
}

if logFile == "" {
absLogFile = filepath.Join(configDir, "notification.log")
absLogFile = filepath.Join(configDir, "notification-server.log")
fp, err := os.OpenFile(absLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
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,23 +184,27 @@ func main() {
if err != nil {
log.Fatalf("Failed to open or create log file: %v", err)
}
logFp = fp
log.SetOutput(fp)
}

if absLogFile != "" {
errorLogFile := filepath.Join(filepath.Dir(absLogFile), "notification_server_error.log")
errorLogFile := filepath.Join(filepath.Dir(absLogFile), "notification-server-error.log")
fp, err := os.OpenFile(errorLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
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 bd6b87c

Please sign in to comment.