fileboy/taskman.go

102 lines
2.1 KiB
Go
Raw Normal View History

2018-12-29 09:49:38 +00:00
package main
import (
"bufio"
2019-01-03 02:27:04 +00:00
"fmt"
2018-12-29 09:49:38 +00:00
"io"
"log"
"os"
"os/exec"
"sync"
"time"
)
2018-12-30 07:14:14 +00:00
type TaskMan struct {
2018-12-29 09:49:38 +00:00
lastTaskId int64
delay int
cmd *exec.Cmd
2019-01-02 11:07:52 +00:00
notifier *NetNotifier
2018-12-29 09:49:38 +00:00
putLock sync.Mutex
runLock sync.Mutex
}
2019-01-02 11:07:52 +00:00
func newTaskMan(delay int, callUrl string) *TaskMan {
2018-12-30 07:14:14 +00:00
return &TaskMan{
2019-01-02 11:07:52 +00:00
delay: delay,
notifier: newNetNotifier(callUrl),
2018-12-29 09:49:38 +00:00
}
}
2019-01-03 01:56:26 +00:00
func (t *TaskMan) Put(cf *changedFile) {
2018-12-29 09:49:38 +00:00
if t.delay < 1 {
2018-12-30 07:07:40 +00:00
t.preRun(cf)
2018-12-29 09:49:38 +00:00
return
}
t.putLock.Lock()
defer t.putLock.Unlock()
t.lastTaskId = cf.Changed
go func() {
2019-01-23 02:27:26 +00:00
<-time.After(time.Millisecond * time.Duration(t.delay))
2018-12-29 09:49:38 +00:00
if t.lastTaskId > cf.Changed {
return
}
t.preRun(cf)
}()
}
2019-01-03 01:56:26 +00:00
func (t *TaskMan) preRun(cf *changedFile) {
if t.cmd != nil && t.cmd.Process != nil && t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
2019-01-19 09:10:07 +00:00
log.Println("stop old process ")
2019-01-19 09:17:55 +00:00
if err := t.cmd.Process.Kill(); err != nil {
2019-01-19 09:10:07 +00:00
log.Println(PreWarn, "stopped err, reason:", err)
2018-12-29 09:49:38 +00:00
}
}
go t.run(cf)
}
2019-01-03 01:56:26 +00:00
func (t *TaskMan) run(cf *changedFile) {
2019-01-02 11:07:52 +00:00
go t.notifier.Put(cf)
2018-12-29 09:49:38 +00:00
t.runLock.Lock()
defer t.runLock.Unlock()
for i := 0; i < len(cfg.Command.Exec); i++ {
carr := cmdParse2Array(cfg.Command.Exec[i], cf)
2019-01-03 02:27:04 +00:00
log.Println("EXEC", carr)
2018-12-29 09:49:38 +00:00
t.cmd = exec.Command(carr[0], carr[1:]...)
//cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.CREATE_UNICODE_ENVIRONMENT}
t.cmd.Stdin = os.Stdin
//cmd.Stdout = os.Stdout
t.cmd.Stderr = os.Stderr
t.cmd.Dir = projectFolder
t.cmd.Env = os.Environ()
stdout, err := t.cmd.StdoutPipe()
if err != nil {
2019-01-19 09:10:07 +00:00
log.Println(PreError, err.Error())
2018-12-29 09:49:38 +00:00
return
}
2019-01-03 02:41:23 +00:00
err = t.cmd.Start()
if err != nil {
2019-01-19 09:10:07 +00:00
log.Println(PreError, "run command", carr, "error. ", err)
2019-01-03 02:41:23 +00:00
}
2018-12-29 09:49:38 +00:00
reader := bufio.NewReader(stdout)
for {
line, err2 := reader.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
}
2019-01-03 02:27:04 +00:00
fmt.Print(line)
2018-12-29 09:49:38 +00:00
}
err = t.cmd.Wait()
if err != nil {
2019-01-19 09:10:07 +00:00
log.Println(PreWarn, "cmd wait err ", err)
2018-12-29 09:49:38 +00:00
break
}
if t.cmd.Process != nil && t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
2019-01-03 02:41:23 +00:00
if err = t.cmd.Process.Kill(); err != nil {
2019-01-19 09:10:07 +00:00
log.Println(PreWarn, "cmd cannot kill, reason:", err)
2019-01-03 02:41:23 +00:00
}
2018-12-29 09:49:38 +00:00
}
}
log.Println("end ")
}