(app App, instanceId string, localNotifyDirPath string)
| 125 | } |
| 126 | |
| 127 | func 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 | } |
no test coverage detected
searching dependent graphs…