1
0
mirror of https://github.com/nxshock/gron.git synced 2024-11-27 03:41:00 +05:00

Add OnErrorCmd and OnSuccessCmd callbacks

This commit is contained in:
nxshock 2022-06-12 13:34:25 +05:00
parent 2886bd2755
commit 27108837d5
2 changed files with 48 additions and 3 deletions

View File

@ -30,6 +30,9 @@
NumberOfRestartAttemts = 3 # number of restart attemts NumberOfRestartAttemts = 3 # number of restart attemts
RestartSec = 5 # the time to sleep before restarting a job (seconds) RestartSec = 5 # the time to sleep before restarting a job (seconds)
RestartRule = "on-error" # Configures whether the job shall be restarted when the job process exits RestartRule = "on-error" # Configures whether the job shall be restarted when the job process exits
OnSuccessCmd = "echo 'Job finished.'" # execute cmd on job success
OnErrorCmd = "echo 'Error occurred: $ErrorText'" # execute cmd on job error
``` ```
3. Launch `gron` binary 3. Launch `gron` binary
4. HTTP interface available on http://127.0.0.1:9876 4. HTTP interface available on http://127.0.0.1:9876

48
job.go
View File

@ -35,6 +35,9 @@ type JobConfig struct {
NumberOfRestartAttemts int NumberOfRestartAttemts int
RestartSec int // the time to sleep before restarting a job (seconds) RestartSec int // the time to sleep before restarting a job (seconds)
RestartRule RestartRule // Configures whether the job shall be restarted when the job process exits RestartRule RestartRule // Configures whether the job shall be restarted when the job process exits
OnSuccessCmd string
OnErrorCmd string
} }
type Job struct { type Job struct {
@ -78,9 +81,9 @@ func readJob(filePath string) (*Job, error) {
return job, nil return job, nil
} }
func (j *Job) commandAndParams() (command string, params []string) { func splitCommandAndParams(s string) (command string, params []string) {
quoted := false quoted := false
items := strings.FieldsFunc(j.JobConfig.Command, func(r rune) bool { items := strings.FieldsFunc(s, func(r rune) bool {
if r == '"' { if r == '"' {
quoted = !quoted quoted = !quoted
} }
@ -171,6 +174,7 @@ func (j *Job) runTry(log *log.Entry, jobLogFile *os.File) error {
j.LastError = "" j.LastError = ""
globalMutex.Unlock() globalMutex.Unlock()
} }
go j.runFinishCallback(err)
endTime := time.Now() endTime := time.Now()
log.Infof("Finished (%s).", endTime.Sub(startTime).Truncate(time.Second).String()) log.Infof("Finished (%s).", endTime.Sub(startTime).Truncate(time.Second).String())
@ -185,7 +189,7 @@ func (j *Job) runTry(log *log.Entry, jobLogFile *os.File) error {
} }
func (j *Job) runCmd(jobLogFile *os.File) error { func (j *Job) runCmd(jobLogFile *os.File) error {
command, params := j.commandAndParams() command, params := splitCommandAndParams(j.JobConfig.Command)
cmd := exec.Command(command, params...) cmd := exec.Command(command, params...)
cmd.Stdout = jobLogFile cmd.Stdout = jobLogFile
@ -218,3 +222,41 @@ func (j *Job) runSql(jobLogFile *os.File) error {
return nil return nil
} }
func (j *Job) runFinishCallback(err error) error {
s := j.JobConfig.OnSuccessCmd
// Fill variables
errStr := ""
if err != nil {
errStr = err.Error()
}
if err != nil {
s = strings.ReplaceAll(s, "$ErrorText", errStr)
}
if err == nil && j.JobConfig.OnSuccessCmd != "" {
log.Println("success")
cmd, params := splitCommandAndParams(s)
return runSimpleCmd(cmd, params...)
}
if err != nil && j.JobConfig.OnErrorCmd != "" {
log.Println("error")
cmd, params := splitCommandAndParams(s)
return runSimpleCmd(cmd, params...)
}
return nil
}
func runSimpleCmd(command string, args ...string) error {
log.Println(command)
log.Println(args)
err := exec.Command(command, args...).Run()
if err != nil {
log.Println(">>", err)
}
return err
}