diff --git a/Cargo.toml b/Cargo.toml index 39deb6d..4a6c1f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,16 +7,28 @@ license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] -# tokio = { version = "1", features = ["full"] } -#tokio = { version = "1", default-features = false, features = ["time", "macros", "rt", "rt-multi-thread"] } -tokio = { version = "1", default-features = false, features = ["time", "macros", "rt"] } +[workspace.dependencies] +async-trait = "0.1" +env_logger = "0.10" +envy = "0.4" futures = "0.3" +lettre = { version = "0.10", features = ["rustls-tls"] } log = { version = "0.4", features = ["max_level_debug", "release_max_level_info"] } -env_logger = "0.10" +merge = "0.1" +reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +tokio = { version = "1", default-features = false, features = ["time", "macros", "rt"] } +toml = "0.7" + +[dependencies] configloader = { path = "./configloader"} mailsender = { path = "./mailsender" } gitlabapi = { path = "./gitlabapi" } +tokio = { workspace = true } +futures = { workspace = true } +log = { workspace = true } +env_logger = { workspace = true } [dev-dependencies] env_logger = "0.10" diff --git a/configloader/Cargo.toml b/configloader/Cargo.toml index dad172f..2d3878d 100644 --- a/configloader/Cargo.toml +++ b/configloader/Cargo.toml @@ -6,10 +6,10 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -toml = "0.7" -envy = "0.4" -merge = "0.1" -serde = { version = "1.0", features = ["derive"] } -log = { version = "0.4", features = ["max_level_debug", "release_max_level_info"] } -env_logger = "0.10" -lettre = { version = "0.10", features = ["rustls-tls"] } \ No newline at end of file +toml = { workspace = true } +envy = { workspace = true } +merge = { workspace = true } +serde = { workspace = true } +log = { workspace = true } +env_logger = { workspace = true } +lettre = { workspace = true } diff --git a/gitlabapi/Cargo.toml b/gitlabapi/Cargo.toml index 1593dca..bffdc06 100644 --- a/gitlabapi/Cargo.toml +++ b/gitlabapi/Cargo.toml @@ -6,16 +6,14 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -# serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] } -log = { version = "0.4", features = ["max_level_debug", "release_max_level_info"] } -async-trait = "0.1" -futures = "0.3" -# tokio = { version = "1", features = ["full"] } -tokio = { version = "1", features = ["macros"] } +serde_json = { workspace = true } +reqwest = { workspace = true } +log = { workspace = true } +async-trait = { workspace = true } +futures = { workspace = true } +tokio = { workspace = true } configloader = { path = "../configloader"} [dev-dependencies] -env_logger = "0.10" \ No newline at end of file +env_logger = "0.10" diff --git a/mailsender/Cargo.toml b/mailsender/Cargo.toml index 6a53e49..0ced29c 100644 --- a/mailsender/Cargo.toml +++ b/mailsender/Cargo.toml @@ -6,12 +6,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lettre = { version = "0.10", features = ["rustls-tls"] } -merge = "0.1" -serde = { version = "1.0", features = ["derive"] } -log = { version = "0.4", features = ["max_level_debug", "release_max_level_info"] } -# tokio = { version = "1", features = ["full"] } -tokio = { version = "1", default-features = false, features = ["time", "macros"] } +lettre = { workspace = true } +merge = { workspace = true } +serde = { workspace = true } +log = { workspace = true } +tokio = { workspace = true } configloader = { path = "../configloader"} diff --git a/mailsender/src/tests.rs b/mailsender/src/tests.rs index 942a456..1910c7a 100644 --- a/mailsender/src/tests.rs +++ b/mailsender/src/tests.rs @@ -90,7 +90,7 @@ This is a test message. :-) debug!("{:?}", mail_message); } - #[tokio::test] + #[tokio::test(flavor = "current_thread")] #[ignore = "It'll really send an email message"] async fn test_send_mail() { init(); diff --git a/mailsender/src/utils.rs b/mailsender/src/utils.rs index b78692e..08aec21 100644 --- a/mailsender/src/utils.rs +++ b/mailsender/src/utils.rs @@ -29,7 +29,7 @@ pub trait SmtpUtils { &self, subject: String, message: String, - destination: &Option, + destination: Option, ) -> Message; } @@ -38,7 +38,7 @@ impl SmtpUtils for SmtpConfig { &self, subject: String, message: String, - destination: &Option, + destination: Option, ) -> Message { if !self.is_valid() { error!("Smtp configuration is invalid"); diff --git a/src/main.rs b/src/main.rs index 291d036..11d1d88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,6 @@ use futures::stream::{self, StreamExt}; use log::{error, info}; -use std::cell::RefCell; use std::rc::Rc; use tokio::runtime; use tokio::time as tktime; @@ -76,8 +75,10 @@ fn main() -> Result<(), Box> { // Build mail relay let smtp_configs = Rc::new(config.smtp.clone().unwrap_or_default()); - let smtp_cfg = smtp_configs.clone(); - let mail_relay_handle = tokio::spawn(utils::mailrelay_buid(smtp_cfg.as_ref().to_owned())); + let smtp_cfg = Rc::clone(&smtp_configs); + + let mail_relay_handle = + tokio::task::spawn(utils::mailrelay_build(smtp_cfg.as_ref().to_owned())); // Scan projects for Manual jobs let api = GitlabJOB::new(&config); @@ -116,7 +117,16 @@ fn main() -> Result<(), Box> { } // Prepare for mail reports - let mail_relay = Rc::new(mail_relay_handle.await.unwrap_or_default()); + let mail_relay = match mail_relay_handle.await { + Ok(mailer) => { + debug!("Mail relay built"); + mailer + } + Err(e) => { + error!("Error setting up mail relay: {}", e); + None + } + }; // Which Gitlab status must be waited let pending_status = [ @@ -126,8 +136,6 @@ fn main() -> Result<(), Box> { JobScope::Manual, ]; - let mails_handler = Rc::new(RefCell::new(vec![])); - // Stream to monitor jobs' status let monitor_jobs = stream::iter(actions) .map(|result| async { @@ -166,14 +174,16 @@ fn main() -> Result<(), Box> { let smtp_configs = smtp_configs.clone(); let mailer = mailer.clone(); - let mut handler = mails_handler.borrow_mut(); - handler.push(tokio::task::spawn_local(async move { - mailer.send(&utils::mail_message( - &job, - msg_reason, - smtp_configs.as_ref(), - )) - })); + match mailer.send(&utils::mail_message( + &job, + msg_reason, + smtp_configs.as_ref(), + )) { + Ok(_) => debug!("Message for job {} sent", &job), + Err(_) => { + error!("Erro while sneding message for job {}", &job) + } + }; } info!("Job {} finished with status: {}", job, curr_status); @@ -186,14 +196,21 @@ fn main() -> Result<(), Box> { let smtp_configs = smtp_configs.clone(); let mailer = mailer.clone(); - let mut handler = mails_handler.borrow_mut(); - handler.push(tokio::task::spawn_local(async move { - mailer.send(&utils::mail_message( - &job, - MailReason::MaxWaitElapsed, - smtp_configs.as_ref(), - )) - })); + let _ = mailer.send(&utils::mail_message( + &job, + MailReason::MaxWaitElapsed, + smtp_configs.as_ref(), + )); + match mailer.send(&utils::mail_message( + &job, + MailReason::MaxWaitElapsed, + smtp_configs.as_ref(), + )) { + Ok(_) => debug!("Message for job {} sent", &job), + Err(_) => { + error!("Erro while sneding message for job {}", &job) + } + }; } warn!("Job {} elapsed max waiting time", job); break; @@ -242,11 +259,7 @@ fn main() -> Result<(), Box> { // Just another way to run streams while (monitor_jobs.next().await).is_some() {} - - // Wait for mail jobs - for handler in mails_handler.take().iter_mut() { - let _ = handler.await; - } }); + debug!("Bye!"); Ok(()) } diff --git a/src/tests.rs b/src/tests.rs index 24b6fd1..55729da 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -77,7 +77,7 @@ mod integration_tests { let to_cancel = utils::pipelines_tocancel(&response); to_cancel.iter().for_each(|(proj, pipes)| { debug!( - "For project {}, {} pipelines will be canceled", + "For project {}, {} jobs will be canceled", proj.0, pipes.len() ) @@ -92,7 +92,7 @@ mod integration_tests { let config = Config::load_config().unwrap().smtp; - let mail_relay_handle = tokio::spawn(utils::mailrelay_buid(config.clone().unwrap())); + let mail_relay_handle = tokio::spawn(utils::mailrelay_build(config.clone().unwrap())); let test_job = JobInfo::default(); diff --git a/src/utils.rs b/src/utils.rs index 006a1ff..7657445 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,18 +8,20 @@ use crate::SmtpConfig; use log::{error, warn}; /// Build the mail relay -pub async fn mailrelay_buid(smtp_config: SmtpConfig) -> Option { +pub async fn mailrelay_build(smtp_config: SmtpConfig) -> Option { match smtp_config.is_valid() { true => match MailSender::try_new(smtp_config.to_owned()).await { - Ok(mailer) => mailer.relay, + Ok(mailer) => { + debug!("Building mail relay"); + mailer.relay + } Err(error) => { error!("{}", error); return None; } }, false => None, - }; - None + } } /// Build mail message facilitator @@ -35,7 +37,16 @@ pub fn mail_message(job: &JobInfo, reason: MailReason, builder: &SmtpConfig) -> MailReason::Status(status) => format!("Status of job {}: {}", job, status), }; - let to = &job.user_mail; + let binding = builder.to.clone(); + let to = match &job.user_mail { + Some(to) => match &binding { + None => Some(to.to_string()), + Some(to_inc) => Some(format!("{to}; {to_inc}")), + }, + None => binding, + }; + + debug!("Sending mail to {:?}", &to); builder.body_builder(subject, job.to_html(), to) }