Skip to content
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

pkg/services: add services.Config.NewServices/Internals helper #204

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jmank88
Copy link
Collaborator

@jmank88 jmank88 commented Oct 29, 2023

This PR introduces services.Config.NewServices with services.Internals to help create services.Services in a standard way that reduces boilerplate and the risk of mistakes that comes with re-implementing logger naming, health reporting, sub-service management, etc.

Blocked by:

@jmank88 jmank88 temporarily deployed to integration October 29, 2023 13:40 — with GitHub Actions Inactive
@jmank88 jmank88 temporarily deployed to integration October 29, 2023 13:40 — with GitHub Actions Inactive
@jmank88 jmank88 temporarily deployed to integration October 29, 2023 13:40 — with GitHub Actions Inactive
@jmank88 jmank88 temporarily deployed to integration October 30, 2023 22:12 — with GitHub Actions Inactive
@jmank88 jmank88 temporarily deployed to integration October 30, 2023 22:12 — with GitHub Actions Inactive
@jmank88 jmank88 temporarily deployed to integration October 30, 2023 22:12 — with GitHub Actions Inactive
return
}

// Output:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a txtar like way to test against this output? That might be too brittle since the sub services are closed concurrently.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a txtar like way to test against this output?

It is already built-in. These execute like regular tests.
https://go.dev/blog/examples

That might be too brittle since the sub services are closed concurrently.

We can probably add a test-only hack to make the close deterministic 🤔

pkg/services/service.go Outdated Show resolved Hide resolved
return
}

func (s *service) HealthReport() map[string]error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a fail-safe, we could track when this is called, and if it is never called, or not called often enough, then we could periodically emit to logs instead 🤔

Comment on lines +74 to +96
func (e *Engine) HealthEvent(err error) { e.healthEvent(err) }

// SetUnhealthy records a condition key and an error, which causes an unhealthy report, until SetHealthy(condition) is called.
func (e *Engine) SetUnhealthy(condition string, err error) {
e.condsMu.Lock()
defer e.condsMu.Unlock()
e.conds[condition] = fmt.Errorf("%s: %e", condition, err)
}

// SetHealthy removes a condition and error recorded by SetUnhealthy.
func (e *Engine) SetHealthy(condition string) {
e.condsMu.Lock()
defer e.condsMu.Unlock()
delete(e.conds, condition)
}

// Unhealthy causes an unhealthy report, until the returned func() is called.
// Use this for simple cases where the func() can be kept in scope, and prefer to defer it inline if possible:
//
// defer Unhealthy(fmt.Errorf("foo bar: %i", err))()
//
// See SetUnhealthy for an alternative API.
func (e *Engine) Unhealthy(err error) func() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still not satisfied with these names. I think that these four different operations are fundamental, but I'm not sure what to call them that would be more intuitive than this... 🤔

})
}

func (s *service) Close() error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DeividasK We could prioritize Stop while adapting Close easily here without spamming it in every implementation.

Suggested change
func (s *service) Close() error {
func (s *service) Close() error { return s.Stop() }
func (s *service) Stop() error {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants