Crate effectum

Source
Expand description

A SQLite-based task queue library that allows running background jobs without requiring external dependencies.

use effectum::{Error, Job, JobState, JobRunner, RunningJob, Queue, Worker};

#[derive(Debug)]
pub struct JobContext {
   // database pool or other things here
}

#[derive(Serialize, Deserialize)]
struct RemindMePayload {
  email: String,
  message: String,
}

async fn remind_me_job(job: RunningJob, context: Arc<JobContext>) -> Result<(), Error> {
    let payload: RemindMePayload = job.json_payload()?;
    // do something with the job
    Ok(())
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
  // Create a queue
  let queue = Queue::new(Path::new("effectum.db")).await?;

  // Define a job type for the queue.
  let remind_job = JobRunner::builder("remind_me", remind_me_job).build();

  let context = Arc::new(JobContext{
    // database pool or other things here
  });

  // Create a worker to run jobs.
  let worker = Worker::builder(&queue, context)
    .max_concurrency(10)
    .jobs([remind_job])
    .build();

  // Submit a job to the queue.
  let job_id = Job::builder("remind_me")
    .run_at(time::OffsetDateTime::now_utc() + std::time::Duration::from_secs(10))
    .json_payload(&RemindMePayload {
        email: "me@example.com".to_string(),
        message: "Time to go!".to_string()
    })?
    .add_to(&queue)
    .await?;

  // See what's happening with the job.
  let status = queue.get_job_status(job_id).await?;
  assert_eq!(status.state, JobState::Pending);

  // Do other stuff...

  Ok(())
}

Structs§

Job
A job to be submitted to the queue. Jobs are uniquely identified by their id, so adding a job with the same ID twice will fail. If you want to clone the same Job object and submit it multiple times, use Job::clone_as_new to generate a new ID with each clone.
JobBuilder
A builder for a job to submit to the queue.
JobRegistry
A list of jobs that can be run by a worker.
JobRunner
A definition of a job, including the name of the job, the function that runs the job, and other settings.
JobRunnerBuilder
A builder object for a JobRunner.
JobStatus
Status information about a job.
JobUpdate
Specified fields of a job to be updated, using the Queue::update_job method. All of these fields except the job ID are optional, so the update can set only the desired fields.
JobUpdateBuilder
A builder for a JobUpdate
Queue
The queue itself, which consists of the SQLite connection and tasks to monitor running jobs.
QueueOptions
Options used to configure a Queue instance.
RecurringJobInfo
Information about a recurring job.
Retries
Retries controls the exponential backoff behavior when retrying failed jobs.
RunInfo
Information about the results of a job run.
RunningJob
Information about a running job.
RunningJobData
Information about a running job. This is usually accessed through the RunningJob type, which wraps this in an Arc.
Worker
A worker that runs jobs from the queue. Dropping a worker will disconnect it and stop running jobs.
WorkerBuilder
A builder object for a Worker.

Enums§

Error
Errors that can be returned from the queue.
JobRecoveryBehavior
How to treat jobs which are already marked as running when the queue starts. This accounts for cases where the process is restarted unexpectedly.
JobState
The current state of a job.
RecurringJobSchedule
The schedule definition for a recurring job.

Type Aliases§

Result
A std::result::Result whose error type defaults to Error.