Skip to content

Commit a0ec75c

Browse files
snorwinm-terra
andauthored
Bugfix/missed events (#10) (#11)
* Bugfix/missed events (#10) * go 21 upgrade * add watch on deleted files before starting the new process * add mutex to avoid race conditions * Fix 403 Forbidden in push to ghcr --------- Co-authored-by: Norwin Schnyder <[email protected]> * Remove mutex --------- Co-authored-by: Andy Baer <[email protected]>
1 parent e2b6e59 commit a0ec75c

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/snorwin/haproxy-reload-wrapper
22

3-
go 1.17
3+
go 1.21
44

5-
require github.com/fsnotify/fsnotify v1.6.0
5+
require github.com/fsnotify/fsnotify v1.7.0
66

7-
require golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
7+
require golang.org/x/sys v0.4.0 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
2-
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
3-
golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY=
4-
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1+
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
2+
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
3+
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
4+
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

main.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,31 @@ func main() {
6060
if !(isWrite(event) || isRemove(event) || isCreate(event)) {
6161
continue
6262
}
63+
6364
log.Notice(fmt.Sprintf("fs event for file %s : %v", cfgFile, event.Op))
6465

66+
// re-add watch if file was removed - config maps are updated by removing/adding a symlink
67+
if isRemove(event) {
68+
if err := fswatch.Add(cfgFile); err != nil {
69+
log.Alert(fmt.Sprintf("watch file failed : %v", err))
70+
} else {
71+
log.Notice(fmt.Sprintf("watch file : %s", cfgFile))
72+
}
73+
}
74+
6575
// create a new haproxy process which will replace the old one after it was successfully started
6676
tmp := exec.Command(executable, append([]string{"-x", utils.LookupHAProxySocketPath(), "-sf", strconv.Itoa(cmd.Process.Pid)}, os.Args[1:]...)...)
6777
tmp.Stdout = os.Stdout
6878
tmp.Stderr = os.Stderr
6979
tmp.Env = utils.LoadEnvFile()
80+
7081
if err := tmp.AsyncRun(); err != nil {
7182
log.Warning(err.Error())
7283
log.Warning("reload failed")
7384
continue
7485
}
75-
log.Notice(fmt.Sprintf("process %d started", tmp.Process.Pid))
7686

87+
log.Notice(fmt.Sprintf("process %d started", tmp.Process.Pid))
7788
select {
7889
case <-cmd.Terminated:
7990
// old haproxy terminated - successfully started a new process replacing the old one
@@ -85,16 +96,6 @@ func main() {
8596
log.Warning(fmt.Sprintf("process %d terminated unexpectedly : %s", tmp.Process.Pid, tmp.Status()))
8697
log.Warning("reload failed")
8798
}
88-
89-
// re-add watch if file was removed - config maps are updated by removing/adding a symlink
90-
if isRemove(event) {
91-
if err := fswatch.Add(cfgFile); err != nil {
92-
log.Alert(fmt.Sprintf("watch file failed : %v", err))
93-
continue
94-
}
95-
96-
log.Notice(fmt.Sprintf("watch file : %s", cfgFile))
97-
}
9899
case err := <-fswatch.Errors:
99100
// handle errors of fsnotify.Watcher
100101
log.Alert(err.Error())

0 commit comments

Comments
 (0)