Support timeout and deadline parameters for send
Add support for timeout and deadline parameters when messages are sent to a message queue. To keep code duplication small, the `utils.rs` file contains a helper function to parse the `Duration` from the `String` received from the timeout parameter. Add conflict rules for the timeout and deadline parameters.
This commit is contained in:
parent
9b52305285
commit
1100ba1b13
4 changed files with 56 additions and 16 deletions
17
src/recv.rs
17
src/recv.rs
|
@ -1,8 +1,9 @@
|
|||
use crate::utils;
|
||||
use anyhow::Result;
|
||||
use chrono::{DateTime, Local};
|
||||
use clap::Clap;
|
||||
use posixmq::PosixMq;
|
||||
use std::{str, time::Duration};
|
||||
use std::str;
|
||||
|
||||
/// Receive and print a message from a message queue
|
||||
#[derive(Clap, Debug)]
|
||||
|
@ -17,10 +18,10 @@ pub struct Recv {
|
|||
#[clap(short, long)]
|
||||
pub timestamp: bool,
|
||||
/// Timeout in "<timeout>[s]" (default) or "<timeout>ms"
|
||||
#[clap(short = 'o', long)]
|
||||
#[clap(short = 'o', long, conflicts_with = "deadline")]
|
||||
pub timeout: Option<String>,
|
||||
/// Deadline until messages are received (format: "%Y-%m-%d %H:%M:%S")
|
||||
#[clap(short, long)]
|
||||
#[clap(short, long, conflicts_with = "timeout")]
|
||||
pub deadline: Option<String>,
|
||||
#[clap(value_name = "QUEUE")]
|
||||
pub queue: String,
|
||||
|
@ -43,15 +44,7 @@ impl Recv {
|
|||
let mut buf = vec![0; mq.attributes()?.max_msg_len];
|
||||
|
||||
if let Some(timeout) = &self.timeout {
|
||||
let timeout = if timeout.ends_with("ms") {
|
||||
Duration::from_millis(timeout[0..timeout.len() - 2].parse::<u64>()?)
|
||||
} else if timeout.ends_with("s") {
|
||||
Duration::from_secs(timeout[0..timeout.len() - 1].parse::<u64>()?)
|
||||
} else {
|
||||
Duration::from_secs(timeout.parse::<u64>()?)
|
||||
};
|
||||
|
||||
let (prio, len) = mq.recv_timeout(&mut buf, timeout)?;
|
||||
let (prio, len) = mq.recv_timeout(&mut buf, utils::parse_duration(timeout)?)?;
|
||||
|
||||
print_message(verbose, prio, len, self.timestamp, str::from_utf8(&buf)?);
|
||||
} else if let Some(deadline) = &self.deadline {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue