Skip to content

Commit

Permalink
feat(healthchecks.io): fail and exit codes support
Browse files Browse the repository at this point in the history
- notify with `/fail` suffix if any update failed
- notify with `/0` on program exit with 0 code
- notify with `/1` on program exit with 1 code
  • Loading branch information
qdm12 committed Feb 9, 2024
1 parent 1697697 commit ec4411e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
11 changes: 11 additions & 0 deletions cmd/updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,12 @@ func _main(ctx context.Context, reader *reader.Reader, args []string, logger log

err = shutdownGroup.Shutdown(context.Background())
if err != nil {
exitHealthchecksio(hioClient, logger, healthchecksio.Exit1)
shoutrrrClient.Notify(err.Error())
return err
}

exitHealthchecksio(hioClient, logger, healthchecksio.Exit0)
return nil
}

Expand Down Expand Up @@ -330,3 +333,11 @@ func backupRunLoop(ctx context.Context, done chan<- struct{}, backupPeriod time.
}
}
}

func exitHealthchecksio(hioClient *healthchecksio.Client,
logger log.LoggerInterface, state healthchecksio.State) {
err := hioClient.Ping(context.Background(), state)
if err != nil {
logger.Error(err.Error())
}
}
25 changes: 22 additions & 3 deletions internal/healthchecksio/healthchecksio.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
)

// New creates a new healthchecks.io client.
Expand All @@ -25,13 +26,31 @@ var (
ErrStatusCode = errors.New("bad status code")
)

func (c *Client) Ping(ctx context.Context) (err error) {
type State string

const (
Ok State = "ok"
Start State = "start"
Fail State = "fail"
Exit0 State = "0"
Exit1 State = "1"
)

func (c *Client) Ping(ctx context.Context, state State) (err error) {
if c.uuid == "" {
return nil
}

request, err := http.NewRequestWithContext(ctx, http.MethodGet,
"https://hc-ping.com/"+c.uuid, nil)
url := url.URL{
Scheme: "https",
Host: "hc-ping.com",
Path: "/" + c.uuid,
}
if state != Ok {
url.Path += "/" + string(state)
}

request, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
return fmt.Errorf("creating request: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/update/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"net/netip"

"github.com/qdm12/ddns-updater/internal/healthchecksio"
"github.com/qdm12/ddns-updater/internal/records"
)

Expand Down Expand Up @@ -40,5 +41,5 @@ type Logger interface {
}

type HealthchecksIOClient interface {
Ping(ctx context.Context) (err error)
Ping(ctx context.Context, state healthchecksio.State) (err error)
}
8 changes: 7 additions & 1 deletion internal/update/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/qdm12/ddns-updater/internal/constants"
"github.com/qdm12/ddns-updater/internal/healthchecksio"
"github.com/qdm12/ddns-updater/internal/models"
librecords "github.com/qdm12/ddns-updater/internal/records"
"github.com/qdm12/ddns-updater/pkg/publicip/ipversion"
Expand Down Expand Up @@ -318,7 +319,12 @@ func (r *Runner) updateNecessary(ctx context.Context) (errors []error) {
}
}

err := r.hioClient.Ping(ctx)
healthchecksIOState := healthchecksio.Ok
if len(errors) > 0 {
healthchecksIOState = healthchecksio.Fail
}

err := r.hioClient.Ping(ctx, healthchecksIOState)
if err != nil {
r.logger.Error("pinging healthchecks.io failed: " + err.Error())
}
Expand Down

0 comments on commit ec4411e

Please sign in to comment.