/
start.go
178 lines (156 loc) · 3.59 KB
/
start.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
package runner
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"syscall"
"time"
)
var (
watchChannel chan string
killChannel chan struct{}
killDoneChannel chan struct{}
doneChannel chan struct{}
exiting bool
mainLog logFunc
watcherLog logFunc
runnerLog logFunc
buildLog logFunc
appLog logFunc
)
func flushEvents() {
for {
select {
case eventName := <-watchChannel:
if isDebug() {
mainLog("Skipping %s", eventName)
}
default:
return
}
}
}
func start() {
loopIndex := 0
delay := buildDelay()
started := false
go termHandler()
go func() {
for {
if isDebug() {
mainLog("Waiting for (Loop: %d, Goroutines: %d)", loopIndex, runtime.NumGoroutine())
}
eventName := <-watchChannel
if isDebug() {
mainLog("First event: %s", eventName)
}
if loopIndex > 0 {
if isDebug() {
mainLog("Sleeping %v", delay)
}
time.Sleep(delay)
}
err := removeBuildErrorsLog()
if err != nil && !os.IsNotExist(err) {
if isDebug() {
mainLog(err.Error())
}
}
if isDebug() {
mainLog("Skipping events")
}
flushEvents()
buildFailed := false
if shouldRebuild(eventName) {
mainLog("Rebuilding due to: %v", eventName)
err := build()
if err != nil {
buildFailed = true
mainLog("Failed:\n%v", err)
if !started {
os.Exit(1)
}
createBuildErrorsLog(err.Error())
}
if !buildFailed {
if started {
killChannel <- struct{}{}
<-killDoneChannel // we don't want run() before killing is finished
}
run()
started = true
}
}
loopIndex++
}
}()
}
func init() {
watchChannel = make(chan string, 1000)
killChannel = make(chan struct{})
killDoneChannel = make(chan struct{})
}
const maxPrefixLength = 7
func initLogFuncs() {
mainLog = newLogFunc("main", true)
watcherLog = newLogFunc("watcher", true)
runnerLog = newLogFunc("runner", true)
buildLog = newLogFunc("build", true)
appLog = newLogFunc("app", false)
}
func setEnvVars() {
os.Setenv("DEV_RUNNER", "1")
wd, err := os.Getwd()
if err == nil {
os.Setenv(EnvPrefix+"WD", wd) // RUNNER_WD
}
os.Setenv(EnvPrefix+"CONFIG_PATH", ConfigPath)
t := reflect.TypeOf(Settings{})
v := reflect.ValueOf(settings)
for i := 0; i < t.NumField(); i++ {
keyName, _, noenv := tagDetails(t.Field(i).Tag)
if keyName != "" && !noenv {
envKey := fmt.Sprintf("%s%s", EnvPrefix, strings.ToUpper(keyName))
field := v.Field(i)
err = nil
if field.Kind() == reflect.String {
err = os.Setenv(envKey, field.String())
} else if field.Kind() == reflect.Bool {
err = os.Setenv(envKey, strconv.FormatBool(field.Bool()))
} else if field.Kind() == reflect.Uint {
err = os.Setenv(envKey, strconv.FormatUint(field.Uint(), 10))
}
if err != nil {
fmt.Printf("Error setting %q to %v due to %v", envKey, field.String(), err)
}
//runnerLog("%v set to %q", envKey, os.Getenv(envKey))
}
}
}
func termHandler() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
exiting = true
killChannel <- struct{}{}
}
// Start Watches for file changes in the root directory.
// After each file system event it builds and (re)starts the application.
func Start() {
doneChannel = make(chan struct{})
initLimit()
initLogFuncs() // Initialize log functions with default settings for initSettings to use
initSettings()
initLogFuncs() // Repeat after reading config file
initFolders()
setEnvVars()
watch()
start()
watchChannel <- string(filepath.Separator)
<-doneChannel
}