This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunner.go
102 lines (90 loc) Β· 2.16 KB
/
runner.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
// KeepMeBot - executor
// 2020-08-20 21:57
// Benny <[email protected]>
package main
// sanitize is necessary
// currently supported command list
import (
"bytes"
"fmt"
"math"
"net/http"
"os/exec"
"time"
log "github.com/sirupsen/logrus"
)
func scheduler() {
log.Debugln("Start scheduler")
var tasks []Queue
var executeTasks []Queue
DB.Find(&tasks)
log.Debugf("Total prepare tasks count: %d", len(tasks))
for _, item := range tasks {
var h History
var s Service
DB.Where("service_id=?", item.ServiceID).
Order("created_at desc").First(&h)
DB.Find(&h).Related(&s)
if time.Now().Sub(h.CreatedAt) > time.Duration(s.Interval*math.Pow10(9)) ||
h.UserID == 0 {
log.Debugf("Add %s(%v):%s to queue...", item.UserName, item.UserID, item.Command)
executeTasks = append(executeTasks, item)
}
}
log.Debugf("Total execute tasks count: %d", len(executeTasks))
for i, v := range executeTasks {
log.Infof("Executing [%d/%d]", i+1, len(tasks))
var message string
var s Service
DB.Model(&v).Related(&s)
switch s.ServiceType {
case "internal":
message = internalExecutor(v)
case "external":
message = externalExecutor(v)
default:
log.Warningln("404")
}
historyRecorder(v, message)
}
}
func externalExecutor(q Queue) string {
log.Infof("External Job for %s(%v):%s", q.UserName, q.UserID, q.Command)
var message string
cmd := exec.Command("sh", "-c", q.Command)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
message = fmt.Sprintf("%s %s", err, stderr.String())
log.Warningln(message)
} else {
message = out.String()
log.Debugf(message)
}
return message
}
func internalExecutor(q Queue) (s string) {
log.Infof("Internal Job for %s(%v):%s", q.UserName, q.UserID, q.Command)
var ser Service
DB.Model(&q).Related(&ser)
switch ser.Name {
case "get":
s = get(q.Parameter)
default:
log.Warningln("Internal job not found.")
}
return
}
func get(url string) (msg string) {
resp, err := http.Get(url)
if err != nil {
msg = err.Error()
} else {
msg = fmt.Sprintf("http status code is %d", resp.StatusCode)
_ = resp.Body.Close()
}
return
}