use super::Error as QueueError;
pub async fn failed(database_url: &str, queue: Option<&str>) -> Result<(), Error> {
let failed = super::failed(database_url, queue).await?;
if failed.is_empty() {
println!("No failed jobs found.");
return Ok(());
}
for job in failed {
println!("{} {}@{} failed at {}", job.id, job.connection, job.queue, job.failed_at);
}
Ok(())
}
pub async fn retry(database_url: &str, ids: &[String]) -> Result<(), Error> {
let all_ids;
let ids = if ids == ["all"] {
all_ids = super::failed(database_url, None).await?.into_iter().map(|job| job.id).collect::<Vec<_>>();
&all_ids
} else {
ids
};
if ids.is_empty() {
println!("No retryable jobs found.");
return Ok(());
}
for id in ids {
if !super::retry(database_url, id).await? {
eprintln!("Unable to find failed job with ID [{id}].");
continue;
}
println!("Failed job [{id}] has been pushed back onto the queue.");
}
Ok(())
}
pub async fn forget(database_url: &str, ids: &[String]) -> Result<(), Error> {
let mut missing = None;
for id in ids {
if !super::forget(database_url, id).await? {
missing.get_or_insert_with(|| id.clone());
continue;
}
println!("Failed job [{id}] deleted successfully.");
}
missing.map_or(Ok(()), |id| Err(Error::MissingFailedJob(id)))
}
pub async fn flush(database_url: &str, hours: Option<u64>, queue: Option<&str>) -> Result<(), Error> {
let deleted = super::flush(database_url, hours, queue).await?;
println!("Deleted {deleted} failed job(s).");
Ok(())
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Queue(#[from] QueueError),
#[error("no failed job matches the ID [{0}]")]
MissingFailedJob(String),
}