finga
12b117db7e
To supporte more than a single log level `log` and `env_logger` crates are used. For the application to support the different type of argument, the verbose argument definition was adapted.
64 lines
1.9 KiB
Rust
64 lines
1.9 KiB
Rust
use anyhow::Result;
|
|
use chrono::DateTime;
|
|
use clap::Clap;
|
|
use humantime::Duration;
|
|
use log::info;
|
|
|
|
/// Send a message to a message queue
|
|
#[derive(Clap, Debug)]
|
|
pub struct Send {
|
|
/// Set a different priority, priority >= 0
|
|
#[clap(short, long, default_value = "0")]
|
|
pub priority: u32,
|
|
/// Do not block
|
|
#[clap(short, long)]
|
|
pub non_blocking: bool,
|
|
/// Timeout, example "5h 23min 42ms"
|
|
#[clap(short = 'o', long, conflicts_with = "deadline")]
|
|
pub timeout: Option<String>,
|
|
/// Deadline until messages are sent (format: "%Y-%m-%d %H:%M:%S")
|
|
#[clap(short, long, conflicts_with = "timeout")]
|
|
pub deadline: Option<String>,
|
|
/// Name of the queue
|
|
#[clap(value_name = "QUEUE")]
|
|
pub queue: String,
|
|
/// Message to be sent to the queue
|
|
#[clap(value_name = "MESSAGE")]
|
|
pub msg: String,
|
|
}
|
|
|
|
impl Send {
|
|
pub fn run(&self) -> Result<()> {
|
|
let mq = &mut posixmq::OpenOptions::writeonly();
|
|
|
|
if self.non_blocking {
|
|
mq.nonblocking();
|
|
}
|
|
|
|
if let Some(timeout) = &self.timeout {
|
|
mq.open(&self.queue)?.send_timeout(
|
|
self.priority,
|
|
&self.msg.as_bytes(),
|
|
*timeout.parse::<Duration>()?,
|
|
)?;
|
|
|
|
info!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue);
|
|
} else if let Some(deadline) = &self.deadline {
|
|
mq.open(&self.queue)?.send_deadline(
|
|
self.priority,
|
|
&self.msg.as_bytes(),
|
|
DateTime::parse_from_str(deadline, "%Y-%m-%d %H:%M:%S")?.into(),
|
|
)?;
|
|
|
|
info!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue);
|
|
} else {
|
|
mq.open(&self.queue)?
|
|
.send(self.priority, &self.msg.as_bytes())?;
|
|
|
|
info!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|