This repository has been archived by the owner on Nov 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathrun.go
84 lines (78 loc) · 1.7 KB
/
run.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
package main
import (
"bufio"
"os"
"os/exec"
"strconv"
"time"
)
/*
* 任务执行
* 开始 结束 日志
*/
var configJobs *Jobs = NewJobs()
var runningJobs *Jobs = NewJobs()
func jobHandle() {
tick := time.NewTicker(time.Second)
for {
select {
case <-stopCh:
tick.Stop()
sysLog.Println("Stop crontab")
case <-startCh:
tick = time.NewTicker(time.Second)
sysLog.Println("Start crontab")
case <-tick.C:
go configJobs.runJobs()
}
}
}
func runJob(j job) {
cmd := exec.Command(j.Cmd, j.Args...)
outpipe, outErr := cmd.StdoutPipe()
if outErr != nil {
runLog.lg.Printf("[Err] %s %s %s %s\n", j.Cmd, j.Args, j.Out, outErr)
}
startErr := cmd.Start()
if startErr != nil {
runLog.lg.Printf("[Err] %s %s %s %s\n", j.Cmd, j.Args, j.Out, startErr)
return
}
pid := cmd.Process.Pid
spid := strconv.Itoa(pid)
j.Start = time.Now().Format(TIMEFORMAT)
runningJobs.add(spid, &j)
defer func() {
runningJobs.del(spid)
runLog.lg.Printf("[End] pid.%d %s %s %s\n", pid, j.Cmd, j.Args, j.Out)
}()
runLog.lg.Printf("[Start] pid.%d %s %s %s\n", pid, j.Cmd, j.Args, j.Out)
if j.Out != "" {
of, ofErr := os.OpenFile(j.Out, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if ofErr != nil {
runLog.lg.Printf("[Err] pid.%d %s %s %s %s", pid, j.Cmd, j.Args, j.Out, ofErr)
} else {
defer of.Close()
outrd := bufio.NewReader(outpipe)
outrd.WriteTo(of)
}
}
waitErr := cmd.Wait()
if waitErr != nil {
runLog.lg.Printf("[Err] pid.%d %s %s %s %s\n", pid, j.Cmd, j.Args, j.Out, waitErr)
}
}
func inArray(array []int, item int) bool {
if len(array) < 1 {
return false
}
if array[0] == -1 {
return true
}
for _, v := range array {
if item == v {
return true
}
}
return false
}