This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
forked from webdevops/go-crond
-
Notifications
You must be signed in to change notification settings - Fork 3
/
runner.go
148 lines (121 loc) · 3.13 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
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
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"github.com/robfig/cron"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"syscall"
"time"
)
type Runner struct {
cron *cron.Cron
}
func NewRunner() *Runner {
r := &Runner{
cron: cron.New(),
}
return r
}
// Add crontab entry
func (r *Runner) Add(cronjob CrontabEntry) error {
cronSpec := cronjob.Spec
if !strings.HasPrefix(cronjob.Spec, "@") {
cronSpec = fmt.Sprintf("%s", cronjob.Spec)
}
_, err := r.cron.AddFunc(cronSpec, r.cmdFunc(cronjob, func(execCmd *exec.Cmd) bool {
// before exec callback
LoggerInfo.CronjobExec(cronjob)
return true
}))
if err != nil {
LoggerError.Printf("Failed add cron job spec:%v cmd:%v err:%v", cronjob.Spec, cronjob.Command, err)
} else {
LoggerInfo.CronjobAdd(cronjob)
}
return err
}
// Add crontab entry with user
func (r *Runner) AddWithUser(cronjob CrontabEntry) error {
cronSpec := cronjob.Spec
if !strings.HasPrefix(cronjob.Spec, "@") {
cronSpec = fmt.Sprintf("%s", cronjob.Spec)
}
_, err := r.cron.AddFunc(cronSpec, r.cmdFunc(cronjob, func(execCmd *exec.Cmd) bool {
// before exec callback
LoggerInfo.CronjobExec(cronjob)
// lookup username
u, err := user.Lookup(cronjob.User)
if err != nil {
LoggerError.Printf("user lookup failed: %v", err)
return false
}
// convert userid to int
userId, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
LoggerError.Printf("Cannot convert user to id:%v", err)
return false
}
// convert groupid to int
groupId, err := strconv.ParseUint(u.Gid, 10, 32)
if err != nil {
LoggerError.Printf("Cannot convert group to id:%v", err)
return false
}
// add process credentials
execCmd.SysProcAttr = &syscall.SysProcAttr{}
execCmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(userId), Gid: uint32(groupId)}
return true
}))
if err != nil {
LoggerError.Printf("Failed add cron job %v; Error:%v", LoggerError.CronjobToString(cronjob), err)
} else {
LoggerInfo.Printf("Add cron job %v", LoggerError.CronjobToString(cronjob))
}
return err
}
// Return number of jobs
func (r *Runner) Len() int {
return len(r.cron.Entries())
}
// Start runner
func (r *Runner) Start() {
LoggerInfo.Printf("Start runner with %d jobs\n", r.Len())
r.cron.Start()
}
// Stop runner
func (r *Runner) Stop() {
r.cron.Stop()
LoggerInfo.Println("Stop runner")
}
// Execute crontab command
func (r *Runner) cmdFunc(cronjob CrontabEntry, cmdCallback func(*exec.Cmd) bool) func() {
cmdFunc := func() {
// fall back to normal shell if not specified
taskShell := cronjob.Shell
if taskShell == "" {
taskShell = DEFAULT_SHELL
}
start := time.Now()
// Init command
execCmd := exec.Command(taskShell, "-c", cronjob.Command)
// add custom env to cronjob
if len(cronjob.Env) >= 1 {
execCmd.Env = append(os.Environ(), cronjob.Env...)
}
// exec custom callback
if cmdCallback(execCmd) {
// exec job
out, err := execCmd.CombinedOutput()
elapsed := time.Since(start)
if err != nil {
LoggerError.CronjobExecFailed(cronjob, string(out), err, elapsed)
} else {
LoggerInfo.CronjobExecSuccess(cronjob, string(out), err, elapsed)
}
}
}
return cmdFunc
}