Module custom

Module custom 

Source
Expand description

§Custom Backend

A highly customizable backend for task processing that allows integration with any persistence engine by providing custom fetcher and sink functions.

§Overview

The CustomBackend struct enables you to define how tasks are fetched from and persisted to your storage engine.

You can use the BackendBuilder to construct a CustomBackend by providing the required database, fetcher, sink, and optional configuration and codec.

§Usage

Use BackendBuilder to configure and build your custom backend:

§Example: CustomBackend with Worker

#[tokio::main]
async fn main() {
    // Create a memory-backed VecDeque
    let memory = Arc::new(Mutex::new(VecDeque::<Task<u32, ()>>::new()));

    // Build the custom backend
    let mut backend = BackendBuilder::new()
        .database(memory)
        .fetcher(|memory, _, _| {
            stream::unfold(memory.clone(), |p| async move {
                let mut memory = p.lock().await;
                let item = memory.pop_front();
                drop(memory);
                match item {
                    Some(item) => Some((Ok::<_, BoxDynError>(Some(item)), p)),
                    None => Some((Ok::<_, BoxDynError>(None), p)),
                }
            })
            .boxed()
        })
        .sink(|memory, _| {
            sink::unfold(memory.clone(), move |p, item| {
                async move {
                    let mut memory = p.lock().await;
                    memory.push_back(item);
                    drop(memory);
                    Ok::<_, BoxDynError>(p)
                }
                .boxed()
            })
        })
        .build()
        .unwrap();

    // Add a task to the backend
    backend.send(Task::new(42)).await.unwrap();

    // Define the task handler
    async fn task(task: u32, ctx: WorkerContext) -> Result<(), BoxDynError> {
        tokio::time::sleep(Duration::from_secs(1)).await;
        Ok(())
    }

    // Build and run the worker
    let worker = WorkerBuilder::new("custom-worker")
        .backend(backend)
        .build(task);
    worker.run().await.unwrap();
}

§Features

  • Custom Fetcher: Define how jobs are fetched from your storage.
  • Custom Sink: Define how jobs are persisted to your storage.
  • Configurable: Pass custom configuration to your backend.

Structs§

BackendBuilder
Builder for CustomBackend
CustomBackend
A highly customizable backend for integration with any persistence engine

Enums§

BuildError
Errors encountered building a CustomBackend