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

Show http get/post errors in logs

This commit is contained in:
nxshock 2022-11-05 14:07:55 +05:00
parent 6fe7019fb3
commit 733d5f28f1
3 changed files with 29 additions and 20 deletions

View File

@ -61,7 +61,7 @@ func httpGet(addrFmt, jobName, text string) error {
if err != nil {
return err
}
defer resp.Body.Close() // TODO: нужно ли закрывать Body при наличии ошибки?
defer resp.Body.Close()
return nil
}

45
job.go
View File

@ -111,7 +111,7 @@ func splitCommandAndParams(s string) (command string, params []string) {
// jobLogFile - job log file with needs to be closed after job is done
func (j *Job) openAndMergeLog() (logEntry *log.Entry, jobLogFile *os.File) {
jobLogFile, _ = os.OpenFile(filepath.Join(config.LogFilesPath, j.Name+".log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) // TODO: handle error
jobLogFile.WriteString("\n")
_, _ = jobLogFile.WriteString("\n")
logWriter := io.MultiWriter(mainLogFile, jobLogFile)
@ -185,7 +185,7 @@ func (j *Job) runTry(log *log.Entry, jobLogFile *os.File) error {
j.LastError = ""
globalMutex.Unlock()
}
go j.runFinishCallback(err)
go j.runFinishCallback(log, err)
endTime := time.Now()
log.Infof("Finished (%s).", endTime.Sub(startTime).Truncate(time.Second).String())
@ -234,44 +234,53 @@ func (j *Job) runSql(jobLogFile *os.File) error {
return nil
}
func (j *Job) runFinishCallback(err error) error {
func (j *Job) runFinishCallback(log *log.Entry, jobErr error) error {
s := j.JobConfig.OnSuccessCmd
// Fill variables
errStr := ""
if err != nil {
errStr = err.Error()
if jobErr != nil {
errStr = jobErr.Error()
}
if err != nil {
if jobErr != nil {
s = format(s, struct{ Error string }{Error: errStr})
}
if err == nil && j.JobConfig.OnSuccessCmd != "" {
if jobErr == nil && j.JobConfig.OnSuccessCmd != "" {
cmd, params := splitCommandAndParams(s)
return runSimpleCmd(cmd, params...)
}
if err == nil && j.JobConfig.OnSuccessHttpPostUrl != "" {
httpPost(j.JobConfig.OnSuccessHttpPostUrl, j.successMessage()) // TODO: обработать ошибку
if jobErr == nil && j.JobConfig.OnSuccessHttpPostUrl != "" {
err := httpPost(j.JobConfig.OnSuccessHttpPostUrl, j.successMessage())
if err != nil {
log.Error(err)
}
}
if err == nil && j.JobConfig.OnSuccessHttpGetUrl != "" {
httpGet(j.JobConfig.OnSuccessHttpPostUrl, j.Name, j.successMessage()) // TODO: обработать ошибку
if jobErr == nil && j.JobConfig.OnSuccessHttpGetUrl != "" {
err := httpGet(j.JobConfig.OnSuccessHttpPostUrl, j.Name, j.successMessage())
if err != nil {
log.Error(err)
}
}
if err != nil && j.JobConfig.OnErrorCmd != "" {
if jobErr != nil && j.JobConfig.OnErrorCmd != "" {
cmd, params := splitCommandAndParams(s)
return runSimpleCmd(cmd, params...)
}
if err != nil && j.JobConfig.OnErrorHttpPostUrl != "" {
httpPost(j.JobConfig.OnErrorHttpPostUrl, j.errorMessage(err)) // TODO: обработать ошибку
if jobErr != nil && j.JobConfig.OnErrorHttpPostUrl != "" {
err := httpPost(j.JobConfig.OnErrorHttpPostUrl, j.errorMessage(jobErr))
if err != nil {
log.Error(err)
}
}
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: сделать формат сообщения по стандарту
if jobErr != nil && j.JobConfig.OnErrorHttpGetUrl != "" {
err := httpGet(j.JobConfig.OnErrorHttpGetUrl, j.Name, jobErr.Error())
if err != nil {
log.Errorf("OnErrorHttpGetUrl error: %v", err) // TODO: сделать формат сообщения по стандарту
}
}

View File

@ -94,6 +94,6 @@ func main() {
err = s.Run()
if err != nil {
logger.Error(err)
_ = logger.Error(err)
}
}