Skip to content

Allow manual specification of filewatcher behavior #29

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

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/localstack/awsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ func RunDNSRewriter(opts *LsOpts, ctx context.Context) {
log.Debugln("DNS server stopped")
}

func RunHotReloadingListener(server *CustomInteropServer, targetPaths []string, ctx context.Context) {
func RunHotReloadingListener(server *CustomInteropServer, targetPaths []string, ctx context.Context, fileWatcherStrategy string) {
if len(targetPaths) == 1 && targetPaths[0] == "" {
log.Debugln("Hot reloading disabled.")
return
}
defaultDebouncingDuration := 500 * time.Millisecond
log.Infoln("Hot reloading enabled, starting filewatcher.", targetPaths)
changeListener, err := NewChangeListener(defaultDebouncingDuration)
changeListener, err := NewChangeListener(defaultDebouncingDuration, fileWatcherStrategy)
if err != nil {
log.Errorln("Hot reloading disabled due to change listener error.", err)
return
Expand Down
20 changes: 17 additions & 3 deletions cmd/localstack/filenotify/filenotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@ func shouldUseEventWatcher() bool {
}

// New tries to use a fs-event watcher, and falls back to the poller if there is an error
func New(interval time.Duration) (FileWatcher, error) {
func New(interval time.Duration, fileWatcherStrategy string) (FileWatcher, error) {
if fileWatcherStrategy != "" {
log.Debugln("Forced usage of filewatcher strategy: ", fileWatcherStrategy)
if fileWatcherStrategy == "event" {
if watcher, err := NewEventWatcher(); err == nil {
return watcher, nil
} else {
log.Fatalln("Event based filewatcher is selected, but unable to start. Please try setting the filewatcher to polling. Error: ", err)
}
} else if fileWatcherStrategy == "polling" {
return NewPollingWatcher(interval), nil
} else {
log.Fatalf("Invalid filewatcher strategy %s. Only event and polling are allowed.\n", fileWatcherStrategy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to crash the init binary rather than just log here?
Currently, it just hangs until a timeout kills the container :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A fatal log should indeed crash the init binary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.
Then it's likely one of the error cases we do not (yet) handle actively in the init. Reporting back to LocalStack would be nice in the future to short-circuit the timeout (which is likely high when debugging).

}
}
if shouldUseEventWatcher() {
if watcher, err := NewEventWatcher(); err == nil {
log.Debugln("Using event based filewatcher")
log.Debugln("Using event based filewatcher (autodetected)")
return watcher, nil
}
}
log.Debugln("Using polling based filewatcher")
log.Debugln("Using polling based filewatcher (autodetected)")
return NewPollingWatcher(interval), nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/localstack/hotreloading.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type ChangeListener struct {
watchedFolders []string
}

func NewChangeListener(debouncingInterval time.Duration) (*ChangeListener, error) {
watcher, err := filenotify.New(200 * time.Millisecond)
func NewChangeListener(debouncingInterval time.Duration, fileWatcherStrategy string) (*ChangeListener, error) {
watcher, err := filenotify.New(200*time.Millisecond, fileWatcherStrategy)
if err != nil {
log.Errorln("Cannot create change listener due to filewatcher error.", err)
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion cmd/localstack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type LsOpts struct {
User string
CodeArchives string
HotReloadingPaths []string
FileWatcherStrategy string
EnableDnsServer string
LocalstackIP string
InitLogLevel string
Expand Down Expand Up @@ -50,6 +51,7 @@ func InitLsOpts() *LsOpts {
// optional or empty
CodeArchives: os.Getenv("LOCALSTACK_CODE_ARCHIVES"),
HotReloadingPaths: strings.Split(GetenvWithDefault("LOCALSTACK_HOT_RELOADING_PATHS", ""), ","),
FileWatcherStrategy: os.Getenv("LOCALSTACK_FILE_WATCHER_STRATEGY"),
EnableDnsServer: os.Getenv("LOCALSTACK_ENABLE_DNS_SERVER"),
EnableXRayTelemetry: os.Getenv("LOCALSTACK_ENABLE_XRAY_TELEMETRY"),
LocalstackIP: os.Getenv("LOCALSTACK_HOSTNAME"),
Expand Down Expand Up @@ -225,7 +227,7 @@ func main() {
if err != nil {
log.Fatalln(err)
}
go RunHotReloadingListener(interopServer, lsOpts.HotReloadingPaths, fileWatcherContext)
go RunHotReloadingListener(interopServer, lsOpts.HotReloadingPaths, fileWatcherContext, lsOpts.FileWatcherStrategy)

// start runtime init. It is important to start `InitHandler` synchronously because we need to ensure the
// notification channels and status fields are properly initialized before `AwaitInitialized`
Expand Down