MCPcopy
hub / github.com/pocketbase/pocketbase / createNotifyDirWatcher

Function createNotifyDirWatcher

core/notify_watcher.go:127–210  ·  view source on GitHub ↗
(app App, instanceId string, localNotifyDirPath string)

Source from the content-addressed store, hash-verified

125}
126
127func createNotifyDirWatcher(app App, instanceId string, localNotifyDirPath string) (*fsnotify.Watcher, error) {
128 // create the notify dir (if not already)
129 err := os.MkdirAll(localNotifyDirPath, os.ModePerm)
130 if err != nil {
131 return nil, fmt.Errorf("failed to create a notify dir: %w", err)
132 }
133
134 watcher, err := fsnotify.NewWatcher()
135 if err != nil {
136 return nil, fmt.Errorf("failed to init notify dir watcher: %w", err)
137 }
138
139 err = watcher.Add(localNotifyDirPath)
140 if err != nil {
141 _ = watcher.Close()
142 return nil, fmt.Errorf("unable to watch notify dir: %w", err)
143 }
144
145 var debounceTimer *time.Timer
146
147 stopDebounceTimer := func() {
148 if debounceTimer != nil {
149 debounceTimer.Stop()
150 debounceTimer = nil
151 }
152 }
153
154 // watch
155 go func() {
156 defer stopDebounceTimer()
157
158 for {
159 select {
160 case event, ok := <-watcher.Events:
161 if !ok {
162 return
163 }
164
165 // modified from within the current app instance or cleanup event
166 if strings.HasSuffix(event.Name, instanceId) || event.Has(fsnotify.Remove) || !app.IsBootstrapped() {
167 continue
168 }
169
170 stopDebounceTimer()
171
172 debounceTimer = time.AfterFunc(50*time.Millisecond, func() {
173 filename := filepath.Base(event.Name)
174
175 // settings changed
176 if strings.HasPrefix(filename, "settings@") {
177 app.Logger().Debug("Reloading settings after notify event")
178
179 err := app.ReloadSettings()
180 if err != nil {
181 app.Logger().Warn("Failed to reload app settings after notify", "error", err)
182 }
183 return
184 }

Callers 1

Calls 9

StopMethod · 0.80
CloseMethod · 0.65
IsBootstrappedMethod · 0.65
LoggerMethod · 0.65
ReloadSettingsMethod · 0.65
IsDevMethod · 0.65
AddMethod · 0.45
HasMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…