1use anyhow::{Context, Error, Result};
2use tokio::sync::mpsc::channel;
3
4use crate::providers::http::Report as HttpReport;
5use crate::report::Report;
6use crate::tasks::adapters::from_file;
7use crate::utils::timestamp::current_timestamp;
8use crate::worker::{Squad, Worker};
9
10const CONFIG_FILE_NAME: &str = "nasu.json";
14
15pub async fn run() -> Result<()> {
16 let tasks =
17 from_file(CONFIG_FILE_NAME).context(format!("Failed to parse \"{}\"", CONFIG_FILE_NAME))?;
18 let workers: Vec<Worker> = tasks.into_iter().map(|task| Worker::from(task)).collect();
19 let (tx, mut rx) = channel::<Report>(1024);
20 let squad = Squad::new(workers, tx);
21
22 let print_proc = tokio::spawn(async move {
23 println!(
24 "{0: <15} | {1: <15} | {2: <20} | {3: <15} | {4: <15}",
25 "Log Time", "Task", "HTTP. Status Code", "Req. Time", "Res. Time"
26 );
27 println!("==========================================================================================");
28 while let Some(report) = rx.recv().await {
29 match report {
30 Report::Http(HttpReport {
31 id,
32 req_end,
33 req_start,
34 status_code,
35 ..
36 }) => {
37 println!(
38 "{0: <15} | {1: <15} | {2: <20} | {3: <15} | {4: <15}",
39 current_timestamp(),
40 id,
41 status_code,
42 req_start,
43 req_end
44 );
45 }
46 }
47 }
48 });
49
50 let run_proc = tokio::spawn(async move {
51 squad.start().await;
52 });
53
54 tokio::select! {
55 _ = print_proc => {
56 Err(Error::msg("Output process stopped"))
57 },
58 _ = run_proc => {
59 Err(Error::msg("Run process stopped"))
60 },
61 }
62}