Skip to content

Commit ba28a02

Browse files
authored
Allow manual specification of filewatcher behavior (#29)
1 parent e063557 commit ba28a02

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

Diff for: cmd/localstack/awsutil.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ func RunDNSRewriter(opts *LsOpts, ctx context.Context) {
156156
log.Debugln("DNS server stopped")
157157
}
158158

159-
func RunHotReloadingListener(server *CustomInteropServer, targetPaths []string, ctx context.Context) {
159+
func RunHotReloadingListener(server *CustomInteropServer, targetPaths []string, ctx context.Context, fileWatcherStrategy string) {
160160
if len(targetPaths) == 1 && targetPaths[0] == "" {
161161
log.Debugln("Hot reloading disabled.")
162162
return
163163
}
164164
defaultDebouncingDuration := 500 * time.Millisecond
165165
log.Infoln("Hot reloading enabled, starting filewatcher.", targetPaths)
166-
changeListener, err := NewChangeListener(defaultDebouncingDuration)
166+
changeListener, err := NewChangeListener(defaultDebouncingDuration, fileWatcherStrategy)
167167
if err != nil {
168168
log.Errorln("Hot reloading disabled due to change listener error.", err)
169169
return

Diff for: cmd/localstack/filenotify/filenotify.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,28 @@ func shouldUseEventWatcher() bool {
3838
}
3939

4040
// New tries to use a fs-event watcher, and falls back to the poller if there is an error
41-
func New(interval time.Duration) (FileWatcher, error) {
41+
func New(interval time.Duration, fileWatcherStrategy string) (FileWatcher, error) {
42+
if fileWatcherStrategy != "" {
43+
log.Debugln("Forced usage of filewatcher strategy: ", fileWatcherStrategy)
44+
if fileWatcherStrategy == "event" {
45+
if watcher, err := NewEventWatcher(); err == nil {
46+
return watcher, nil
47+
} else {
48+
log.Fatalln("Event based filewatcher is selected, but unable to start. Please try setting the filewatcher to polling. Error: ", err)
49+
}
50+
} else if fileWatcherStrategy == "polling" {
51+
return NewPollingWatcher(interval), nil
52+
} else {
53+
log.Fatalf("Invalid filewatcher strategy %s. Only event and polling are allowed.\n", fileWatcherStrategy)
54+
}
55+
}
4256
if shouldUseEventWatcher() {
4357
if watcher, err := NewEventWatcher(); err == nil {
44-
log.Debugln("Using event based filewatcher")
58+
log.Debugln("Using event based filewatcher (autodetected)")
4559
return watcher, nil
4660
}
4761
}
48-
log.Debugln("Using polling based filewatcher")
62+
log.Debugln("Using polling based filewatcher (autodetected)")
4963
return NewPollingWatcher(interval), nil
5064
}
5165

Diff for: cmd/localstack/hotreloading.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ type ChangeListener struct {
1616
watchedFolders []string
1717
}
1818

19-
func NewChangeListener(debouncingInterval time.Duration) (*ChangeListener, error) {
20-
watcher, err := filenotify.New(200 * time.Millisecond)
19+
func NewChangeListener(debouncingInterval time.Duration, fileWatcherStrategy string) (*ChangeListener, error) {
20+
watcher, err := filenotify.New(200*time.Millisecond, fileWatcherStrategy)
2121
if err != nil {
2222
log.Errorln("Cannot create change listener due to filewatcher error.", err)
2323
return nil, err

Diff for: cmd/localstack/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type LsOpts struct {
2121
User string
2222
CodeArchives string
2323
HotReloadingPaths []string
24+
FileWatcherStrategy string
2425
EnableDnsServer string
2526
LocalstackIP string
2627
InitLogLevel string
@@ -52,6 +53,7 @@ func InitLsOpts() *LsOpts {
5253
// optional or empty
5354
CodeArchives: os.Getenv("LOCALSTACK_CODE_ARCHIVES"),
5455
HotReloadingPaths: strings.Split(GetenvWithDefault("LOCALSTACK_HOT_RELOADING_PATHS", ""), ","),
56+
FileWatcherStrategy: os.Getenv("LOCALSTACK_FILE_WATCHER_STRATEGY"),
5557
EnableDnsServer: os.Getenv("LOCALSTACK_ENABLE_DNS_SERVER"),
5658
EnableXRayTelemetry: os.Getenv("LOCALSTACK_ENABLE_XRAY_TELEMETRY"),
5759
LocalstackIP: os.Getenv("LOCALSTACK_HOSTNAME"),
@@ -230,7 +232,7 @@ func main() {
230232
if err != nil {
231233
log.Fatalln(err)
232234
}
233-
go RunHotReloadingListener(interopServer, lsOpts.HotReloadingPaths, fileWatcherContext)
235+
go RunHotReloadingListener(interopServer, lsOpts.HotReloadingPaths, fileWatcherContext, lsOpts.FileWatcherStrategy)
234236

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

0 commit comments

Comments
 (0)