From b06690151179dbd930ed722fb1088236361cb5c6 Mon Sep 17 00:00:00 2001 From: nxshock Date: Tue, 21 Jun 2022 22:14:50 +0500 Subject: [PATCH] WIP: Add job HTTP callbacks --- README.md | 12 ++++++++++- config.go | 2 ++ consts.go | 3 +++ job.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++--- strutils.go | 13 +++++++++++ 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 strutils.go diff --git a/README.md b/README.md index 0194389..b4c16ea 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,17 @@ 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 + OnErrorCmd = "echo 'Error occurred: {{.Error}}'" # execute cmd on job error + + + OnSuccessHttpGetUrl = "" + OnErrorHttpGetUrl = "http://127.0.0.1/alerts?title={{.JobName}}%20failed&message={{.Error}}&tags=warning" + + OnSuccessHttpPostUrl = "http://127.0.0.1/alerts" + OnSuccessMessageFmt = "Job {{.JobName}} finished." + + OnErrorHttpPostUrl = "http://127.0.0.1/alerts" + OnErrorMessageFmt = "Job {{.JobName}} failed:\n\n{{.Error}}" ``` 3. Launch `gron` binary 4. HTTP interface available on http://127.0.0.1:9876 diff --git a/config.go b/config.go index 11b7180..ecc2f9e 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,8 @@ type Config struct { LogFilePath string `default:"gron.log"` // core log file path LogFilesPath string `default:"logs"` // job log files path HttpListenAddr string `default:"127.0.0.1:9876"` + + HttpProxyAddr string // proxy address for local http client } func initConfig() error { diff --git a/consts.go b/consts.go index 5b9b19d..02e56f8 100644 --- a/consts.go +++ b/consts.go @@ -2,4 +2,7 @@ package main const ( defaultConfigFilePath = "gron.conf" + + defaultOnSuccessMessageFmt = "Job {{.JobName}} finished." + defaultOnErrorMessageFmt = "Job {{.JobName}} failed:\n\n{{.Error}}" ) diff --git a/job.go b/job.go index 682b36a..f4c0dec 100644 --- a/job.go +++ b/job.go @@ -36,8 +36,17 @@ type JobConfig struct { 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 + OnSuccessMessageFmt string // Success message format + OnErrorMessageFmt string // Error message format + OnSuccessCmd string OnErrorCmd string + + OnSuccessHttpPostUrl string + OnErrorHttpPostUrl string + + OnSuccessHttpGetUrl string + OnErrorHttpGetUrl string } type Job struct { @@ -232,24 +241,71 @@ func (j *Job) runFinishCallback(err error) error { errStr = err.Error() } if err != nil { - s = strings.ReplaceAll(s, "$ErrorText", errStr) + s = format(s, struct{ Error string }{Error: errStr}) } if err == nil && j.JobConfig.OnSuccessCmd != "" { - log.Println("success") cmd, params := splitCommandAndParams(s) return runSimpleCmd(cmd, params...) } + if err == nil && j.JobConfig.OnSuccessHttpPostUrl != "" { + httpPost(j.JobConfig.OnSuccessHttpPostUrl, j.successMessage()) // TODO: обработать ошибку + } + + if err == nil && j.JobConfig.OnSuccessHttpGetUrl != "" { + httpGet(j.JobConfig.OnSuccessHttpPostUrl, j.Name, j.successMessage()) // TODO: обработать ошибку + } + if err != nil && j.JobConfig.OnErrorCmd != "" { - log.Println("error") cmd, params := splitCommandAndParams(s) return runSimpleCmd(cmd, params...) } + if err != nil && j.JobConfig.OnErrorHttpPostUrl != "" { + httpPost(j.JobConfig.OnErrorHttpPostUrl, j.errorMessage(err)) // TODO: обработать ошибку + } + + if err != nil && j.JobConfig.OnErrorHttpGetUrl != "" { + err2 := httpGet(j.JobConfig.OnErrorHttpGetUrl, j.Name, err.Error()) + if err2 != nil { + log.Errorf("OnErrorHttpGetUrl error: %v", err2) // TODO: сделать формат сообщения по стандарту + } + } + return nil } +func (j *Job) successMessage() string { + s := j.JobConfig.OnSuccessMessageFmt + if s == "" { + s = defaultOnSuccessMessageFmt + } + + v := struct { + JobName string + }{ + JobName: j.Name} + + return format(s, v) +} + +func (j *Job) errorMessage(err error) string { + s := j.JobConfig.OnErrorMessageFmt + if s == "" { + s = defaultOnErrorMessageFmt + } + + v := struct { + JobName string + Error string + }{ + JobName: j.Name, + Error: err.Error()} + + return format(s, v) +} + func runSimpleCmd(command string, args ...string) error { log.Println(command) log.Println(args) diff --git a/strutils.go b/strutils.go new file mode 100644 index 0000000..994d16f --- /dev/null +++ b/strutils.go @@ -0,0 +1,13 @@ +package main + +import ( + "strings" + "text/template" +) + +func format(fmt string, v interface{}) string { + t := new(template.Template) + b := new(strings.Builder) + template.Must(t.Parse(fmt)).Execute(b, v) // TODO: обработать возможные ошибки + return b.String() +}