Skip to main content

aurelin_framework/queue/
commands.rs

1//! Application-console entry points for failed queue jobs.
2
3use super::Error as QueueError;
4
5/// List terminal failed jobs.
6pub async fn failed(database_url: &str, queue: Option<&str>) -> Result<(), Error> {
7    let failed = super::failed(database_url, queue).await?;
8    if failed.is_empty() {
9        println!("No failed jobs found.");
10        return Ok(());
11    }
12    for job in failed {
13        println!("{}  {}@{}  failed at {}", job.id, job.connection, job.queue, job.failed_at);
14    }
15    Ok(())
16}
17
18/// Retry terminal failed jobs.
19pub async fn retry(database_url: &str, ids: &[String]) -> Result<(), Error> {
20    let all_ids;
21    let ids = if ids == ["all"] {
22        all_ids = super::failed(database_url, None).await?.into_iter().map(|job| job.id).collect::<Vec<_>>();
23        &all_ids
24    } else {
25        ids
26    };
27    if ids.is_empty() {
28        println!("No retryable jobs found.");
29        return Ok(());
30    }
31    for id in ids {
32        if !super::retry(database_url, id).await? {
33            eprintln!("Unable to find failed job with ID [{id}].");
34            continue;
35        }
36        println!("Failed job [{id}] has been pushed back onto the queue.");
37    }
38    Ok(())
39}
40
41/// Delete terminal failed jobs.
42pub async fn forget(database_url: &str, ids: &[String]) -> Result<(), Error> {
43    let mut missing = None;
44    for id in ids {
45        if !super::forget(database_url, id).await? {
46            missing.get_or_insert_with(|| id.clone());
47            continue;
48        }
49        println!("Failed job [{id}] deleted successfully.");
50    }
51    missing.map_or(Ok(()), |id| Err(Error::MissingFailedJob(id)))
52}
53
54/// Delete terminal failed jobs matching the retention and queue filters.
55pub async fn flush(database_url: &str, hours: Option<u64>, queue: Option<&str>) -> Result<(), Error> {
56    let deleted = super::flush(database_url, hours, queue).await?;
57    println!("Deleted {deleted} failed job(s).");
58    Ok(())
59}
60
61/// Failure from an operational queue command.
62#[derive(Debug, thiserror::Error)]
63pub enum Error {
64    /// The queue backend rejected the operation.
65    #[error(transparent)]
66    Queue(#[from] QueueError),
67    /// No terminal failure has the requested identifier.
68    #[error("no failed job matches the ID [{0}]")]
69    MissingFailedJob(String),
70}