mirror of
https://github.com/qdm12/ddns-updater.git
synced 2026-03-31 06:24:00 -04:00
feat(healthchecks.io): fail and exit codes support
- 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
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// New creates a new healthchecks.io client.
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"net"
|
||||
"net/netip"
|
||||
|
||||
"github.com/qdm12/ddns-updater/internal/healthchecksio"
|
||||
"github.com/qdm12/ddns-updater/internal/records"
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user