fileboy/taskman.go

89 lines
1.8 KiB
Go
Raw Normal View History

2018-12-29 09:49:38 +00:00
package main
import (
"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) {
2019-06-04 02:52:58 +00:00
if t.cmd != nil && t.cmd.Process != nil {
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-06-04 02:58:42 +00:00
go t.notifier.Put(cf)
2018-12-29 09:49:38 +00:00
}
2019-01-03 01:56:26 +00:00
func (t *TaskMan) run(cf *changedFile) {
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
2019-12-27 10:09:56 +00:00
t.cmd.Stdout = os.Stdout
2018-12-29 09:49:38 +00:00
t.cmd.Stderr = os.Stderr
t.cmd.Dir = projectFolder
t.cmd.Env = os.Environ()
2019-12-27 10:09:56 +00:00
err := t.cmd.Start()
2019-01-03 02:41:23 +00:00
if err != nil {
2019-01-19 09:10:07 +00:00
log.Println(PreError, "run command", carr, "error. ", err)
2019-12-27 10:09:56 +00:00
break
2018-12-29 09:49:38 +00:00
}
err = t.cmd.Wait()
if err != nil {
2019-12-27 10:09:56 +00:00
log.Println(PreWarn, "command exec failed:", carr, err)
2018-12-29 09:49:38 +00:00
break
}
2019-06-04 03:12:41 +00:00
if t.cmd.Process != nil {
2019-12-27 10:09:56 +00:00
err := t.cmd.Process.Kill()
log.Println(t.cmd.ProcessState)
if t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
log.Println(PreError, "command cannot stop!", carr, err)
2019-01-03 02:41:23 +00:00
}
2018-12-29 09:49:38 +00:00
}
}
2019-12-27 10:09:56 +00:00
log.Println("EXEC end")
2018-12-29 09:49:38 +00:00
}