Skip to content

Commit

Permalink
feat: Add fsync to log file close
Browse files Browse the repository at this point in the history
  • Loading branch information
devanbenz committed Dec 27, 2024
1 parent 93288a9 commit 642aec4
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions query/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strconv"
"sync"
"sync/atomic"
"syscall"
"time"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -292,15 +291,13 @@ func (e *Executor) WithLogger(log *zap.Logger) {
e.TaskManager.Logger = e.Logger
}

func initQueryLogWriter(e *Executor, path string) (uintptr, error) {
func initQueryLogWriter(e *Executor, path string) (*os.File, error) {
logFile, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
e.Logger.Error("failed to open log file", zap.Error(err))
return 0, err
return nil, err
}

fd := logFile.Fd()

// If existing logger is a noop then we will just want to log to the provided file
if e.Logger == zap.NewNop() {
encoderConfig := zap.NewProductionEncoderConfig()
Expand All @@ -313,7 +310,7 @@ func initQueryLogWriter(e *Executor, path string) (uintptr, error) {

e.Logger = zap.New(core).With(zap.String("service", "query"))
e.TaskManager.Logger = e.Logger
return fd, nil
return logFile, nil
}

// If the existing logger exists we just need to register the file to it in order to write to as well
Expand All @@ -331,14 +328,14 @@ func initQueryLogWriter(e *Executor, path string) (uintptr, error) {
e.Logger = zap.New(newCore)
e.TaskManager.Logger = e.Logger

return fd, nil
return logFile, nil
}

func (e *Executor) WithLogWriter(path string) {
var fd uintptr
var file *os.File
var err error

fd, err = initQueryLogWriter(e, path)
file, err = initQueryLogWriter(e, path)
if err != nil {
e.Logger.Error("failed to open log file", zap.Error(err))
return
Expand Down Expand Up @@ -367,13 +364,17 @@ func (e *Executor) WithLogWriter(path string) {
}
e.Logger.Debug("event", zap.String("event", event.Name))
if event.Op == fsnotify.Remove || event.Op == fsnotify.Rename {
if err := syscall.Close(int(fd)); err != nil {
if err := file.Sync(); err != nil {
e.Logger.Error("failed to sync log file", zap.Error(err))
return
}
if err := file.Close(); err != nil {
e.Logger.Error("failed to close log file", zap.Error(err))
return
}

e.Logger.Debug("creating a new query log file; registered file was altered.", zap.String("event", event.Name), zap.String("path", path))
fd, err = initQueryLogWriter(e, path)
file, err = initQueryLogWriter(e, path)
if err != nil {
e.Logger.Error("failed to create log file watcher", zap.Error(err))
return
Expand Down

0 comments on commit 642aec4

Please sign in to comment.