-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Background checks
- Loading branch information
Showing
5 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package examples | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/kazhuravlev/healthcheck" | ||
"math/rand/v2" | ||
"time" | ||
) | ||
|
||
func example() { | ||
ctx := context.TODO() | ||
|
||
// 1. Init healthcheck instance. It will store all our checks. | ||
hc, _ := healthcheck.New() | ||
|
||
// 2. Register basic check. It will be called each time when you call `/ready` endpoint. | ||
{ | ||
hc.Register(ctx, healthcheck.NewBasic("postgres", time.Second, func(ctx context.Context) error { | ||
if rand.Float64() > 0.5 { | ||
return errors.New("service is not available") | ||
} | ||
|
||
return nil | ||
})) | ||
} | ||
|
||
// 3. Register manual check. | ||
{ | ||
cacheWarmUp := healthcheck.NewManual("cache-warmup") | ||
cacheWarmUp.SetErr(errors.New("cache did not warmed up yet")) | ||
|
||
time.AfterFunc(5*time.Second, func() { | ||
// Mark this check as OK | ||
cacheWarmUp.SetErr(nil) | ||
}) | ||
|
||
hc.Register(ctx, cacheWarmUp) | ||
} | ||
|
||
// 4. Register background check. It will be checked in background even nobody call `/ready` endpoint. | ||
{ | ||
hc.Register(ctx, healthcheck.NewBackground( | ||
"clickhouse", | ||
errors.New("clickhouse not ready"), | ||
1*time.Second, | ||
30*time.Second, | ||
10*time.Second, | ||
func(ctx context.Context) error { | ||
return nil | ||
}, | ||
)) | ||
} | ||
|
||
// 3. Init and run a webserver for integration with Kubernetes. | ||
sysServer, _ := healthcheck.NewServer(hc, healthcheck.WithPort(8080)) | ||
_ = sysServer.Run(ctx) | ||
|
||
// 4. Open http://localhost:8080/ready to check the status of your system | ||
select {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters