1
0
Fork 0
mirror of https://github.com/Luzifer/elastic_cron.git synced 2024-12-23 02:41:20 +00:00

Add HTTP-GET ping for success / failure

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-09-13 13:12:02 +02:00
parent d425a78077
commit d4ca79b9fe
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

50
main.go
View file

@ -4,21 +4,23 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"time" "time"
"gopkg.in/yaml.v2"
"github.com/Luzifer/rconfig" "github.com/Luzifer/rconfig"
"github.com/robfig/cron" "github.com/robfig/cron"
"golang.org/x/net/context"
"gopkg.in/yaml.v2"
) )
var ( var (
cfg = struct { cfg = struct {
ConfigFile string `flag:"config" default:"config.yaml" description:"Cron definition file"` ConfigFile string `flag:"config" default:"config.yaml" description:"Cron definition file"`
Hostname string `flag:"hostname" description:"Overwrite system hostname"` Hostname string `flag:"hostname" description:"Overwrite system hostname"`
PingTimout time.Duration `flag:"ping-timeout" default:"1s" description:"Timeout for success / failure pings"`
}{} }{}
version = "dev" version = "dev"
@ -36,6 +38,8 @@ type cronJob struct {
Schedule string `yaml:"schedule"` Schedule string `yaml:"schedule"`
Command string `yaml:"cmd"` Command string `yaml:"cmd"`
Arguments []string `yaml:"args"` Arguments []string `yaml:"args"`
PingSuccess string `yaml:"ping_success"`
PingFailure string `yaml:"ping_failure"`
} }
func init() { func init() {
@ -102,14 +106,56 @@ func getJobExecutor(job cronJob) func() {
switch err.(type) { switch err.(type) {
case nil: case nil:
fmt.Fprintln(stdout, "[SYS] Command execution successful") fmt.Fprintln(stdout, "[SYS] Command execution successful")
go func(url string) {
if err := doPing(url); err != nil {
fmt.Fprintf(stderr, "[SYS] Ping to URL %q caused an error: %s", url, err)
}
}(job.PingSuccess)
case *exec.ExitError: case *exec.ExitError:
fmt.Fprintln(stderr, "[SYS] Command exited with unexpected exit code != 0") fmt.Fprintln(stderr, "[SYS] Command exited with unexpected exit code != 0")
go func(url string) {
if err := doPing(url); err != nil {
fmt.Fprintf(stderr, "[SYS] Ping to URL %q caused an error: %s", url, err)
}
}(job.PingFailure)
default: default:
fmt.Fprintf(stderr, "[SYS] Execution caused error: %s\n", err) fmt.Fprintf(stderr, "[SYS] Execution caused error: %s\n", err)
go func(url string) {
if err := doPing(url); err != nil {
fmt.Fprintf(stderr, "[SYS] Ping to URL %q caused an error: %s", url, err)
}
}(job.PingFailure)
} }
} }
} }
func doPing(url string) error {
if url == "" {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), cfg.PingTimout)
defer cancel()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
if resp.StatusCode > 299 {
return fmt.Errorf("Expected HTTP2xx status, got HTTP%d", resp.StatusCode)
}
return nil
}
type messageChanWriter struct { type messageChanWriter struct {
jobName string jobName string
msgChan chan *message msgChan chan *message