-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: polling based execution config watcher #1671
base: main
Are you sure you want to change the base?
Changes from all commits
5f74dde
2c859bd
45a2031
dbdad0d
9e99301
4e88df7
3fe32ec
d211a44
dfe6113
12b63c1
7416557
bb2b443
183d5b4
5bf593b
d42cd76
dd9a5c0
239915d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package watcher | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
type SimpleWatcherOptions struct { | ||
Interval time.Duration | ||
Logger *zap.Logger | ||
Path string | ||
Callback func() | ||
} | ||
|
||
func LogSimpleWatch(ctx context.Context, options SimpleWatcherOptions) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as we basically require all options to be set, would it make sense to make them private and have a function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, I could also split it into like |
||
if err := SimpleWatch(ctx, options); err != nil { | ||
options.Logger.Error("Error watching file", zap.Error(err)) | ||
} | ||
} | ||
|
||
func SimpleWatch(ctx context.Context, options SimpleWatcherOptions) error { | ||
ticker := time.NewTicker(options.Interval) | ||
defer ticker.Stop() | ||
|
||
ll := options.Logger.With(zap.String("path", options.Path)) | ||
|
||
var prevModTime time.Time | ||
|
||
stat, err := os.Stat(options.Path) | ||
if err != nil { | ||
ll.Debug("Target file cannot be statted", zap.Error(err)) | ||
} else { | ||
prevModTime = stat.ModTime() | ||
} | ||
|
||
ll.Debug("Watching file for changes", zap.Time("initialModTime", prevModTime)) | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
stat, err := os.Stat(options.Path) | ||
if err != nil { | ||
ll.Debug("Target file cannot be statted", zap.Error(err)) | ||
|
||
// Reset the mod time so we catch any new file at the target path | ||
prevModTime = time.Time{} | ||
|
||
continue | ||
} | ||
|
||
ll.Debug("Checking file for changes", zap.Time("prev", prevModTime), zap.Time("mod", stat.ModTime())) | ||
|
||
if stat.ModTime().After(prevModTime) { | ||
prevModTime = stat.ModTime() | ||
options.Callback() | ||
} | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old watcher is unused now, correct? Any reason to keep unused code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Posterity I guess, but its broken in other ways anyway. I'll remove it