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

Fix container HEALTHCHECK when port 0 is used in confguration #524

Closed
josecelano opened this issue Nov 27, 2023 · 2 comments · Fixed by #623
Closed

Fix container HEALTHCHECK when port 0 is used in confguration #524

josecelano opened this issue Nov 27, 2023 · 2 comments · Fixed by #623
Assignees
Labels
- Admin - Enjoyable to Install and Setup our Software - Developer - Torrust Improvement Experience Blocked Has Unsatisfied Dependency or blocked by a contributor. Quality & Assurance Relates to QA, Testing, and CI
Milestone

Comments

@josecelano
Copy link
Member

josecelano commented Nov 27, 2023

Context

We have a HEALTHCHECK instruction in the Containerfile:

HEALTHCHECK --interval=5s --timeout=5s --start-period=3s --retries=3 \  
  CMD /usr/bin/http_health_check http://localhost:${HEALTH_CHECK_API_PORT}/health_check \
    || exit 1

The implementation is here. It checks that all services are running:

  • API
  • 1 or more HTTP trackers
  • 1 or more UDP trackers

For the API, for example, it tries to connect to the API healthcheck endpoint.

async fn api_health_check(config: &HttpApi) -> Option<Json<Report>> {
    // todo: when port 0 is specified in the configuration get the port from the
    // running service, after starting it as we do for testing with ephemeral
    // configurations.

    if config.enabled {
        let addr: SocketAddr = config.bind_address.parse().expect("invalid socket address for API");

        if addr.port() != UNKNOWN_PORT {
            let health_check_url = format!("http://{addr}/health_check");

            if !get_req_is_ok(&health_check_url).await {
                return Some(responses::error(format!(
                    "API is not healthy. Health check endpoint: {health_check_url}"
                )));
            }
        }
    }

    None
}

In the API configuration (Config.toml):

[http_api]
bind_address = "127.0.0.1:1212"
enabled = true
#...

you can specify the port = like this:

[http_api]
bind_address = "127.0.0.1:0"
enabled = true
#...

In that case, the application will assign a free port.

Problem

The healthcheck endpoint relies on static configuration. If you use port 0 the healthcheck skippies that service. We need to pass the list of actual ports to the "healthchecker" instead of the configuration.

Solution

When we run the services we have to send back the port used to the main app. And the main app has to inject that configuration (list of services and ports) to the "healthchecker".

Relates To

@josecelano josecelano added Quality & Assurance Relates to QA, Testing, and CI - Developer - Torrust Improvement Experience - Admin - Enjoyable to Install and Setup our Software labels Nov 27, 2023
@cgbosse cgbosse moved this to Maintenance in Torrust Solution Jan 8, 2024
@cgbosse cgbosse added this to the v3.0.0 milestone Jan 16, 2024
@cgbosse cgbosse added the Blocked Has Unsatisfied Dependency or blocked by a contributor. label Jan 16, 2024
@josecelano
Copy link
Member Author

josecelano commented Jan 16, 2024

Hi @da2ce7 I think you started refactoring the healthcheck API by using closures instead of injecting the whole configuration in the handler. Could you maybe open a PR of that part or share it so I can continue working on this.

@josecelano josecelano linked a pull request Jan 26, 2024 that will close this issue
@josecelano
Copy link
Member Author

Fixed via #623

@github-project-automation github-project-automation bot moved this from Maintenance to Done in Torrust Solution Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- Admin - Enjoyable to Install and Setup our Software - Developer - Torrust Improvement Experience Blocked Has Unsatisfied Dependency or blocked by a contributor. Quality & Assurance Relates to QA, Testing, and CI
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants