-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner_watch_target.go
199 lines (171 loc) · 4.82 KB
/
runner_watch_target.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package service
import (
"fmt"
"os"
"path/filepath"
"sync"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/seungyeop-lee/directory-watcher/internal/app/domain"
"github.com/seungyeop-lee/directory-watcher/internal/helper"
)
type watchTargetRunners []*watchTargetRunner
type watchTargetRunner struct {
path domain.Path
onStartWatch domain.Cmd
onBeforeChange domain.Cmd
onChange domain.Cmd
onAfterChange domain.Cmd
onFinishWatch domain.Cmd
excludeDir domain.Paths
excludeSuffix domain.PathSuffixes
waitMillisecond domain.Millisecond
logger Logger
watcher *fsnotify.Watcher
}
func NewWatchTargetRunner(globalCommandSet domain.GlobalCommandSet, commandSet domain.WatchTargetsCommandSet, logger Logger) *watchTargetRunner {
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Error(err.Error())
}
return &watchTargetRunner{
path: commandSet.Path,
onStartWatch: commandSet.LifeCycle.OnStartWatch,
onBeforeChange: globalCommandSet.LifeCycle.OnBeforeChange,
onChange: commandSet.LifeCycle.OnChange,
onAfterChange: globalCommandSet.LifeCycle.OnAfterChange,
onFinishWatch: commandSet.LifeCycle.OnFinishWatch,
excludeDir: commandSet.Option.ExcludeDir,
excludeSuffix: commandSet.Option.ExcludeSuffix,
waitMillisecond: commandSet.Option.WaitMillisecond,
logger: logger,
watcher: watcher,
}
}
func (r watchTargetRunner) Run() {
r.callOnStartWatch()
r.addDir()
event := make(chan domain.Event)
go func(evChan chan domain.Event) {
var threshold <-chan time.Time
for {
select {
case ev := <-evChan:
if r.excludeSuffix.Contain(ev.Path) {
break
}
threshold = helper.CreateThreshold(r.waitMillisecond.Duration())
case <-threshold:
r.callOnBeforeChange()
r.callOnChange()
r.callOnAfterChange()
}
}
}(event)
for {
select {
case ev := <-r.watcher.Events:
if ev.Op&fsnotify.Create == fsnotify.Create {
r.logger.Info(fmt.Sprintf("%s has created", ev.Name))
if domain.FileName(ev.Name).IsDefaultExcludeFile() {
break
}
if domain.Path(ev.Name).IsDir() && !r.excludeDir.Equal(domain.Path(ev.Name)) {
r.watcher.Add(ev.Name)
}
} else if ev.Op&fsnotify.Remove == fsnotify.Remove {
r.logger.Info(fmt.Sprintf("%s has removed", ev.Name))
r.watcher.Remove(ev.Name)
}
if ev.Op&fsnotify.Create == fsnotify.Create || ev.Op&fsnotify.Write == fsnotify.Write || ev.Op&fsnotify.Remove == fsnotify.Remove {
r.logger.Info(fmt.Sprintf("%s has changed", ev.Name))
event <- domain.NewEventByFsnotify(ev)
}
case err := <-r.watcher.Errors:
if v, ok := err.(*os.SyscallError); ok {
// 인터럽트 발생은 에러로 처리하지 않는다.
if v.Err == syscall.EINTR {
continue
}
r.logger.Debug(fmt.Sprint("watcher.Error: SyscallError:", v))
}
r.logger.Debug(fmt.Sprint("watcher.Error:", err))
}
}
}
func (r watchTargetRunner) Stop(wg *sync.WaitGroup) {
defer wg.Done()
r.callOnFinishWatch()
}
func (r watchTargetRunner) addDir() {
if !helper.IsExist(r.path.String()) {
r.logger.Error(fmt.Sprintf("not exist path: %s", r.path))
return
}
err := filepath.Walk(r.path.String(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
if r.excludeDir.Equal(domain.Path(path)) {
return nil
}
if r.excludeDir.IsSubFolder(domain.Path(path)) {
return nil
}
r.logger.Info(fmt.Sprint("add path:", path))
return r.watcher.Add(path)
})
if err != nil {
panic(err)
}
}
func (r watchTargetRunner) callOnStartWatch() {
err := r.printInfo("callOnStartWatch", r.onStartWatch, func() error {
return r.onStartWatch.Run(r.path)
})
if err != nil {
r.logger.Error(err.Error())
}
}
func (r watchTargetRunner) callOnBeforeChange() {
err := r.printInfo("callOnBeforeChange", r.onBeforeChange, func() error {
return r.onBeforeChange.Run(r.path)
})
if err != nil {
r.logger.Error(err.Error())
}
}
func (r watchTargetRunner) callOnChange() {
err := r.printInfo("callOnChange", r.onChange, func() error {
return r.onChange.Run(r.path)
})
if err != nil {
r.logger.Error(err.Error())
}
}
func (r watchTargetRunner) callOnAfterChange() {
err := r.printInfo("callOnAfterChange", r.onAfterChange, func() error {
return r.onAfterChange.Run(r.path)
})
if err != nil {
r.logger.Error(err.Error())
}
}
func (r watchTargetRunner) callOnFinishWatch() {
err := r.printInfo("callOnFinishWatch", r.onFinishWatch, func() error {
return r.onFinishWatch.Run(r.path)
})
if err != nil {
r.logger.Error(err.Error())
}
}
func (r watchTargetRunner) printInfo(methodName string, cmd domain.Cmd, fn func() error) error {
r.logger.Info(fmt.Sprintf("%s start: %v", methodName, cmd))
err := fn()
r.logger.Info(fmt.Sprintf("%s finished: %v", methodName, cmd))
return err
}