Skip to content

Commit

Permalink
Allow manual specification of filewatcher behavior (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfangl authored Feb 6, 2024
1 parent e063557 commit ba28a02
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/localstack/awsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,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)
}
}
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 @@ -21,6 +21,7 @@ type LsOpts struct {
User string
CodeArchives string
HotReloadingPaths []string
FileWatcherStrategy string
EnableDnsServer string
LocalstackIP string
InitLogLevel string
Expand Down Expand Up @@ -52,6 +53,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 @@ -230,7 +232,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

0 comments on commit ba28a02

Please sign in to comment.