vine_core/
app.rs

1use std::sync::Arc;
2use std::time::{Instant};
3
4use config::Config;
5use log::{debug, info, warn};
6
7use crate::context::context::Context;
8use crate::core::Error;
9use crate::core::runner::Runner;
10
11pub struct App {
12    context: Arc<Context>,
13}
14
15impl Default for App {
16    fn default() -> Self {
17        App {
18            context: Arc::new(Context::new("root")),
19        }
20    }
21}
22
23impl App {
24    pub fn get_context(&self) -> &Context {
25        &self.context
26    }
27
28    pub fn add_context(&self, context: Context) {
29        self.context.add_context(context);
30    }
31
32    pub async fn exec(&self) -> Result<(), Error> {
33        let timer = Instant::now();
34        info!("starting application");
35        self.context.init_contexts()?;
36
37        let config = self.context.get_bean::<Config>("config")?;
38        let mut runners = self.context.get_beans::<dyn Runner + Send + Sync>()?;
39
40        debug!("starting {} runners", runners.len());
41        let mut runner_results = Vec::new();
42        while let Some(r) = runners.pop() {
43            let config = config.clone();
44            let runner = r.clone();
45            runner_results.push(tokio::task::spawn_blocking(move || {
46                let name = runner.name().to_string();
47
48                debug!("create Runtime for runner {}", &name);
49                let runtime = runner.runtime(config).unwrap(); // TODO: fix unwrap
50
51                debug!("start runner {} within runtime", &name);
52                let join_handle = runtime.spawn(async move { runner.run().await });
53                (name, runtime, join_handle)
54            }));
55            debug!("runner {} has been started in {} micros", r.name(), timer.elapsed().as_micros());
56        }
57        info!("started in {} micros", timer.elapsed().as_micros());
58
59        let mut errors = Vec::new();
60        while let Some(runner_result) = runner_results.pop() {
61            let (name, _runtime, join_handle) = runner_result.await.map_err(|_join_error| {
62               Error::from("failed to start runner")
63            })?;
64
65            let result = join_handle.await.map_err(|_join_error| {
66                Error::from(format!("failed to execute runner {}", &name))
67            })?;
68
69            if let Err(error) = result {
70                warn!("runner {} has been finished with error: {}", &name, &error);
71                errors.push(error);
72            }
73        }
74
75        info!("application finished {} micros", timer.elapsed().as_micros());
76        if errors.is_empty() {
77            Ok(())
78        } else {
79            Err(Error::from(errors.join("\n")))
80        }
81    }
82}