mirror of
https://github.com/nxshock/gron.git
synced 2024-11-27 03:41:00 +05:00
WIP: Add job HTTP callbacks
This commit is contained in:
parent
27108837d5
commit
b066901511
12
README.md
12
README.md
@ -32,7 +32,17 @@
|
|||||||
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
|
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
|
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
|
||||||
|
@ -17,6 +17,8 @@ type Config struct {
|
|||||||
LogFilePath string `default:"gron.log"` // core log file path
|
LogFilePath string `default:"gron.log"` // core log file path
|
||||||
LogFilesPath string `default:"logs"` // job log files path
|
LogFilesPath string `default:"logs"` // job log files path
|
||||||
HttpListenAddr string `default:"127.0.0.1:9876"`
|
HttpListenAddr string `default:"127.0.0.1:9876"`
|
||||||
|
|
||||||
|
HttpProxyAddr string // proxy address for local http client
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() error {
|
func initConfig() error {
|
||||||
|
@ -2,4 +2,7 @@ package main
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultConfigFilePath = "gron.conf"
|
defaultConfigFilePath = "gron.conf"
|
||||||
|
|
||||||
|
defaultOnSuccessMessageFmt = "Job {{.JobName}} finished."
|
||||||
|
defaultOnErrorMessageFmt = "Job {{.JobName}} failed:\n\n{{.Error}}"
|
||||||
)
|
)
|
||||||
|
62
job.go
62
job.go
@ -36,8 +36,17 @@ type JobConfig struct {
|
|||||||
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
|
||||||
|
|
||||||
|
OnSuccessMessageFmt string // Success message format
|
||||||
|
OnErrorMessageFmt string // Error message format
|
||||||
|
|
||||||
OnSuccessCmd string
|
OnSuccessCmd string
|
||||||
OnErrorCmd string
|
OnErrorCmd string
|
||||||
|
|
||||||
|
OnSuccessHttpPostUrl string
|
||||||
|
OnErrorHttpPostUrl string
|
||||||
|
|
||||||
|
OnSuccessHttpGetUrl string
|
||||||
|
OnErrorHttpGetUrl string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Job struct {
|
type Job struct {
|
||||||
@ -232,24 +241,71 @@ func (j *Job) runFinishCallback(err error) error {
|
|||||||
errStr = err.Error()
|
errStr = err.Error()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s = strings.ReplaceAll(s, "$ErrorText", errStr)
|
s = format(s, struct{ Error string }{Error: errStr})
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == nil && j.JobConfig.OnSuccessCmd != "" {
|
if err == nil && j.JobConfig.OnSuccessCmd != "" {
|
||||||
log.Println("success")
|
|
||||||
cmd, params := splitCommandAndParams(s)
|
cmd, params := splitCommandAndParams(s)
|
||||||
return runSimpleCmd(cmd, params...)
|
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 != "" {
|
if err != nil && j.JobConfig.OnErrorCmd != "" {
|
||||||
log.Println("error")
|
|
||||||
cmd, params := splitCommandAndParams(s)
|
cmd, params := splitCommandAndParams(s)
|
||||||
return runSimpleCmd(cmd, params...)
|
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
|
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 {
|
func runSimpleCmd(command string, args ...string) error {
|
||||||
log.Println(command)
|
log.Println(command)
|
||||||
log.Println(args)
|
log.Println(args)
|
||||||
|
13
strutils.go
Normal file
13
strutils.go
Normal file
@ -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()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user