1
0
mirror of https://github.com/beego/bee.git synced 2024-11-21 23:50:54 +00:00

remove useless codes

This commit is contained in:
sun shengxiang 2013-07-06 16:13:30 +08:00
parent 0cf1a553de
commit 9e602fd75f
2 changed files with 23 additions and 76 deletions

View File

@ -51,13 +51,4 @@ func startapp(cmd *Command, args []string) {
for { for {
runtime.Gosched() runtime.Gosched()
} }
//go Start(args[0])
//for {
// select {
// case <-restart:
// go Start(args[0])
// case err := <-builderror:
// fmt.Println("build error:", err)
// }
//}
} }

View File

@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/howeyc/fsnotify" "github.com/howeyc/fsnotify"
//"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -13,21 +12,12 @@ import (
) )
var ( var (
builderror chan string
restart chan bool restart chan bool
cmd *exec.Cmd cmd *exec.Cmd
state sync.Mutex state sync.Mutex
running bool eventTime = make(map[string]time.Time)
) )
func init() {
builderror = make(chan string)
//restart = make(chan bool)
running = false
}
var eventTime = make(map[string]time.Time)
func NewWatcher(paths []string) { func NewWatcher(paths []string) {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
@ -38,15 +28,14 @@ func NewWatcher(paths []string) {
for { for {
select { select {
case e := <-watcher.Event: case e := <-watcher.Event:
isbuild := true isbuild := true
if t, ok := eventTime[e.String()]; ok { if t, ok := eventTime[e.String()]; ok {
//Debugf("%s pk %s", t, time.Now()) // if 500ms change many times, then ignore it.
// for liteide often gofmt code after save.
if t.Add(time.Millisecond * 500).After(time.Now()) { if t.Add(time.Millisecond * 500).After(time.Now()) {
isbuild = false isbuild = false
} }
} }
Debugf("isbuild:%v", isbuild)
eventTime[e.String()] = time.Now() eventTime[e.String()] = time.Now()
if isbuild { if isbuild {
@ -69,6 +58,8 @@ func NewWatcher(paths []string) {
} }
func Autobuild() { func Autobuild() {
state.Lock()
defer state.Unlock()
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
str := "" str := ""
@ -79,35 +70,42 @@ func Autobuild() {
} }
str = str + fmt.Sprintf("%v,%v", file, line) str = str + fmt.Sprintf("%v,%v", file, line)
} }
builderror <- str
} }
}() }()
fmt.Println("autobuild") fmt.Println("start autobuild")
path, _ := os.Getwd() path, _ := os.Getwd()
os.Chdir(path) os.Chdir(path)
bcmd := exec.Command("go", "build") bcmd := exec.Command("go", "build")
bcmd.Stdout = os.Stdout bcmd.Stdout = os.Stdout
bcmd.Stderr = os.Stderr bcmd.Stderr = os.Stderr
err := bcmd.Run() err := bcmd.Run()
if err != nil { if err != nil {
builderror <- err.Error() fmt.Println("============== build failed ===================")
return return
} }
fmt.Println("build success")
Restart(appname) Restart(appname)
} }
func Kill() { func Kill() {
err := cmd.Process.Kill() defer func() {
if err != nil { if e := recover(); e != nil {
panic(err) fmt.Println("Kill -> ", e)
}
}()
if cmd != nil {
cmd.Process.Kill()
} }
} }
func Restart(appname string) { func Restart(appname string) {
Debugf("kill running process")
Kill() Kill()
go Start() go Start(appname)
} }
func Start(appname string) { func Start(appname string) {
fmt.Println("start", appname) fmt.Println("start", appname)
@ -115,47 +113,5 @@ func Start(appname string) {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
ch := Go(func() error { go cmd.Run()
state.Lock()
defer state.Unlock()
running = true
return cmd.Run()
})
for {
select {
case err := <-ch:
fmt.Println("cmd start error: ", err)
state.Lock()
defer state.Unlock()
running = false
return
case <-time.After(2 * time.Second):
}
}
//stdout, err := cmd.StdoutPipe()
//if err != nil {
//fmt.Println("stdout:", err)
//}
//stderr, err := cmd.StderrPipe()
//if err != nil {
//fmt.Println("stdin:", err)
//}
//r := io.MultiReader(stdout, stderr)
//err = cmd.Start()
//if err != nil {
// fmt.Println("cmd start:", err)
//}
//for {
// buf := make([]byte, 1024)
// count, err := r.Read(buf)
// if err != nil || count == 0 {
// fmt.Println("process exit")
// restart <- true
// return
// } else {
// fmt.Println("result:", string(buf))
// }
//}
} }