-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovo.go
193 lines (153 loc) · 3.71 KB
/
ovo.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
package main
import (
"fmt"
"os"
"os/exec"
"sync"
"time"
"github.com/francium/ovo/internal"
log "github.com/sirupsen/logrus"
"github.com/fatih/color"
"github.com/fsnotify/fsnotify"
)
const defaultWatchFile = ".watchfile"
const clearScreenEscapeSequence = "\x1bc"
func main() {
args := internal.ParseArgs()
if args.WatchPath == "" {
args.WatchPath = defaultWatchFile
}
if args.Help {
internal.DisplayHelp()
os.Exit(0)
}
internal.InitLogging(args.Verbose)
if args.WatchPath == defaultWatchFile {
internal.CreateIfNotExists(defaultWatchFile)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
log.Info("Watching ", args.WatchPath)
err = watcher.Add(args.WatchPath)
if err != nil {
log.Fatal(err)
}
cancel := make(chan struct{})
runner_lock := sync.Mutex{}
i := 0
go handleFSEvent(watcher, args.StartNow, func() {
i++
log_prefix := fmt.Sprintf("(%d)", i)
log.Info(log_prefix, " Execution routine start")
select {
case cancel <- struct{}{}:
default:
}
log.Info(log_prefix, " Execution routine waiting for runner lock")
runner_lock.Lock()
log.Info(log_prefix, " Execution routine acquired runner lock")
cmd := exec.Command(
"bash", "-c", args.Cmd,
)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
done := make(chan struct{})
killing := false
go func() {
log.Info(log_prefix, " Cancel routine start")
<-cancel
log.Info(log_prefix, " Cancel routine received cancel sign")
if cmd != nil {
log.Info(log_prefix, " Cancel routine killing cmd")
killing = true
cmd.Process.Signal(args.Signal)
} else {
log.Warn(log_prefix, " Cmd is nil")
}
log.Info(log_prefix, " Cancel routine end ")
}()
go func() {
log.Info(log_prefix, " Runner routine start ")
if args.ClearScreen {
fmt.Print(clearScreenEscapeSequence)
}
bold := color.New(color.Bold).SprintFunc()
if args.ShowTime {
time := time.Now().Format(time.TimeOnly)
fmt.Printf(bold("[%s] > %s\n"), time, args.Cmd)
} else {
fmt.Println(bold("> ", args.Cmd))
}
log.Info(log_prefix, " Runner routine invoking command ")
err := cmd.Run()
if err != nil && !killing {
log.Error(log_prefix, " Failed to run command: ", err)
}
select {
case done <- struct{}{}:
default:
}
log.Info(log_prefix, " Runner routine end ")
}()
// It's possible this logic below isn't 100% bulletproof. Especially
// the way the cancel is triggered in the go routine below and the
// done/cancel block is below that.
// Here we want to first block until one of done or cancel send a
// message
select {
case <-done:
case <-cancel:
}
// Setup an async task to send a message to trigger the cancellation
go func() {
select {
case cancel <- struct{}{}:
default:
}
}()
// Wait until until the other channel sends a message.
select {
case <-done:
case <-cancel:
}
if cmd.ProcessState != nil && !cmd.ProcessState.Exited() {
log.Warning(
fmt.Sprintf(
"%s Process did not exit, current state=%s, exit status=%d",
log_prefix,
cmd.ProcessState,
cmd.ProcessState.ExitCode(),
),
)
}
log.Info(log_prefix, " Execution routine releasing lock ")
runner_lock.Unlock()
log.Info(log_prefix, " Execution routine end ")
})
// Block
<-make(chan struct{})
}
func handleFSEvent(watcher *fsnotify.Watcher, start_now bool, cb func()) {
if start_now {
go cb()
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
log.Error("Failed to read event from channel")
return
}
log.Infof("path=%s, op=%s", event.Name, event.Op)
go cb()
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Error("Error:", err)
}
}
}