Crates.io | effectum |
lib.rs | effectum |
version | 0.7.0 |
source | src |
created_at | 2022-12-03 19:37:49.225039 |
updated_at | 2024-07-23 18:39:36.682011 |
description | An embeddable task queue based on SQLite |
homepage | |
repository | https://github.com/dimfeld/effectum |
max_upload_size | |
id | 729222 |
size | 300,348 |
A Rust job queue library, based on SQLite so it doesn't depend on any other services.
Currently this is just a library embeddable into Rust applications, but future goals include bindings into other languages and the ability to run as a standalone server, accessible by HTTP and gRPC. This will be designed so that a product can start with the embedded version to use minimal infrastructure, and then move to the server version with minimal changes when the time comes to scale out.
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(&PathBuf::from("effectum.db")).await?;
// Define a type job for the queue.
let a_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([a_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(3600))
.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(())
}