Skip to content

Commit

Permalink
heartbeat support
Browse files Browse the repository at this point in the history
  • Loading branch information
Snawoot committed Oct 24, 2021
1 parent d4e7297 commit fd56604
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmd/everssl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/mysteriumnetwork/everssl/reporter"
"github.com/mysteriumnetwork/everssl/validator"
"github.com/mysteriumnetwork/everssl/validator/result"
"github.com/mysteriumnetwork/everssl/heartbeat"
)

var version = "undefined"
Expand All @@ -38,6 +39,8 @@ var (
ignoreExpirationErrors = flag.Bool("ignore-expiration-errors", false, "ignore expiration errors")

pagerDutyKey = flag.String("pagerduty-key", "", "PagerDuty Events V2 integration key")

heartbeatURL = flag.String("heartbeat-url", "", "heartbeat URL, URL to GET after successful finish")
)

func run() int {
Expand Down Expand Up @@ -66,6 +69,13 @@ func run() int {
}
}

if *heartbeatURL == "" {
envToken := os.Getenv("HEARTBEAT_URL")
if envToken != "" {
*heartbeatURL = envToken
}
}

if *zoneName == "" {
log.Fatal("zone is not specified")
}
Expand Down Expand Up @@ -144,6 +154,14 @@ func run() int {
log.Fatal(err)
}

if *heartbeatURL != "" {
var beat heartbeat.Heartbeat = heartbeat.NewURLHeartbeat(*heartbeatURL)
err = beat.Beat(ctx)
if err != nil {
log.Fatal(err)
}
}

return 0
}

Expand Down
9 changes: 9 additions & 0 deletions heartbeat/heartbeat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package heartbeat

import (
"context"
)

type Heartbeat interface {
Beat(context.Context) error
}
46 changes: 46 additions & 0 deletions heartbeat/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package heartbeat

import (
"context"
"io"
"io/ioutil"
"net/http"
)

const discardLimit int64 = 128 * 1024

type URLHeartbeat struct {
url string
}

func NewURLHeartbeat(url string) *URLHeartbeat {
return &URLHeartbeat{
url: url,
}
}

func (b *URLHeartbeat) Beat(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, "GET", b.url, nil)
if err != nil {
return err
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer cleanupBody(resp.Body)

return nil
}

// Does cleanup of HTTP response in order to make it reusable by keep-alive
// logic of HTTP client
func cleanupBody(body io.ReadCloser) {
io.Copy(ioutil.Discard, &io.LimitedReader{
R: body,
N: discardLimit,
})
body.Close()
}

0 comments on commit fd56604

Please sign in to comment.