Skip to content

Commit

Permalink
Add Notifications error control logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Castell committed Jul 24, 2019
1 parent 179b766 commit 1eb69ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion dkron/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 13 additions & 7 deletions dkron/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions dkron/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1eb69ce

Please sign in to comment.