Skip to content

Commit

Permalink
Merge pull request #53 from atc0005/i52-add-support-for-contexts
Browse files Browse the repository at this point in the history
Implement context support in teams subpkg, app
  • Loading branch information
atc0005 authored Apr 19, 2020
2 parents 86e5b7c + 730b474 commit 44c4d77
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 13 deletions.
8 changes: 6 additions & 2 deletions cmd/send2teams/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package main

import (
"context"
"errors"
"flag"
"log"
Expand Down Expand Up @@ -79,9 +80,12 @@ func main() {
os.Exit(1)
}

ctxSubmissionTimeout, cancel := context.WithTimeout(context.Background(), config.TeamsSubmissionTimeout)
defer cancel()

// Submit message card, retry submission if needed up to specified number
// of retry attempts.
if err := teams.SendMessage(cfg.WebhookURL, msgCard, cfg.Retries, cfg.RetriesDelay); err != nil {
// of retry attempts or until context expires.
if err := teams.SendMessage(ctxSubmissionTimeout, cfg.WebhookURL, msgCard, cfg.Retries, cfg.RetriesDelay); err != nil {

// Display error output if silence is not requested
if !cfg.SilentOutput {
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

module github.com/atc0005/send2teams

// replace github.com/atc0005/go-teams-notify => ../go-teams-notify

require (
//gopkg.in/dasrick/go-teams-notify.v1 v1.2.0

// temporarily use our fork while developing changes for potential
// inclusion in the upstream project
github.com/atc0005/go-teams-notify v1.3.1-0.20200418112621-bff30feb673e
//
// temporarily use local copy instead of pinning to a specific commit in
// our test branch
//github.com/atc0005/go-teams-notify v0.0.0
github.com/atc0005/go-teams-notify v1.3.1-0.20200419155834-55cca556e726
github.com/davecgh/go-spew v1.1.1 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/atc0005/go-teams-notify v1.3.1-0.20200418112621-bff30feb673e h1:zRyZGkOWTLymKTp96PmPaOO1KIAtS0YBvAeTIKEodNs=
github.com/atc0005/go-teams-notify v1.3.1-0.20200418112621-bff30feb673e/go.mod h1:zUADEXrhalWyaQvxzYgHswljBWycIpX1UAFrggjcdi4=
github.com/atc0005/go-teams-notify v1.3.1-0.20200419155834-55cca556e726 h1:pUJFxj7XRR6UxgWTvG4BCf1cVsn7nNqxdigBhMd1r/c=
github.com/atc0005/go-teams-notify v1.3.1-0.20200419155834-55cca556e726/go.mod h1:zUADEXrhalWyaQvxzYgHswljBWycIpX1UAFrggjcdi4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
8 changes: 8 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/atc0005/send2teams/teams"
// temporarily use our fork while developing changes for potential
// inclusion in the upstream project
)

const (
Expand Down Expand Up @@ -54,6 +57,11 @@ var version string = "dev build"
const myAppName string = "send2teams"
const myAppURL string = "https://github.com/atc0005/" + myAppName

// TeamsSubmissionTimeout is the timeout value for sending messages to
// Microsoft Teams. This value is used to build a context with the desired
// timeout value.
const TeamsSubmissionTimeout time.Duration = 5 * time.Second

// Config is a unified set of configuration values for this application. This
// struct is configured via command-line flags provided by the user.
type Config struct {
Expand Down
52 changes: 48 additions & 4 deletions teams/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package teams

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -272,7 +273,9 @@ func ConvertEOLToBreak(s string) string {
// SendMessage is a wrapper function for setting up and using the
// goteamsnotify client to send a message card to Microsoft Teams via a
// webhook URL.
func SendMessage(webhookURL string, message goteamsnotify.MessageCard, retries int, retriesDelay int) error {
func SendMessage(ctx context.Context, webhookURL string, message goteamsnotify.MessageCard, retries int, retriesDelay int) error {

// NOTE: The caller is responsible for setting the desired context timeout

// init the client
mstClient := goteamsnotify.NewClient()
Expand All @@ -286,16 +289,57 @@ func SendMessage(webhookURL string, message goteamsnotify.MessageCard, retries i
// times before giving up
for attempt := 1; attempt <= attemptsAllowed; attempt++ {

// While the context is passed to mstClient.SendWithContext and it
// should ensure that it is respected, we check here at the start of
// the loop iteration (either first or subsequent) in order to return
// early in an effort to prevent undesired message attempts
if ctx.Err() != nil {
msg := fmt.Sprintf(
"SendMessage: context cancelled or expired: %v; aborting message submission after %d of %d attempts",
ctx.Err().Error(),
attempt,
attemptsAllowed,
)

// if this is set, we're looking at the second (incomplete)
// iteration
if result != nil {
msg += ": " + result.Error()
}

logger.Println(msg)
return fmt.Errorf(msg)
}

// the result from the last attempt is returned to the caller
result = mstClient.Send(webhookURL, message)
result = mstClient.SendWithContext(ctx, webhookURL, message)
if result != nil {
logger.Printf("SendMessage: Attempt %d of %d to send messaged failed: %v",

// check context again?
if ctx.Err() != nil {
errMsg := fmt.Errorf(
"SendMessage: Attempt %d of %d to send message failed: %v",
attempt,
attemptsAllowed,
result,
)
logger.Println(errMsg.Error())

return errMsg
}

// logger.Printf("Type of err: %T\n", result)
logger.Printf("SendMessage: Attempt %d of %d to send message failed: %v\n",
attempt, attemptsAllowed, result)
time.Sleep(time.Duration(retriesDelay) * time.Second)
continue
}

logger.Println("SendMessage: successfully sent message")
logger.Printf(
"SendMessage: successfully sent message after %d of %d attempts\n",
attempt,
attemptsAllowed,
)
break
}

Expand Down
49 changes: 46 additions & 3 deletions vendor/github.com/atc0005/go-teams-notify/send.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# github.com/atc0005/go-teams-notify v1.3.1-0.20200418112621-bff30feb673e
# github.com/atc0005/go-teams-notify v1.3.1-0.20200419155834-55cca556e726
github.com/atc0005/go-teams-notify

0 comments on commit 44c4d77

Please sign in to comment.