forked from lateefj/fresh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.go
115 lines (100 loc) · 1.88 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
package runner
import (
"fmt"
"os"
"strings"
"syscall"
"time"
)
var (
startChannel chan string
stopChannel chan bool
mainLog logFunc
watcherLog logFunc
runnerLog logFunc
buildLog logFunc
appLog logFunc
)
func flushEvents() {
for {
select {
case eventName := <-startChannel:
mainLog("receiving event %s", eventName)
default:
return
}
}
}
func start() {
buildDelay := buildDelay()
started := false
go func() {
for {
<-startChannel
time.Sleep(buildDelay * time.Millisecond)
flushEvents()
err := removeBuildErrorsLog()
if err != nil {
mainLog(err.Error())
}
errorMessage, ok := build()
if !ok {
mainLog("Build Failed: \n %s", errorMessage)
if !started {
os.Exit(1)
}
createBuildErrorsLog(errorMessage)
} else {
if started {
stopChannel <- true
}
run()
}
started = true
}
}()
}
func init() {
startChannel = make(chan string, 1000)
stopChannel = make(chan bool)
}
func initLogFuncs() {
mainLog = newLogFunc("fresh")
watcherLog = newLogFunc("watcher")
runnerLog = newLogFunc("runner")
buildLog = newLogFunc("builder")
appLog = newLogFunc("bin")
}
func initLimit() {
var rLimit syscall.Rlimit
rLimit.Max = 10000
rLimit.Cur = 10000
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Setting Rlimit ", err)
}
}
func setEnvVars() {
os.Setenv("DEV_RUNNER", "1")
wd, err := os.Getwd()
if err == nil {
os.Setenv("RUNNER_WD", wd)
}
for k, v := range settings {
key := strings.ToUpper(fmt.Sprintf("%s%s", envSettingsPrefix, k))
os.Setenv(key, v)
}
}
// Watches for file changes in the root directory.
// After each file system event it builds and (re)starts the application.
func Start() {
initLimit()
initSettings()
initLogFuncs()
initFolders()
setEnvVars()
watch()
start()
startChannel <- "/"
<-make(chan int)
}