diff --git a/dkron/grpc.go b/dkron/grpc.go index d23dcb971..a98669fc1 100644 --- a/dkron/grpc.go +++ b/dkron/grpc.go @@ -208,7 +208,9 @@ func (grpcs *GRPCServer) ExecutionDone(ctx context.Context, execDoneReq *proto.E } // Send notification - Notification(grpcs.agent.config, &execution, exg, job).Send() + if err := Notification(grpcs.agent.config, &execution, exg, job).Send(); err != nil { + return nil, err + } // Jobs that have dependent jobs are a bit more expensive because we need to call the Status() method for every execution. // Check first if there's dependent jobs and then check for the job status to begin execution dependent jobs on success. diff --git a/dkron/notifier.go b/dkron/notifier.go index 9552bd895..03396e98a 100644 --- a/dkron/notifier.go +++ b/dkron/notifier.go @@ -33,13 +33,15 @@ func Notification(config *Config, execution *Execution, exGroup []*Execution, jo } // Send sends the notifications using any configured method -func (n *Notifier) Send() { +func (n *Notifier) Send() error { if n.Config.MailHost != "" && n.Config.MailPort != 0 && n.Job.OwnerEmail != "" { - n.sendExecutionEmail() + return n.sendExecutionEmail() } if n.Config.WebhookURL != "" && n.Config.WebhookPayload != "" { - n.callExecutionWebhook() + return n.callExecutionWebhook() } + + return nil } func (n *Notifier) report() string { @@ -97,7 +99,7 @@ func (n *Notifier) buildTemplate(templ string) *bytes.Buffer { return out } -func (n *Notifier) sendExecutionEmail() { +func (n *Notifier) sendExecutionEmail() error { var data *bytes.Buffer if n.Config.MailPayload != "" { data = n.buildTemplate(n.Config.MailPayload) @@ -114,8 +116,10 @@ func (n *Notifier) sendExecutionEmail() { serverAddr := fmt.Sprintf("%s:%d", n.Config.MailHost, n.Config.MailPort) if err := e.Send(serverAddr, n.auth()); err != nil { - log.WithError(err).Error("notifier: Error sending email") + return fmt.Errorf("notifier: Error sending email %s", err) } + + return nil } func (n *Notifier) auth() smtp.Auth { @@ -128,7 +132,7 @@ func (n *Notifier) auth() smtp.Auth { return auth } -func (n *Notifier) callExecutionWebhook() { +func (n *Notifier) callExecutionWebhook() error { out := n.buildTemplate(n.Config.WebhookPayload) req, err := http.NewRequest("POST", n.Config.WebhookURL, out) for _, h := range n.Config.WebhookHeaders { @@ -141,7 +145,7 @@ func (n *Notifier) callExecutionWebhook() { client := &http.Client{} resp, err := client.Do(req) if err != nil { - log.WithError(err).Error("notifier: Error posting notification") + return fmt.Errorf("notifier: Error posting notification: %s", err) } defer resp.Body.Close() @@ -151,6 +155,8 @@ func (n *Notifier) callExecutionWebhook() { "header": resp.Header, "body": string(body), }).Debug("notifier: Webhook call response") + + return nil } func (n *Notifier) statusString(execution *Execution) string { diff --git a/dkron/notifier_test.go b/dkron/notifier_test.go index 4048261b2..b767a18db 100644 --- a/dkron/notifier_test.go +++ b/dkron/notifier_test.go @@ -25,7 +25,7 @@ func TestNotifier_callExecutionWebhook(t *testing.T) { n := Notification(c, &Execution{}, []*Execution{}, &Job{}) - n.Send() + assert.NoError(t, n.Send()) } func TestNotifier_sendExecutionEmail(t *testing.T) { @@ -62,7 +62,7 @@ func TestNotifier_sendExecutionEmail(t *testing.T) { } n := Notification(c, ex1, exg, job) - n.Send() + assert.NoError(t, n.Send()) } func Test_auth(t *testing.T) {