Skip to content

Commit

Permalink
Merge pull request #1 from mittwald/feat/inotify-support
Browse files Browse the repository at this point in the history
Add inotify support
  • Loading branch information
maxihafer authored Sep 11, 2024
2 parents 32e229c + 32075dc commit a849a80
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
supercronic
vendor
dist/*
.idea
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.0

require (
github.com/evalphobia/logrus_sentry v0.8.2
github.com/fsnotify/fsnotify v1.7.0
github.com/prometheus/client_golang v1.20.2
github.com/ramr/go-reaper v0.2.1
github.com/sirupsen/logrus v1.9.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evalphobia/logrus_sentry v0.8.2 h1:dotxHq+YLZsT1Bb45bB5UQbfCh3gM/nFFetyN46VoDQ=
github.com/evalphobia/logrus_sentry v0.8.2/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand Down
61 changes: 58 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/aptible/supercronic/log/hook"
"github.com/aptible/supercronic/prometheus_metrics"
"github.com/evalphobia/logrus_sentry"
"github.com/fsnotify/fsnotify"
reaper "github.com/ramr/go-reaper"
"github.com/sirupsen/logrus"
)
Expand All @@ -29,6 +30,7 @@ func main() {
quiet := flag.Bool("quiet", false, "do not log informational messages (takes precedence over debug)")
json := flag.Bool("json", false, "enable JSON logging")
test := flag.Bool("test", false, "test crontab (does not run jobs)")
inotify := flag.Bool("inotify", false, "use inotify to detect crontab file changes")
prometheusListen := flag.String(
"prometheus-listen-address",
"",
Expand Down Expand Up @@ -102,6 +104,24 @@ func main() {

crontabFileName := flag.Args()[0]

var watcher *fsnotify.Watcher
if *inotify {
logrus.Info("using inotify to detect crontab file changes")
var err error
watcher, err = fsnotify.NewWatcher()
if err != nil {
logrus.Fatal(err)
return
}
defer watcher.Close()

logrus.Infof("adding file watch for '%s'", crontabFileName)
if err := watcher.Add(crontabFileName); err != nil {
logrus.Fatal(err)
return
}
}

var sentryHook *logrus_sentry.SentryHook
if sentryDsn != "" {
sentryLevels := []logrus.Level{
Expand Down Expand Up @@ -149,6 +169,44 @@ func main() {
go reaper.Reap()
// _ = reaper.Reap

termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR2)

if *inotify {
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
logrus.Debugf("event: %v, watch-list: %v", event, watcher.WatchList())

switch event.Op {
case event.Op & fsnotify.Write:
logrus.Debug("watched file changed")
termChan <- syscall.SIGUSR2

// workaround for k8s configmap and secret mounts
case event.Op & fsnotify.Remove:
logrus.Debug("watched file changed")
if err := watcher.Add(event.Name); err != nil {
logrus.Fatal(err)
return
}
termChan <- syscall.SIGUSR2
}

case err, ok := <-watcher.Errors:
if !ok {
return
}
logrus.Error("error:", err)
}
}
}()
}

for {
promMetrics.Reset()

Expand Down Expand Up @@ -179,9 +237,6 @@ func main() {
cron.StartJob(&wg, tab.Context, job, exitCtx, cronLogger, *overlapping, *passthroughLogs, &promMetrics)
}

termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR2)

termSig := <-termChan

if termSig == syscall.SIGUSR2 {
Expand Down

0 comments on commit a849a80

Please sign in to comment.