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

Tracker Checker: handle Health Check timeouts #678

Closed
Tracked by #669 ...
josecelano opened this issue Feb 2, 2024 · 1 comment
Closed
Tracked by #669 ...

Tracker Checker: handle Health Check timeouts #678

josecelano opened this issue Feb 2, 2024 · 1 comment
Assignees
Labels
- Admin - Enjoyable to Install and Setup our Software - Developer - Torrust Improvement Experience Code Cleanup / Refactoring Tidying and Making Neat Easy Good for Newcomers Enhancement / Feature Request Something New good first issue Good for newcomers Testing Checking Torrust
Milestone

Comments

@josecelano
Copy link
Member

Parent issue: #677

You can run a Tracker Checker with:

TORRUST_CHECKER_CONFIG='{
    "udp_trackers": [],
    "http_trackers": [],
    "health_checks": ["http://127.0.0.1:3030/sleep"]
}' cargo run --bin tracker_checker

If the server does not reply in 5 seconds you will see an error like this:

$ TORRUST_CHECKER_CONFIG='{
    "udp_trackers": [],
    "http_trackers": [],
    "health_checks": ["http://127.0.0.1:3030/sleep"]
}' cargo run --bin tracker_checker
    Finished dev [optimized + debuginfo] target(s) in 0.08s
     Running `target/debug/tracker_checker`
Running checks for trackers ...
UDP trackers ...
HTTP trackers ...
Health checks ...
✗ - Health API at http://127.0.0.1:3030/sleep is failing: reqwest::Error { kind: Request, url: Url { scheme: "http", cannot_be_a_base: false, username: "", password: None, host: Some(Ipv4(127.0.0.1)), port: Some(3030), path: "/sleep", query: None, fragment: None }, source: TimedOut }

You can use this example app to simulate the server.

This is the function for the check:

async fn run_health_check(url: Url, console: &Console) -> Result<(), CheckError> {
    let client = HttpClient::builder().timeout(Duration::from_secs(5)).build().unwrap();

    let colored_url = url.to_string().yellow();

    match client.get(url.clone()).send().await {
        Ok(response) => {
            if response.status().is_success() {
                console.println(&format!("{} - Health API at {} is OK", "✓".green(), colored_url));
                Ok(())
            } else {
                console.eprintln(&format!(
                    "{} - Health API at {} is failing: {:?}",
                    "✗".red(),
                    colored_url,
                    response
                ));
                Err(CheckError::HealthCheckError { url })
            }
        }
        Err(err) => {
            console.eprintln(&format!(
                "{} - Health API at {} is failing: {:?}",
                "✗".red(),
                colored_url,
                err
            ));
            Err(CheckError::HealthCheckError { url })
        }
    }
}

It contains this line:

let client = HttpClient::builder().timeout(Duration::from_secs(5)).build().unwrap();

We have to catch the error instead of unwrapping. Otherwise, the Tracker Checker is halted. The Tracker Checker should finish the whole report even if one service is not available.

@josecelano josecelano added Enhancement / Feature Request Something New Easy Good for Newcomers Code Cleanup / Refactoring Tidying and Making Neat - Developer - Torrust Improvement Experience - Admin - Enjoyable to Install and Setup our Software Testing Checking Torrust good first issue Good for newcomers labels Feb 2, 2024
@josecelano josecelano added this to the v3.1.0 milestone Feb 2, 2024
@da2ce7 da2ce7 self-assigned this Mar 27, 2024
@josecelano
Copy link
Member Author

This was implemented by @da2ce7:

Example:

TORRUST_CHECKER_CONFIG='{
    "udp_trackers": [],
    "http_trackers": [],
    "health_checks": ["http://127.0.0.1:3030/sleep", "https://google.es"]
}' cargo run --bin tracker_checker

Output:

    Finished `dev` profile [optimized + debuginfo] target(s) in 0.09s
     Running `target/debug/tracker_checker`
2024-09-11T08:11:26.887637Z  INFO torrust_tracker::console::clients::checker::service: Running checks for trackers ...
[
  {
    "Health": {
      "Err": {
        "url": "http://127.0.0.1:3030/sleep",
        "result": {
          "Err": "Heath check failed to get a response: reqwest::Error { kind: Request, url: \"http://127.0.0.1:3030/sleep\", source: TimedOut }"
        }
      }
    }
  },
  {
    "Health": {
      "Ok": {
        "url": "https://google.es/",
        "result": {
          "Ok": "200 OK"
        }
      }
    }
  }
]

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 Code Cleanup / Refactoring Tidying and Making Neat Easy Good for Newcomers Enhancement / Feature Request Something New good first issue Good for newcomers Testing Checking Torrust
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants