-
Notifications
You must be signed in to change notification settings - Fork 69
/
taskman.go
140 lines (130 loc) · 3.1 KB
/
taskman.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2018-2022 Author dengsgo<dengsgo@yoytang.com> [https://github.com/dengsgo/fileboy]
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
package main
import (
"os"
"os/exec"
"sync"
"time"
)
type TaskMan struct {
lastTaskId int64
delay int
cmd *exec.Cmd
notifier *NetNotifier
putLock sync.Mutex
runLock sync.Mutex
waitChan chan bool
waitQueue []*changedFile
}
func newTaskMan(delay int, callUrl string) *TaskMan {
t := &TaskMan{
delay: delay,
notifier: newNetNotifier(callUrl),
waitChan: make(chan bool, 1),
waitQueue: []*changedFile{},
}
if keyInInstruction(InstShouldFinish) {
go func() {
for {
<-t.waitChan
if len(t.waitQueue) < 1 {
continue // thanks to ehlxr <https://github.com/ehlxr> for the bug
}
cf := t.waitQueue[len(t.waitQueue)-1]
if len(t.waitQueue) > 1 {
logInfo("redundant tasks dropped:", len(t.waitQueue)-1)
}
t.waitQueue = []*changedFile{}
go t.preRun(cf)
}
}()
}
return t
}
func (t *TaskMan) Put(cf *changedFile) {
if t.delay < 1 {
t.dispatcher(cf)
return
}
t.putLock.Lock()
defer t.putLock.Unlock()
t.lastTaskId = cf.Changed
go func() {
<-time.After(time.Millisecond * time.Duration(t.delay))
if t.lastTaskId > cf.Changed {
return
}
t.dispatcher(cf)
}()
}
func (t *TaskMan) dispatcher(cf *changedFile) {
if keyInInstruction(InstShouldFinish) {
t.waitQueue = append(t.waitQueue, cf)
if t.cmd == nil {
t.waitChan <- true
return
}
logInfo("waitting for the last task to finish")
logInfo("waiting tasks:", len(t.waitQueue))
} else {
t.preRun(cf)
}
}
func (t *TaskMan) preRun(cf *changedFile) {
if t.cmd != nil && t.cmd.Process != nil {
if err := t.cmd.Process.Kill(); err != nil {
logInfo("stop old process ")
logWarn("stopped err, reason:", err)
}
}
go t.run(cf)
go t.notifier.Put(cf)
}
func (t *TaskMan) run(cf *changedFile) {
t.runLock.Lock()
defer t.runLock.Unlock()
for i := 0; i < len(cfg.Command.Exec); i++ {
carr := cmdParse2Array(cfg.Command.Exec[i], cf)
logInfo("EXEC", carr)
t.cmd = exec.Command(carr[0], carr[1:]...)
//cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.CREATE_UNICODE_ENVIRONMENT}
t.cmd.Stdin = os.Stdin
t.cmd.Stdout = os.Stdout
if keyInInstruction(InstIgnoreStdout) {
t.cmd.Stdout = nil
}
t.cmd.Stderr = os.Stderr
t.cmd.Dir = projectFolder
t.cmd.Env = os.Environ()
err := t.cmd.Start()
if err != nil {
logError("run command", carr, "error. ", err)
if keyInInstruction(InstIgnoreExecError) {
continue
}
break
}
err = t.cmd.Wait()
if err != nil {
logError("command exec failed:", carr, err)
if keyInInstruction(InstIgnoreExecError) {
continue
}
break
}
if t.cmd.Process != nil {
err := t.cmd.Process.Kill()
logInfo(t.cmd.ProcessState)
if t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
logError("command cannot stop!", carr, err)
}
}
}
if keyInInstruction(InstShouldFinish) {
t.cmd = nil
t.waitChan <- true
}
logInfo("EXEC end")
}