-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_monitor_events.go
55 lines (47 loc) · 1.24 KB
/
file_monitor_events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 通过文件监控使数据库自适应
package orm
import (
"../../dm_1"
"github.com/fsnotify/fsnotify"
"github.com/kpango/glg"
)
func createDb( event fsnotify.Event ) {
DMAddResources( []dm_1.DMResource{ { Path: event.Name } }, nil )
}
func removeDb( event fsnotify.Event ) {
if tr, e := DMGetTargetResourceByPath(event.Name); e != nil {
glg.Error( "[WATCHER] REMOVE: ", e )
} else {
tr.Dead = true
tr.Update()
}
}
func writeDb( event fsnotify.Event ) {
if tr, e := DMGetTargetResourceByPath(event.Name); e != nil {
glg.Error( "[WATCHER] WRITE: ", e )
} else {
if r, e := tr.Decay(); e != nil { glg.Error( "[WATCHER] WRITE while resource decaying: ", e ) } else {
if tr.MD5, e = r.GetMD5(); e != nil { glg.Error( "[WATCHER] WRITE while getting new md5: ", e ) } else {
tr.Update()
}
}
}
}
func renameDb( event fsnotify.Event ) {
if tr, e := DMGetTargetResourceByPath(event.Name); e != nil {
glg.Error( "[WATCHER] RENAME: ", e )
} else {
tr.Dead = true
tr.Update()
}
}
func DMNewNotifyFileDb() *dm_1.DMNotifyFile {
w := new(dm_1.DMNotifyFile)
w.Watch, _ = fsnotify.NewWatcher()
w.Create = createDb
w.Remove = removeDb
w.Rename = renameDb
w.Write = writeDb
w.Chmod = func(event fsnotify.Event) {}
return w
}