Skip to content

Commit

Permalink
background-api: Add Healthchecks.io ping
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Aug 12, 2024
1 parent 44eee35 commit 36e320d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
44 changes: 44 additions & 0 deletions internal/healthchecksio/healthchecks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Package healthchecksio is a wrapper for API calls to HealthChecks.io
package healthchecksio

import (
"context"
"net/http"

"github.com/carlmjohnson/errorx"
"github.com/carlmjohnson/requests"
)

// Client is a convenient way to ping HealthChecks.io
type Client struct {
rb *requests.Builder
}

// New returns a configured client. If c is nil, http.DefaultClient is used.
func New(uuid string, c *http.Client) Client {
return Client{
requests.
URL("https://hc-ping.com").
Path(uuid).
Client(c),
}
}

// Start calls the start HealthChecks.io endpoint
func (cl Client) Start(ctx context.Context) (err error) {
defer errorx.Trace(&err)

return cl.rb.Clone().
Path("start").
Fetch(ctx)
}

// Status calls the HealthChecks.io status endpoint
func (cl Client) Status(ctx context.Context, code int, msg []byte) (err error) {
defer errorx.Trace(&err)

return cl.rb.Clone().
Pathf("%d", code).
BodyBytes(msg).
Fetch(ctx)
}
3 changes: 3 additions & 0 deletions pkg/almanack/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spotlightpa/almanack/internal/db"
"github.com/spotlightpa/almanack/internal/github"
"github.com/spotlightpa/almanack/internal/google"
"github.com/spotlightpa/almanack/internal/healthchecksio"
"github.com/spotlightpa/almanack/internal/index"
"github.com/spotlightpa/almanack/internal/mailchimp"
"github.com/spotlightpa/almanack/internal/plausible"
Expand All @@ -35,6 +36,7 @@ func AddFlags(fl *flag.FlagSet) func() (svc Services, err error) {
gsvc.AddFlags(fl)
mailServiceAPIKey := fl.String("mc-api-key", "", "API `key` for MailChimp v2")
mailServiceListID := fl.String("mc-list-id", "", "List `ID` MailChimp v2 campaign")
hc := fl.String("healthchecks-uuid", "", "`UUID` for Healthchecks.io alert")
var pl plausible.API
pl.AddFlags(fl)

Expand Down Expand Up @@ -67,6 +69,7 @@ func AddFlags(fl *flag.FlagSet) func() (svc Services, err error) {
Gsvc: &gsvc,
EmailService: mc,
Plausible: pl,
HC: healthchecksio.New(*hc, &client),
}, nil
}
}
2 changes: 2 additions & 0 deletions pkg/almanack/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spotlightpa/almanack/internal/db"
"github.com/spotlightpa/almanack/internal/github"
"github.com/spotlightpa/almanack/internal/google"
"github.com/spotlightpa/almanack/internal/healthchecksio"
"github.com/spotlightpa/almanack/internal/index"
"github.com/spotlightpa/almanack/internal/mailchimp"
"github.com/spotlightpa/almanack/internal/plausible"
Expand All @@ -30,4 +31,5 @@ type Services struct {
Gsvc *google.Service
mailchimp.EmailService
Plausible plausible.API
HC healthchecksio.Client
}
23 changes: 23 additions & 0 deletions pkg/api/routes-background.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -38,6 +39,13 @@ func (app *appEnv) backgroundSleep(w http.ResponseWriter, r *http.Request) http.
func (app *appEnv) backgroundCron(w http.ResponseWriter, r *http.Request) http.Handler {
app.logStart(r)

if err := app.svc.HC.Start(r.Context()); err != nil {
app.logErr(r.Context(), err)
l := almlog.FromContext(r.Context())
l.ErrorContext(r.Context(), "could not contact Healthchecks.io with start; continuing")
// fallthrough and keep trying…
}

if err := flowmatic.Do(
func() error {
var errs []error
Expand Down Expand Up @@ -114,10 +122,25 @@ func (app *appEnv) backgroundCron(w http.ResponseWriter, r *http.Request) http.H
)
},
); err != nil {
msg := fmt.Appendf(nil, "error: %v", err)
if suberr := app.svc.HC.Status(r.Context(), 500, msg); suberr != nil {
app.logErr(r.Context(), suberr)
l := almlog.FromContext(r.Context())
l.ErrorContext(r.Context(), "could not contact Healthchecks.io with error")
// fallthrough to reply
}

// reply shows up in dev only
return app.jsonErr(err)
}

if suberr := app.svc.HC.Status(r.Context(), 200, []byte("OK")); suberr != nil {
app.logErr(r.Context(), suberr)
l := almlog.FromContext(r.Context())
l.ErrorContext(r.Context(), "could not contact Healthchecks.io with success")
// fallthrough to reply
}

return app.jsonAccepted("OK")
}

Expand Down

0 comments on commit 36e320d

Please sign in to comment.