1
0
mirror of https://github.com/beego/bee.git synced 2024-11-21 13:40:53 +00:00

half work

This commit is contained in:
sun shengxiang 2013-07-06 15:30:57 +08:00
parent 14510fc9b6
commit 0cf1a553de
4 changed files with 125 additions and 43 deletions

View File

@ -45,6 +45,7 @@ func createapp(cmd *Command, args []string) {
}
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
fmt.Println("you should set GOPATH in the env")
os.Exit(2)

View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
path "path/filepath"
"runtime"
)
var cmdStart = &Command{
@ -32,22 +33,31 @@ func init() {
cmdStart.Run = startapp
}
var appname string
func startapp(cmd *Command, args []string) {
if len(args) != 1 {
fmt.Println("error args")
os.Exit(2)
}
crupath, _ := os.Getwd()
Debugf("current path:%s\n", crupath)
var paths []string
paths = append(paths, path.Join(crupath, "controllers"), path.Join(crupath, "models"))
NewWatcher(paths)
go Start(args[0])
appname = args[0]
Autobuild()
for {
select {
case <-restart:
go Start(args[0])
case <-builderror:
fmt.Println("build error:", builderror)
}
runtime.Gosched()
}
//go Start(args[0])
//for {
// select {
// case <-restart:
// go Start(args[0])
// case err := <-builderror:
// fmt.Println("build error:", err)
// }
//}
}

30
util.go Normal file
View File

@ -0,0 +1,30 @@
package main
import "fmt"
import "os"
import "runtime"
import "path/filepath"
// Go is a basic promise implementation: it wraps calls a function in a goroutine
// and returns a channel which will later return the function's return value.
func Go(f func() error) chan error {
ch := make(chan error)
go func() {
ch <- f()
}()
return ch
}
// if os.env DEBUG set, debug is on
func Debugf(format string, a ...interface{}) {
if os.Getenv("DEBUG") != "" {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "<unknown>"
line = -1
} else {
file = filepath.Base(file)
}
fmt.Fprintf(os.Stderr, fmt.Sprintf("[debug] %s:%d %s\n", file, line, format), a...)
}
}

113
watch.go
View File

@ -1,27 +1,33 @@
package main
import (
"bytes"
"fmt"
"github.com/howeyc/fsnotify"
"io"
//"io"
"log"
"os"
"os/exec"
"runtime"
"sync"
"time"
)
var (
builderror chan string
restart chan bool
cmd *exec.Cmd
state sync.Mutex
running bool
)
func init() {
builderror = make(chan string)
restart = make(chan bool)
//restart = make(chan bool)
running = false
}
var eventTime = make(map[string]time.Time)
func NewWatcher(paths []string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
@ -32,8 +38,21 @@ func NewWatcher(paths []string) {
for {
select {
case e := <-watcher.Event:
fmt.Println(e)
go Autobuild()
isbuild := true
if t, ok := eventTime[e.String()]; ok {
//Debugf("%s pk %s", t, time.Now())
if t.Add(time.Millisecond * 500).After(time.Now()) {
isbuild = false
}
}
Debugf("isbuild:%v", isbuild)
eventTime[e.String()] = time.Now()
if isbuild {
fmt.Println(e)
go Autobuild()
}
case err := <-watcher.Error:
log.Fatal("error:", err)
}
@ -64,23 +83,18 @@ func Autobuild() {
}
}()
fmt.Println("Autobuild")
fmt.Println("autobuild")
path, _ := os.Getwd()
os.Chdir(path)
bcmd := exec.Command("go", "build")
var out bytes.Buffer
var berr bytes.Buffer
bcmd.Stdout = &out
bcmd.Stderr = &berr
bcmd.Stdout = os.Stdout
bcmd.Stderr = os.Stderr
err := bcmd.Run()
if err != nil {
fmt.Println("run error", err)
}
if out.String() == "" {
Kill()
} else {
builderror <- berr.String()
builderror <- err.Error()
return
}
Restart(appname)
}
func Kill() {
@ -90,31 +104,58 @@ func Kill() {
}
}
func Restart(appname string) {
Kill()
go Start()
}
func Start(appname string) {
fmt.Println("start", appname)
cmd = exec.Command(appname)
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)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
ch := Go(func() error {
state.Lock()
defer state.Unlock()
running = true
return cmd.Run()
})
for {
buf := make([]byte, 1024)
count, err := r.Read(buf)
if err != nil || count == 0 {
fmt.Println("process exit")
restart <- true
select {
case err := <-ch:
fmt.Println("cmd start error: ", err)
state.Lock()
defer state.Unlock()
running = false
return
} else {
fmt.Println("result:", string(buf))
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))
// }
//}
}