/
watch.go
143 lines (127 loc) · 3.66 KB
/
watch.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
//go:build linux || darwin || windows
// +build linux darwin windows
package engine
import (
"context"
// "log/slog"
"os"
"path/filepath"
"time"
"github.com/rjeczalik/notify"
)
type EventManager struct {
engine *Engine
lastEventTime time.Time
debounceThreshold time.Duration
debounceTimer *time.Timer
ctx context.Context
cancel context.CancelFunc
}
func NewEventManager(engine *Engine, debounce int) *EventManager {
ctx, cancel := context.WithCancel(context.Background())
em := &EventManager{
engine: engine,
debounceThreshold: time.Duration(debounce) * time.Millisecond,
ctx: ctx,
cancel: cancel,
}
return em
}
func (em *EventManager) HandleEvent(ei notify.EventInfo) {
eventInfo, ok := EventMap[ei.Event()]
if !ok {
// slog.Error("Unknown event", "event", ei.Event())
return
}
if em.engine.Config.Callback != nil {
event := CallbackMap[ei.Event()]
handle := em.engine.Config.Callback(&EventCallback{
Type: event,
Path: getPath(ei.Path()),
Time: time.Now(),
})
switch handle {
case EventContinue:
// Continue
case EventBypass:
// slog.Debug("Bypassing event", "event", ei.Event(), "path", ei.Path())
return
case EventIgnore:
// slog.Debug("Ignoring event", "event", ei.Event(), "path", ei.Path())
return
default:
}
}
if eventInfo.Reload {
newCtx, newCancel := context.WithCancel(context.Background())
em.engine.ctx = newCtx
em.engine.cancel = newCancel
if em.engine.Config.Ignore.shouldIgnore(ei.Path()) {
return
}
// slog.Debug("Event", "event", ei.Event(), "path", ei.Path(), "time", time.Now())
currentTime := time.Now()
if currentTime.Sub(em.lastEventTime) >= em.debounceThreshold {
// slog.Debug("Setting debounce timer", "event", ei.Event(), "path", ei.Path(), "time", time.Now())
// slog.Info("File modified...Refreshing", "file", getPath(ei.Path()))
// Find the specific process associated with the file change event
for _, p := range em.engine.ProcessManager.Processes {
if p.Primary {
// Kill the specific process by canceling its context
if cancel, ok := em.engine.ProcessManager.Cancels[p.Exec]; ok {
cancel()
delete(em.engine.ProcessManager.Ctxs, p.Exec)
delete(em.engine.ProcessManager.Cancels, p.Exec)
}
break
}
}
// Start a new instance of the process
go em.engine.ProcessManager.StartProcess(em.engine.ctx, em.engine.cancel)
go func() {
<-em.engine.ctx.Done()
if em.engine.ctx.Err() == context.Canceled {
if !em.engine.ProcessManager.FirstRun {
// slog.Error("Could not refresh processes due to build errors")
newCtx, newCancel := context.WithCancel(context.Background())
em.engine.ctx = newCtx
em.engine.cancel = newCancel
return
}
em.engine.Stop()
}
}()
em.lastEventTime = currentTime
} else {
// slog.Debug("Debouncing event", "event", ei.Event(), "path", ei.Path(), "time", time.Now())
}
}
}
func (engine *Engine) watch(eventManager *EventManager) {
// slog.Info("Watching", "path", engine.Config.RootPath)
engine.Chan = make(chan notify.EventInfo, 1)
defer notify.Stop(engine.Chan)
if err := notify.Watch(engine.Config.RootPath+"/...", engine.Chan, notify.All); err != nil {
// slog.Error("Watch Error", "err", err.Error())
return
}
for {
select {
case ei := <-engine.Chan:
eventManager.HandleEvent(ei)
}
}
}
func getPath(path string) string {
wd, err := os.Getwd()
if err != nil {
// slog.Error("Getting working directory")
return ""
}
relPath, err := filepath.Rel(wd, path)
if err != nil {
// slog.Error("Getting relative path")
return ""
}
return relPath
}