1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::sync::Arc;
use std::time::{Instant};

use config::Config;
use log::{debug, info, warn};

use crate::context::context::Context;
use crate::core::Error;
use crate::core::runner::Runner;

pub struct App {
    context: Arc<Context>,
}

impl Default for App {
    fn default() -> Self {
        App {
            context: Arc::new(Context::new("root")),
        }
    }
}

impl App {
    pub fn get_context(&self) -> &Context {
        &self.context
    }

    pub fn add_context(&self, context: Context) {
        self.context.add_context(context);
    }

    pub async fn exec(&self) -> Result<(), Error> {
        let timer = Instant::now();
        info!("starting application");
        self.context.init_contexts()?;

        let config = self.context.get_bean::<Config>("config")?;
        let mut runners = self.context.get_beans::<dyn Runner + Send + Sync>()?;

        debug!("starting {} runners", runners.len());
        let mut runner_results = Vec::new();
        while let Some(r) = runners.pop() {
            let config = config.clone();
            let runner = r.clone();
            runner_results.push(tokio::task::spawn_blocking(move || {
                let name = runner.name().to_string();

                debug!("create Runtime for runner {}", &name);
                let runtime = runner.runtime(config).unwrap(); // TODO: fix unwrap

                debug!("start runner {} within runtime", &name);
                let join_handle = runtime.spawn(async move { runner.run().await });
                (name, runtime, join_handle)
            }));
            debug!("runner {} has been started in {} micros", r.name(), timer.elapsed().as_micros());
        }
        info!("started in {} micros", timer.elapsed().as_micros());

        let mut errors = Vec::new();
        while let Some(runner_result) = runner_results.pop() {
            let (name, _runtime, join_handle) = runner_result.await.map_err(|_join_error| {
               Error::from("failed to start runner")
            })?;

            let result = join_handle.await.map_err(|_join_error| {
                Error::from(format!("failed to execute runner {}", &name))
            })?;

            if let Err(error) = result {
                warn!("runner {} has been finished with error: {}", &name, &error);
                errors.push(error);
            }
        }

        info!("application finished {} micros", timer.elapsed().as_micros());
        if errors.is_empty() {
            Ok(())
        } else {
            Err(Error::from(errors.join("\n")))
        }
    }
}