Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support error log rotate and notification log rotate #649

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading