-
Notifications
You must be signed in to change notification settings - Fork 29
[health] add internal healthcheck command #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
| package health | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/android-sms-gateway/server/internal/config" | ||
| "github.com/go-core-fx/logger" | ||
| "go.uber.org/fx" | ||
| ) | ||
|
|
||
| func Run() { | ||
| fx.New( | ||
| fx.StartTimeout(time.Second), | ||
| logger.Module(), | ||
| logger.WithFxDefaultLogger(), | ||
| config.Module(), | ||
| module(), | ||
| ).Run() | ||
| } | ||
|
|
||
| func module() fx.Option { | ||
| return fx.Module( | ||
| "health", | ||
| fx.Provide(NewChecker), | ||
| fx.Invoke(func(lc fx.Lifecycle, checker *Checker) { | ||
| lc.Append(fx.Hook{ | ||
| OnStart: func(ctx context.Context) error { | ||
| return checker.Execute(ctx) | ||
| }, | ||
| OnStop: func(_ context.Context) error { | ||
| return nil | ||
| }, | ||
| }) | ||
| }), | ||
| ) | ||
| } |
This file contains hidden or 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,74 @@ | ||
| package health | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| httpclient "net/http" | ||
| "time" | ||
|
|
||
| "github.com/capcom6/go-infra-fx/http" | ||
| "go.uber.org/fx" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| var ErrNotHealthy = errors.New("not healthy") | ||
|
|
||
| type Checker struct { | ||
| config http.Config | ||
|
|
||
| shutdowner fx.Shutdowner | ||
| logger *zap.Logger | ||
| } | ||
|
|
||
| func NewChecker(config http.Config, shutdowner fx.Shutdowner, logger *zap.Logger) *Checker { | ||
| return &Checker{ | ||
| config: config, | ||
| shutdowner: shutdowner, | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| func (c *Checker) Execute(ctx context.Context) error { | ||
| ctx, cancel := context.WithTimeout(ctx, time.Second) | ||
| defer cancel() | ||
|
|
||
| client := httpclient.DefaultClient | ||
|
|
||
| req, err := httpclient.NewRequestWithContext( | ||
| ctx, | ||
| httpclient.MethodGet, | ||
| "http://"+c.config.Listen+"/health/live", | ||
| nil, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create request: %w", err) | ||
| } | ||
|
|
||
| res, err := client.Do(req) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to send request: %w", err) | ||
| } | ||
| defer res.Body.Close() | ||
|
|
||
| body, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read response body: %w", err) | ||
| } | ||
|
|
||
| c.logger.Info(string(body)) | ||
|
|
||
| if res.StatusCode >= httpclient.StatusBadRequest { | ||
| c.logger.Error("health check failed", zap.Int("status", res.StatusCode), zap.String("body", string(body))) | ||
| return fmt.Errorf("%w: health check failed: %s", ErrNotHealthy, string(body)) | ||
| } | ||
|
|
||
| c.logger.Info("health check passed", zap.Int("status", res.StatusCode)) | ||
|
|
||
| if shErr := c.shutdowner.Shutdown(); shErr != nil { | ||
| c.logger.Error("failed to shutdown", zap.Error(shErr)) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.