Expand description
A SQLite-based task queue library that allows running background jobs without requiring external dependencies.
use prefect::{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().unwrap();
// 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("prefect.db")).await?;
// Define a job type 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()
})
.unwrap()
.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
A job to be submitted to the queue.
A builder for a job to submit to the queue.
A list of jobs that can be run by a worker.
A definition of a job, including the name of the job, the function that runs the job, and
other settings.
A builder object for a JobRunner.
Status information about a job.
The queue itself, which consists of the SQLite connection and tasks to monitor running jobs.
Retries controls the exponential backoff behavior when retrying failed jobs.Information about the results of a job run.
Information about a running job.
Information about a running job. This is usually accessed through the RunningJob type,
which wraps this in an Arc.
A worker that runs jobs from the queue.
A builder object for a Worker.
Enums
Type Definitions
A std::result::Result whose error type defaults to Error.