-
Notifications
You must be signed in to change notification settings - Fork 6
/
gaper.go
228 lines (183 loc) · 5.35 KB
/
gaper.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Package gaper implements a supervisor restarts a go project
// when it crashes or a watched file changes
package gaper
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
shellwords "github.com/mattn/go-shellwords"
)
// DefaultBuildPath is the default build and watched path
var DefaultBuildPath = "."
// DefaultExtensions is the default watched extension
var DefaultExtensions = []string{"go"}
// DefaultPoolInterval is the time in ms used by the watcher to wait between scans
var DefaultPoolInterval = 500
// No restart types
var (
NoRestartOnError = "error"
NoRestartOnSuccess = "success"
NoRestartOnExit = "exit"
)
// exit statuses
var exitStatusSuccess = 0
var exitStatusError = 1
// Config contains all settings supported by gaper
type Config struct {
BinName string
BuildPath string
BuildArgs []string
BuildArgsMerged string
ProgramArgs []string
ProgramArgsMerged string
WatchItems []string
IgnoreItems []string
PollInterval int
Extensions []string
NoRestartOn string
DisableDefaultIgnore bool
WorkingDirectory string
}
// Run starts the whole gaper process watching for file changes or exit codes
// and restarting the program
func Run(cfg *Config, chOSSiginal chan os.Signal) error {
logger.Debug("Starting gaper")
if err := setupConfig(cfg); err != nil {
return err
}
logger.Debugf("Config: %+v", cfg)
wCfg := WatcherConfig{
DefaultIgnore: !cfg.DisableDefaultIgnore,
PollInterval: cfg.PollInterval,
WatchItems: cfg.WatchItems,
IgnoreItems: cfg.IgnoreItems,
Extensions: cfg.Extensions,
}
builder := NewBuilder(cfg.BuildPath, cfg.BinName, cfg.WorkingDirectory, cfg.BuildArgs)
runner := NewRunner(os.Stdout, os.Stderr, filepath.Join(cfg.WorkingDirectory, builder.Binary()), cfg.ProgramArgs)
watcher, err := NewWatcher(wCfg)
if err != nil {
return fmt.Errorf("watcher error: %v", err)
}
return run(cfg, chOSSiginal, builder, runner, watcher)
}
// nolint: gocyclo
func run(cfg *Config, chOSSiginal chan os.Signal, builder Builder, runner Runner, watcher Watcher) error {
if err := builder.Build(); err != nil {
return fmt.Errorf("build error: %v", err)
}
// listen for OS signals
signal.Notify(chOSSiginal, os.Interrupt, syscall.SIGTERM)
if _, err := runner.Run(); err != nil {
return fmt.Errorf("run error: %v", err)
}
// flag to know if an exit was caused by a restart from a file changing
changeRestart := false
go watcher.Watch()
for {
select {
case event := <-watcher.Events():
logger.Debug("Detected new changed file:", event)
if changeRestart {
logger.Debug("Skip restart due to existing on going restart")
continue
}
changeRestart = runner.IsRunning()
if err := restart(builder, runner); err != nil {
return err
}
case err := <-watcher.Errors():
return fmt.Errorf("error on watching files: %v", err)
case err := <-runner.Errors():
logger.Debug("Detected program exit:", err)
// ignore exit by change
if changeRestart {
changeRestart = false
continue
}
if err = handleProgramExit(builder, runner, err, cfg.NoRestartOn); err != nil {
return err
}
case signal := <-chOSSiginal:
logger.Debug("Got signal:", signal)
if err := runner.Kill(); err != nil {
logger.Error("Error killing:", err)
}
return fmt.Errorf("OS signal: %v", signal)
default:
time.Sleep(time.Duration(cfg.PollInterval) * time.Millisecond)
}
}
}
func restart(builder Builder, runner Runner) error {
logger.Debug("Restarting program")
// kill process if it is running
if !runner.Exited() {
if err := runner.Kill(); err != nil {
return fmt.Errorf("kill error: %v", err)
}
}
if err := builder.Build(); err != nil {
logger.Error("Error building binary during a restart:", err)
return nil
}
if _, err := runner.Run(); err != nil {
logger.Error("Error starting process during a restart:", err)
return nil
}
return nil
}
func handleProgramExit(builder Builder, runner Runner, err error, noRestartOn string) error {
exitStatus := runner.ExitStatus(err)
// if "error", an exit code of 0 will still restart.
if noRestartOn == NoRestartOnError && exitStatus == exitStatusError {
return nil
}
// if "success", no restart only if exit code is 0.
if noRestartOn == NoRestartOnSuccess && exitStatus == exitStatusSuccess {
return nil
}
// if "exit", no restart regardless of exit code.
if noRestartOn == NoRestartOnExit {
return nil
}
return restart(builder, runner)
}
func setupConfig(cfg *Config) error {
var err error
if len(cfg.BuildPath) == 0 {
cfg.BuildPath = DefaultBuildPath
}
cfg.BuildArgs, err = parseInnerArgs(cfg.BuildArgs, cfg.BuildArgsMerged)
if err != nil {
return err
}
cfg.ProgramArgs, err = parseInnerArgs(cfg.ProgramArgs, cfg.ProgramArgsMerged)
if err != nil {
return err
}
cfg.WorkingDirectory, err = os.Getwd()
if err != nil {
return err
}
if len(cfg.WatchItems) == 0 {
cfg.WatchItems = append(cfg.WatchItems, cfg.BuildPath)
}
var extensions []string
for i := range cfg.Extensions {
values := strings.Split(cfg.Extensions[i], ",")
extensions = append(extensions, values...)
}
cfg.Extensions = extensions
return nil
}
func parseInnerArgs(args []string, argsm string) ([]string, error) {
if len(args) > 0 || len(argsm) == 0 {
return args, nil
}
return shellwords.Parse(argsm)
}