MCPcopy
hub / github.com/pocketbase/pocketbase / registerNotifyWatcherHooks

Method registerNotifyWatcherHooks

core/notify_watcher.go:18–125  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16const systemHookIdNotifyWatcher = "__pbNotifyWatcherSystemHook__"
17
18func (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 {

Callers 1

registerBaseHooksMethod · 0.95

Calls 15

DataDirMethod · 0.95
OnBootstrapMethod · 0.95
OnTerminateMethod · 0.95
PseudorandomStringFunction · 0.92
createNotifyDirWatcherFunction · 0.85
NextMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected