<commit_before>use std::time::Duration;

use futures::*;
use tokio_timer::*;
use tokio_core::reactor::Core;
use hyper::Client;
use hyper_tls::HttpsConnector;

use pool::Pool;

pub struct HealthCheck {
    interval: Duration,
    pool: Pool,
    uri_path: String,
}

impl HealthCheck {
    pub fn new(interval: Duration, pool: Pool, uri_path: String) -> HealthCheck {

        HealthCheck {
            interval: interval,
            pool: pool,
            uri_path: uri_path,
        }
    }

    pub fn run(&self) {
        let mut core = Core::new().unwrap();
        let timer = Timer::default();

        let handle = core.handle();
        let client = Client::configure()
            .connector(HttpsConnector::new(4, &handle))
            .build(&handle);

        let pool = self.pool.clone();
        let work = timer.interval(self.interval).for_each(move |()| {
            let servers = pool.all();
            for server in servers {
                let url = server.url().join(&self.uri_path).unwrap();
                debug!("Health check {:?}", url);

                let pool1 = pool.clone();
                let pool2 = pool.clone();
                let server1 = server.clone();
                let server2 = server.clone();
                let req = client.get(url).and_then(move |res| {
                    debug!("Response: {}", res.status());
                    debug!("Headers: \n{}", res.headers());

                    if ! res.status().is_success() {
                        info!("Removing {:?} from pool", server1);
                        pool1.remove(&server1);
                    }

                    ::futures::finished(())
                }).map_err(move |e| {
                    error!("Error connecting to backend: {:?}", e);
                    info!("Removing {:?} from pool", server2);
                    pool2.remove(&server2);
                });

                handle.spawn(req);
            }

            Ok(())
        }).map_err(|_| {});

        core.run(work).unwrap();
    }
}
<commit_msg>#71 Should now bubble errors up the scope in health.rs (#81)<commit_after>use std::time::Duration;

use futures::*;
use tokio_timer::*;
use tokio_core::reactor::Core;
use hyper::Client;
use hyper_tls::HttpsConnector;
use std::io;

use pool::Pool;

pub struct HealthCheck {
    interval: Duration,
    pool: Pool,
    uri_path: String,
}

impl HealthCheck {
    pub fn new(interval: Duration, pool: Pool, uri_path: String) -> HealthCheck {

        HealthCheck {
            interval: interval,
            pool: pool,
            uri_path: uri_path,
        }
    }

    pub fn run(&self) -> io::Result<()> {
        let mut core = try!(Core::new());
        let timer = Timer::default();

        let handle = core.handle();
        let client = Client::configure()
            .connector(HttpsConnector::new(4, &handle))
            .build(&handle);

        let pool = self.pool.clone();
        let work = timer.interval(self.interval).for_each(move |()| {
            let servers = pool.all();
            for server in servers {
                let url = match server.url().join(&self.uri_path) {
                Ok(url) => url,
                Err(e) => {
                    error!("Invalid health check url: {:?}",e);
                    return Ok(());
                }
            };
                debug!("Health check {:?}", url);

                let pool1 = pool.clone();
                let pool2 = pool.clone();
                let server1 = server.clone();
                let server2 = server.clone();
                let req = client.get(url).and_then(move |res| {
                    debug!("Response: {}", res.status());
                    debug!("Headers: \n{}", res.headers());

                    if ! res.status().is_success() {
                        info!("Removing {:?} from pool", server1);
                        pool1.remove(&server1);
                    }

                    ::futures::finished(())
                }).map_err(move |e| {
                    error!("Error connecting to backend: {:?}", e);
                    info!("Removing {:?} from pool", server2);
                    pool2.remove(&server2);
                });

                handle.spawn(req);
            }

            Ok(())
        }).map_err(From::from);

        core.run(work)
    }
}

{'fraction_non_alphanumeric': 0.14810562571756603, 'fraction_numerical': 0.005970149253731343, 'mean_word_length': 1.5325581395348837, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6560239', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1174, 'n_words': 272}