()
| 16 | const systemHookIdNotifyWatcher = "__pbNotifyWatcherSystemHook__" |
| 17 | |
| 18 | func (app *BaseApp) registerNotifyWatcherHooks() { |
| 19 | var notifyWatcher *fsnotify.Watcher |
| 20 | |
| 21 | instanceId := "@" + security.PseudorandomString(10) |
| 22 | |
| 23 | localNotifyDirPath := filepath.Join(app.DataDir(), LocalNotifyDirName) |
| 24 | settingsFile := filepath.Join(localNotifyDirPath, "settings"+instanceId) |
| 25 | collectionsFile := filepath.Join(localNotifyDirPath, "collections"+instanceId) |
| 26 | |
| 27 | // init |
| 28 | app.OnBootstrap().Bind(&hook.Handler[*BootstrapEvent]{ |
| 29 | Id: systemHookIdNotifyWatcher, |
| 30 | Func: func(e *BootstrapEvent) error { |
| 31 | err := e.Next() |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | |
| 36 | if notifyWatcher != nil { |
| 37 | _ = notifyWatcher.Close() |
| 38 | } |
| 39 | |
| 40 | notifyWatcher, err = createNotifyDirWatcher(e.App, instanceId, localNotifyDirPath) |
| 41 | if err != nil { |
| 42 | e.App.Logger().Warn("Notify dir watcher failure.", "error", err) |
| 43 | } |
| 44 | |
| 45 | return nil |
| 46 | }, |
| 47 | Priority: -998, |
| 48 | }) |
| 49 | |
| 50 | // cleanup |
| 51 | app.OnTerminate().Bind(&hook.Handler[*TerminateEvent]{ |
| 52 | Id: systemHookIdNotifyWatcher, |
| 53 | Func: func(e *TerminateEvent) error { |
| 54 | if notifyWatcher != nil { |
| 55 | _ = notifyWatcher.Close() |
| 56 | } |
| 57 | |
| 58 | _ = os.Remove(settingsFile) |
| 59 | _ = os.Remove(collectionsFile) |
| 60 | |
| 61 | return e.Next() |
| 62 | }, |
| 63 | Priority: -998, |
| 64 | }) |
| 65 | |
| 66 | // --------------------------------------------------------------- |
| 67 | |
| 68 | settingsNotify := func(e *ModelEvent) error { |
| 69 | err := e.Next() |
| 70 | if err != nil || e.Model.PK() != paramsKeySettings { |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | if notifyWatcher != nil { |
| 75 | if err := os.WriteFile(settingsFile, nil, 0644); err != nil { |
no test coverage detected