watchers 2

This commit is contained in:
Sky Johnson 2025-03-07 10:51:14 -06:00
parent 4a17388d83
commit 4f02f0e5bf
3 changed files with 38 additions and 20 deletions

View File

@ -21,7 +21,7 @@ func LogRequest(log *logger.Logger, statusCode int, r *http.Request, duration ti
statusColor := getStatusColor(statusCode) statusColor := getStatusColor(statusCode)
// Use the logger's raw message writer to bypass the standard format // Use the logger's raw message writer to bypass the standard format
log.LogRaw("%s [%s%d%s] %s %s (%v)", log.LogRaw("%s [ %s%d%s] %s %s (%v)",
time.Now().Format(log.TimeFormat()), time.Now().Format(log.TimeFormat()),
statusColor, statusCode, colorReset, statusColor, statusCode, colorReset,
r.Method, r.URL.Path, duration) r.Method, r.URL.Path, duration)

View File

@ -34,11 +34,11 @@ var levelProps = map[int]struct {
tag string tag string
color string color string
}{ }{
LevelDebug: {"DBG", colorCyan}, LevelDebug: {" DBG", colorCyan},
LevelInfo: {"INF", colorBlue}, LevelInfo: {"INFO", colorBlue},
LevelWarning: {"WRN", colorYellow}, LevelWarning: {"WARN", colorYellow},
LevelError: {"ERR", colorRed}, LevelError: {" ERR", colorRed},
LevelFatal: {"FTL", colorPurple}, LevelFatal: {"FATL", colorPurple},
} }
// Time format for log messages // Time format for log messages

View File

