Crate muffin

Source
Expand description

Muffin is a background job processing library designed to work with MongoDB as its storage backend.

Requires MongoDB 6 with feature compatibility level set to 6.

§Features

§Example

use std::sync::Arc;

use async_trait::async_trait;
use mongodb::{bson::doc, Client};
use muffin::{Job, ProcessResult, Muffin, Status};
use serde::{Deserialize, Serialize};

struct JobState {
    mongo_client: Client,
}

#[derive(Serialize, Deserialize)]
struct MyJob {
    name: String,
}

#[async_trait]
impl Job for MyJob {
    type State = Arc<JobState>;

    fn id() -> &'static str
    where
       Self: Sized,
    {
        // A unique identifier of the job
        "MyJob"
    }

    async fn process(&self, state: Self::State) -> ProcessResult {
       // Do something with self.name and state
       Ok(Status::Completed)
    }
}

#[tokio::main]
async fn main() -> muffin::Result<()> {
    // Connect to a database called "muffin_testing".
    let client = Client::with_uri_str("mongodb://localhost:27017").await?;
    let database = client.database("muffin_testing");

    // The state is passed to each "process" method on an instance of Job.
    let state = JobState {
        mongo_client: client,
    };
    let mut muffin = Muffin::new(database, Arc::new(state)).await?;

    // Create a new job and push it for processing
    muffin
        .push(MyJob {
            name: "Peter".into(),
        })
        .await?;

    // Register jobs that should be processed.
    muffin.register::<MyJob>();

    // Start processing jobs.
    tokio::select! {
        _ = muffin.run() => {},
        _ = tokio::signal::ctrl_c() => {
            eprintln!("Received ctrl+c!");
        }
    };

    // Need to wait for all in-flight jobs to finish processing.
    muffin.shutdown().await;

    Ok(())
}

You can find other examples in the repository.

Modules§

backoff
Provides implementations of the most common backoff strategies. You can also implement your own.

Structs§

Config
Use a ConfigBuilder to create an instance of Config.
ConfigBuilder
Creates a Config using the builder interface.
Muffin
Muffin is the primary struct the users of the library will interact with. It’s constructed with a configuration (or just a database connection) and a state. The state provides a way of easily passing variables into individual jobs. Since the jobs can be moved between threads, the state must be safe to move between threads as well.

Enums§

Error
JobPersistedStatus
The Job’s status as persisted in the database.
Schedule
Used when scheduling jobs for alter processing using the crate::Muffin::schedule method. Note that the library polls for available jobs and as such jobs might not be executed precisely at the scheduled time.
Status
Status of the job as returned from the process method on an instance of Job.

Traits§

Job
This is the trait that needs to implemented for all job structs. The only required methods are Job::id(), which uniquely identifies a job, and Job::process(), which does the actual processing.

Type Aliases§

ProcessResult
The returned type of the Job::process method. See the Status for more information. Because this is internally just a standard Result, you can use the ? operator (in case of Error, the job will be assigned Status::ErrorObj).
Result