Add test and fix bug

This commit is contained in:
Mubelotix
2025-07-31 16:45:30 +02:00
parent 35537e0b0b
commit ed147f80ac
2 changed files with 77 additions and 13 deletions

View File

@ -828,16 +828,32 @@ impl IndexScheduler {
written: 0,
};
enum EitherRead<T: Read> {
Other(T),
Data(Vec<u8>),
enum EitherRead<'a, T: Read> {
Other(Option<T>),
Data(&'a [u8]),
}
impl<T: Read> Read for &mut EitherRead<T> {
impl<T: Read> EitherRead<'_, T> {
/// A clone that works only once for the Other variant.
fn clone(&mut self) -> Self {
match self {
Self::Other(r) => {
let r = r.take();
Self::Other(r)
}
Self::Data(arg0) => Self::Data(arg0),
}
}
}
impl<T: Read> Read for EitherRead<'_, T> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
EitherRead::Other(reader) => reader.read(buf),
EitherRead::Data(data) => data.as_slice().read(buf),
EitherRead::Other(Some(reader)) => reader.read(buf),
EitherRead::Other(None) => {
Err(io::Error::new(io::ErrorKind::Other, "No reader available"))
}
EitherRead::Data(data) => data.read(buf),
}
}
}
@ -845,16 +861,17 @@ impl IndexScheduler {
let mut reader = GzEncoder::new(BufReader::new(task_reader), Compression::default());
// When there is more than one webhook, cache the data in memory
let mut data;
let mut reader = match webhooks.webhooks.len() {
1 => EitherRead::Other(reader),
1 => EitherRead::Other(Some(reader)),
_ => {
let mut data = Vec::new();
data = Vec::new();
reader.read_to_end(&mut data)?;
EitherRead::Data(data)
EitherRead::Data(&data)
}
};
for (name, Webhook { url, headers }) in webhooks.webhooks.iter() {
for (uuid, Webhook { url, headers }) in webhooks.webhooks.iter() {
let mut request = ureq::post(url)
.timeout(Duration::from_secs(30))
.set("Content-Encoding", "gzip")
@ -863,8 +880,8 @@ impl IndexScheduler {
request = request.set(header_name, header_value);
}
if let Err(e) = request.send(&mut reader) {
tracing::error!("While sending data to the webhook {name}: {e}");
if let Err(e) = request.send(reader.clone()) {
tracing::error!("While sending data to the webhook {uuid}: {e}");
}
}