@ -1,6 +1,7 @@
package watchers package watchers
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
@ -120,9 +121,9 @@ func WatchDirectory(config WatcherConfig) (*Watcher, error) {
go w.debounceLoop() go w.debounceLoop()
if config.Adaptive { if config.Adaptive {
config.Log.Debug("Started watching directory with adaptive polling: %s", config.Dir) w.logDebug("Started watching with adaptive polling")
} else { } else {
config.Log.Debug("Started watching directory: %s", config.Dir) w.logDebug("Started watching with fixed polling interval: %v", pollInterval)
} }
return w, nil return w, nil
@ -147,7 +148,7 @@ func (w *Watcher) watchLoop() {
case <-ticker.C: case <-ticker.C:
changed, err := w.checkForChanges() changed, err := w.checkForChanges()
if err != nil { if err != nil {
w.log.Error("Error checking for changes: %v", err) w.logError("Error checking for changes: %v", err)
continue continue
} }
@ -155,11 +156,10 @@ func (w *Watcher) watchLoop() {
// Update last change time // Update last change time
w.lastChangeTime = time.Now() w.lastChangeTime = time.Now()
// Reset to base polling interval if we were using extended intervals
if w.adaptive && w.pollInterval > w.basePollInterval { if w.adaptive && w.pollInterval > w.basePollInterval {
w.pollInterval = w.basePollInterval w.pollInterval = w.basePollInterval
ticker.Reset(w.pollInterval) ticker.Reset(w.pollInterval)
w.log.Debug("Reset to base polling interval: %v", w.pollInterval) w.logDebug("Reset to base polling interval: %v", w.pollInterval)
} }
// Try to send a change notification, non-blocking // Try to send a change notification, non-blocking
@ -176,13 +176,13 @@ func (w *Watcher) watchLoop() {
// First extension // First extension
w.pollInterval = extendedPollInterval w.pollInterval = extendedPollInterval
ticker.Reset(w.pollInterval) ticker.Reset(w.pollInterval)
w.log.Debug("Extended polling interval to %v after %v of inactivity", w.logDebug("Extended polling interval to: %v after %v of inactivity",
w.pollInterval, inactiveDuration.Round(time.Second)) w.pollInterval, inactiveDuration.Round(time.Second))
} else if w.pollInterval == extendedPollInterval && inactiveDuration > secondExtendThreshold { } else if w.pollInterval == extendedPollInterval && inactiveDuration > secondExtendThreshold {
// Second extension // Second extension
w.pollInterval = maxPollInterval w.pollInterval = maxPollInterval
ticker.Reset(w.pollInterval) ticker.Reset(w.pollInterval)
w.log.Debug("Extended polling interval to %v after %v of inactivity", w.logDebug("Extended polling interval to: %v after %v of inactivity",
w.pollInterval, inactiveDuration.Round(time.Second)) w.pollInterval, inactiveDuration.Round(time.Second))
} }
} }
@ -209,7 +209,7 @@ func (w *Watcher) debounceLoop() {
// Start a new timer // Start a new timer
timer = time.AfterFunc(w.debounceTime, func() { timer = time.AfterFunc(w.debounceTime, func() {
if err := w.callback(); err != nil { if err := w.callback(); err != nil {
w.log.Error("Refresh callback error: %v", err) w.logError("Refresh callback error: %v", err)
} }
}) })
@ -229,7 +229,7 @@ func (w *Watcher) scanDirectory() error {
return filepath.Walk(w.dir, func(path string, info os.FileInfo, err error) error { return filepath.Walk(w.dir, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
w.log.Warning("Error accessing path %s: %v", path, err) w.logWarning("Error accessing path %s: %v", path, err)
return nil // Continue with other files return nil // Continue with other files
} }
@ -248,7 +248,25 @@ func (w *Watcher) scanDirectory() error {
}) })
} }
// checkForChanges compares current file state with the last scan // logDebug logs a debug message with the watcher's directory prefix
func (w *Watcher) logDebug(format string, args ...any) {
w.log.Debug("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...))
}
// logInfo logs an info message with the watcher's directory prefix
func (w *Watcher) logInfo(format string, args ...any) {
w.log.Info("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...))
}
// logWarning logs a warning message with the watcher's directory prefix
func (w *Watcher) logWarning(format string, args ...any) {
w.log.Warning("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...))
}
// logError logs an error message with the watcher's directory prefix
func (w *Watcher) logError(format string, args ...any) {
w.log.Error("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...))
}
func (w *Watcher) checkForChanges() (bool, error) { func (w *Watcher) checkForChanges() (bool, error) {
// Get current state // Get current state
currentFiles := make(map[string]FileInfo) currentFiles := make(map[string]FileInfo)
@ -284,7 +302,7 @@ func (w *Watcher) checkForChanges() (bool, error) {
// Check for different file count (quick check) // Check for different file count (quick check)
if len(currentFiles) != len(previousFiles) { if len(currentFiles) != len(previousFiles) {
w.log.Debug("File count changed: %d -> %d", len(previousFiles), len(currentFiles)) w.logDebug("File count changed: %d -> %d", len(previousFiles), len(currentFiles))
w.updateFiles(currentFiles) w.updateFiles(currentFiles)
return true, nil return true, nil
} }
@ -294,14 +312,14 @@ func (w *Watcher) checkForChanges() (bool, error) {
prevInfo, exists := previousFiles[path] prevInfo, exists := previousFiles[path]
if !exists { if !exists {
// New file // New file
w.log.Debug("New file detected: %s", path) w.logDebug("New file detected: %s", path)
w.updateFiles(currentFiles) w.updateFiles(currentFiles)
return true, nil return true, nil
} }
if currentInfo.ModTime != prevInfo.ModTime || currentInfo.Size != prevInfo.Size { if currentInfo.ModTime != prevInfo.ModTime || currentInfo.Size != prevInfo.Size {
// File modified // File modified
w.log.Debug("File modified: %s", path) w.logDebug("File modified: %s", path)
w.updateFiles(currentFiles) w.updateFiles(currentFiles)
return true, nil return true, nil
} }
@ -311,7 +329,7 @@ func (w *Watcher) checkForChanges() (bool, error) {
for path := range previousFiles { for path := range previousFiles {
if _, exists := currentFiles[path]; !exists { if _, exists := currentFiles[path]; !exists {
// File deleted // File deleted
w.log.Debug("File deleted: %s", path) w.logDebug("File deleted: %s", path)
w.updateFiles(currentFiles) w.updateFiles(currentFiles)
return true, nil return true, nil
} }