Skip to content

Commit

Permalink
Merge branch 'main' into gh_main
Browse files Browse the repository at this point in the history
  • Loading branch information
dyegomb committed Nov 14, 2024
2 parents 5be62fc + a0fa3a3 commit 0259ad4
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 64 deletions.
22 changes: 17 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 7 additions & 7 deletions configloader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
toml = { workspace = true }
envy = { workspace = true }
merge = { workspace = true }
serde = { workspace = true }
log = { workspace = true }
env_logger = { workspace = true }
lettre = { workspace = true }
16 changes: 7 additions & 9 deletions gitlabapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
env_logger = "0.10"
11 changes: 5 additions & 6 deletions mailsender/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
2 changes: 1 addition & 1 deletion mailsender/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ This is a <b>test message</b>. :-)
debug!("{:?}", mail_message);
}

#[tokio::test]
#[tokio::test(flavor = "current_thread")]
#[ignore = "It'll really send an email message"]
async fn test_send_mail() {
init();
Expand Down
4 changes: 2 additions & 2 deletions mailsender/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub trait SmtpUtils {
&self,
subject: String,
message: String,
destination: &Option<String>,
destination: Option<String>,
) -> Message;
}

Expand All @@ -38,7 +38,7 @@ impl SmtpUtils for SmtpConfig {
&self,
subject: String,
message: String,
destination: &Option<String>,
destination: Option<String>,
) -> Message {
if !self.is_valid() {
error!("Smtp configuration is invalid");
Expand Down
67 changes: 40 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,8 +75,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// 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);
Expand Down Expand Up @@ -116,7 +117,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}

// 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 = [
Expand All @@ -126,8 +136,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
JobScope::Manual,
];

let mails_handler = Rc::new(RefCell::new(vec![]));

// Stream to monitor jobs' status
let monitor_jobs = stream::iter(actions)
.map(|result| async {
Expand Down Expand Up @@ -166,14 +174,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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);
Expand All @@ -186,14 +196,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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;
Expand Down Expand Up @@ -242,11 +259,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// 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(())
}
4 changes: 2 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand All @@ -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();

Expand Down
21 changes: 16 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ use crate::SmtpConfig;
use log::{error, warn};

/// Build the mail relay
pub async fn mailrelay_buid(smtp_config: SmtpConfig) -> Option<SmtpTransport> {
pub async fn mailrelay_build(smtp_config: SmtpConfig) -> Option<SmtpTransport> {
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
Expand All @@ -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)
}
Expand Down

0 comments on commit 0259ad4

Please sign in to comment.