replace email processing reactions; update deps

This commit is contained in:
Aine
2024-04-30 09:18:04 +03:00
parent 15d61f174e
commit 0e3655195a
35 changed files with 709 additions and 247 deletions

View File

@@ -1,15 +1,37 @@
# healthchecks
A [healthchecks.io](https://github.com/healthchecks/healthchecks) client
A fully async [healthchecks.io](https://github.com/healthchecks/healthchecks) golang client, with lots of features, some highlights:
check the godoc for information
* Highly configurable: `WithHTTPClient()`, `WithBaseURL()`, `WithUserAgent()`, `WithErrLog()`, `WithCheckUUID()`, `WithAutoProvision()`, etc.
* Automatic determination of HTTP method (`POST`, `HEAD`) based on body existence
* Auto mode: just call `client.Auto(time.Duration)` and client will send `Success()` request automatically with specified frequency
* Global mode: init client once with `healthchecks.New()`, and access it from anywhere by calling `healthchecks.Global()`
Check [godoc](https://pkg.go.dev/gitlab.com/etke.cc/go/healthchecks/v2) for more details.
```go
hc := healthchecks.New(
healthchecks.WithCheckUUID("your-uuid"),
)
go hc.Auto()
package main
hc.Log(strings.NewReader("optional body you can attach to any action"))
hc.Shutdown()
import (
"time"
"gitlab.com/etke.cc/go/healthchecks/v2"
)
var hc *healthchecks.Client
func main() {
hc = healthchecks.New(
healthchecks.WithCheckUUID("CHECK_UUID")
)
defer hc.Shutdown()
// send basic success request
hc.Success()
// or use auto mode, that will send success request with the specified frequency
go hc.Auto(1*time.Minute)
// need to call the client from another place in your project?
// just call healthchecks.Global() and you will get the same client
}
```

View File

@@ -17,8 +17,3 @@ func (c *Client) Auto(every time.Duration) {
}
}
}
// Shutdown the client
func (c *Client) Shutdown() {
c.done <- true
}

View File

@@ -5,39 +5,46 @@ import (
"io"
"net/http"
"strconv"
"sync"
"time"
"github.com/google/uuid"
)
// Client for healthchecks
// if client initialized without any options, it will be disabled by default,
// but you can override it by calling SetEnabled(true).
type Client struct {
http *http.Client
log func(string, error)
baseURL string
uuid string
rid string
create bool
done chan bool
wg sync.WaitGroup
enabled bool
http *http.Client
log func(string, error)
userAgent string
baseURL string
uuid string
rid string
create bool
done chan bool
}
// init client
func (c *Client) init(options ...Option) {
c.enabled = true
c.log = DefaultErrLog
c.baseURL = DefaultAPI
c.userAgent = DefaultUserAgent
c.http = &http.Client{Timeout: 10 * time.Second}
c.done = make(chan bool, 1)
c.uuid = ""
if len(options) == 0 {
c.enabled = false
}
for _, option := range options {
option(c)
}
if c.log == nil {
c.log = DefaultErrLog
}
if c.baseURL == "" {
c.baseURL = DefaultAPI
}
if c.http == nil {
c.http = &http.Client{Timeout: 10 * time.Second}
}
if c.done == nil {
c.done = make(chan bool, 1)
}
if c.uuid == "" {
randomUUID, _ := uuid.NewRandom()
c.uuid = randomUUID.String()
@@ -46,22 +53,39 @@ func (c *Client) init(options ...Option) {
}
}
// call API
func (c *Client) call(operation, endpoint string, body ...io.Reader) {
var err error
var resp *http.Response
if !c.enabled {
return
}
c.wg.Add(1)
defer c.wg.Done()
targetURL := fmt.Sprintf("%s/%s%s?rid=%s", c.baseURL, c.uuid, endpoint, c.rid)
if c.create {
targetURL += "&create=1"
}
var req *http.Request
var err error
if len(body) > 0 {
resp, err = c.http.Post(targetURL, "text/plain; charset=utf-8", body[0])
req, err = http.NewRequest(http.MethodPost, targetURL, body[0])
} else {
resp, err = c.http.Head(targetURL)
req, err = http.NewRequest(http.MethodHead, targetURL, http.NoBody)
}
if err != nil {
c.log(operation, err)
return
}
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
resp, err := c.http.Do(req)
if err != nil {
c.log(operation, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
@@ -76,27 +100,40 @@ func (c *Client) call(operation, endpoint string, body ...io.Reader) {
}
}
// SetEnabled sets the enabled flag, ignoring the options
// if client initialized without any options, it will be disabled by default,
// but you can override it by calling SetEnabled(true).
func (c *Client) SetEnabled(enabled bool) {
c.enabled = enabled
}
// Start signal means the job started
func (c *Client) Start(optionalBody ...io.Reader) {
c.call("start", "/start", optionalBody...)
go c.call("start", "/start", optionalBody...)
}
// Success signal means the job has completed successfully (or, a continuously running process is still running and healthy).
func (c *Client) Success(optionalBody ...io.Reader) {
c.call("success", "", optionalBody...)
go c.call("success", "", optionalBody...)
}
// Fail signal means the job failed
func (c *Client) Fail(optionalBody ...io.Reader) {
c.call("fail", "/fail", optionalBody...)
go c.call("fail", "/fail", optionalBody...)
}
// Log signal just adds an event to the job log, without changing job status
func (c *Client) Log(optionalBody ...io.Reader) {
c.call("log", "/log", optionalBody...)
go c.call("log", "/log", optionalBody...)
}
// ExitStatus signal sends job's exit code (0-255)
func (c *Client) ExitStatus(exitCode int, optionalBody ...io.Reader) {
c.call("exit status", "/"+strconv.Itoa(exitCode), optionalBody...)
go c.call("exit status", "/"+strconv.Itoa(exitCode), optionalBody...)
}
// Shutdown the client
func (c *Client) Shutdown() {
c.done <- true
c.wg.Wait()
}

View File

@@ -6,12 +6,19 @@ import (
"github.com/google/uuid"
)
// DefaultAPI base url for checks
const DefaultAPI = "https://hc-ping.com"
const (
// DefaultAPI base url for checks
DefaultAPI = "https://hc-ping.com"
// DefaultUserAgent for the client
DefaultUserAgent = "Go-Healthchecks (lib; +https://gitlab.com/etke.cc/go/healthchecks)"
)
// ErrLog used to log errors occurred during an operation
type ErrLog func(operation string, err error)
// global client
var global *Client
// DefaultErrLog if you don't provide one yourself
var DefaultErrLog = func(operation string, err error) {
fmt.Printf("healtchecks operation %q failed: %v\n", operation, err)
@@ -25,5 +32,18 @@ func New(options ...Option) *Client {
}
c.init(options...)
if global == nil {
global = c
}
return c
}
// Global healthchecks client
func Global() *Client {
if global == nil {
global = New()
}
return global
}

View File

@@ -2,6 +2,7 @@ package healthchecks
import "net/http"
// Option for healthchecks client
type Option func(*Client)
// WithHTTPClient sets the http client
@@ -18,6 +19,13 @@ func WithBaseURL(baseURL string) Option {
}
}
// WithUserAgent sets the user agent
func WithUserAgent(userAgent string) Option {
return func(c *Client) {
c.userAgent = userAgent
}
}
// WithErrLog sets the error log
func WithErrLog(errLog ErrLog) Option {
return func(c *Client) {
@@ -39,6 +47,13 @@ func WithAutoProvision() Option {
}
}
// WithGlobal sets this client as the global client
func WithGlobal() Option {
return func(c *Client) {
global = c
}
}
// WithDone sets the done channel
func WithDone(done chan bool) Option {
return func(c *Client) {

View File

@@ -53,13 +53,17 @@ func (p *Client) GetWithContext(ctx context.Context, identifier string) ([]*Targ
return nil, err
}
req.SetBasicAuth(p.login, p.password)
req.Header.Set("User-Agent", "Go-psd-client/"+version)
req.Header.Set("User-Agent", "Go-PSD-client/"+version)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusGone { // not found, to distinguish from reverse proxy 404 error
return nil, nil
}
err = fmt.Errorf("%s", resp.Status) //nolint:goerr113 // that's ok
return nil, err
}