Skip to main content

ShouldQueue

Trait ShouldQueue 

Source
pub trait ShouldQueue {
    // Provided methods
    fn queue(&self) -> &'static str { ... }
    fn delay(&self) -> Option<u64> { ... }
    fn max_retries(&self) -> u32 { ... }
}
Expand description

Marker trait for listeners that should be queued for background processing.

Listeners implementing this trait will not be executed immediately. Instead, they will be pushed to a job queue and processed asynchronously by a worker.

§Example

use ferro_events::{Event, Listener, ShouldQueue, Error, async_trait};

#[derive(Clone)]
struct LargeFileUploaded { path: String }

impl Event for LargeFileUploaded {
    fn name(&self) -> &'static str { "LargeFileUploaded" }
}

struct ProcessUploadedFile;

impl ShouldQueue for ProcessUploadedFile {
    fn queue(&self) -> &'static str {
        "file-processing"
    }
}

#[async_trait]
impl Listener<LargeFileUploaded> for ProcessUploadedFile {
    async fn handle(&self, event: &LargeFileUploaded) -> Result<(), Error> {
        // This will run in a background worker
        println!("Processing file: {}", event.path);
        Ok(())
    }
}

Provided Methods§

Source

fn queue(&self) -> &'static str

The queue name to dispatch this listener to.

Source

fn delay(&self) -> Option<u64>

The number of seconds to delay before processing.

Source

fn max_retries(&self) -> u32

The number of times to retry on failure.

Implementors§