1 Star 0 Fork 0

煮酒品天下/go-fresh

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
start.go 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
package runner
import (
"fmt"
"os"
"runtime"
"strings"
"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() {
loopIndex := 0
buildDelay := buildDelay()
started := false
go func() {
for {
loopIndex++
mainLog("Waiting (loop %d)...", loopIndex)
eventName := <-startChannel
mainLog("receiving first event %s", eventName)
mainLog("sleeping for %d milliseconds", buildDelay)
time.Sleep(buildDelay * time.Millisecond)
mainLog("flushing events")
flushEvents()
mainLog("Started! (%d Goroutines)", runtime.NumGoroutine())
err := removeBuildErrorsLog()
if err != nil {
mainLog(err.Error())
}
buildFailed := false
if shouldRebuild(eventName) {
errorMessage, ok := build()
if !ok {
buildFailed = true
mainLog("Build Failed: \n %s", errorMessage)
if !started {
os.Exit(1)
}
createBuildErrorsLog(errorMessage)
}
}
if !buildFailed {
if started {
stopChannel <- true
}
run()
}
started = true
mainLog(strings.Repeat("-", 20))
}
}()
}
func init() {
startChannel = make(chan string, 1000)
stopChannel = make(chan bool)
}
func initLogFuncs() {
mainLog = newLogFunc("main")
watcherLog = newLogFunc("watcher")
runnerLog = newLogFunc("runner")
buildLog = newLogFunc("build")
appLog = newLogFunc("app")
}
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)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/werde/go-fresh.git
git@gitee.com:werde/go-fresh.git
werde
go-fresh
go-fresh
0fa698148017

搜索帮助