1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-11-24 08:52:25 +02:00
oauth2-proxy/watcher.go
Mitsuo Heijo 3fa42edb73
Fix import path for v7 (#800)
* fix import path for v7

find ./ -name "*.go" | xargs sed -i -e 's|"github.com/oauth2-proxy/oauth2-proxy|"github.com/oauth2-proxy/oauth2-proxy/v7|'

* fix module path

* go mod tidy

* fix installation docs

* update CHANGELOG

* Update CHANGELOG.md

Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk>

Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk>
2020-09-29 17:44:42 +01:00

81 lines
2.3 KiB
Go

// +build go1.3,!plan9,!solaris
package main
import (
"os"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
)
// WaitForReplacement waits for a file to exist on disk and then starts a watch
// for the file
func WaitForReplacement(filename string, op fsnotify.Op,
watcher *fsnotify.Watcher) {
const sleepInterval = 50 * time.Millisecond
// Avoid a race when fsnofity.Remove is preceded by fsnotify.Chmod.
if op&fsnotify.Chmod != 0 {
time.Sleep(sleepInterval)
}
for {
if _, err := os.Stat(filename); err == nil {
if err := watcher.Add(filename); err == nil {
logger.Printf("watching resumed for %s", filename)
return
}
}
time.Sleep(sleepInterval)
}
}
// WatchForUpdates performs an action every time a file on disk is updated
func WatchForUpdates(filename string, done <-chan bool, action func()) {
filename = filepath.Clean(filename)
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Fatal("failed to create watcher for ", filename, ": ", err)
}
go func() {
defer func(w *fsnotify.Watcher) {
cerr := w.Close()
if cerr != nil {
logger.Fatalf("error closing watcher: %v", err)
}
}(watcher)
for {
select {
case <-done:
logger.Printf("Shutting down watcher for: %s", filename)
return
case event := <-watcher.Events:
// On Arch Linux, it appears Chmod events precede Remove events,
// which causes a race between action() and the coming Remove event.
// If the Remove wins, the action() (which calls
// UserMap.LoadAuthenticatedEmailsFile()) crashes when the file
// can't be opened.
if event.Op&(fsnotify.Remove|fsnotify.Rename|fsnotify.Chmod) != 0 {
logger.Printf("watching interrupted on event: %s", event)
err = watcher.Remove(filename)
if err != nil {
logger.Printf("error removing watcher on %s: %v", filename, err)
}
WaitForReplacement(filename, event.Op, watcher)
}
logger.Printf("reloading after event: %s", event)
action()
case err = <-watcher.Errors:
logger.Errorf("error watching %s: %s", filename, err)
}
}
}()
if err = watcher.Add(filename); err != nil {
logger.Fatal("failed to add ", filename, " to watcher: ", err)
}
logger.Printf("watching %s for updates", filename)
}