backyard_core/lib.rs
1//! # Backyard Core
2//!
3//! Core traits and types for the Backyard async job queue.
4//!
5//! This crate provides the fundamental abstractions that make Backyard backend-agnostic:
6//! - [`Job`] trait for defining background jobs
7//! - [`Queue`] trait for implementing backends (SQLite, Redis, etc.)
8//! - [`WorkerPool`] for processing jobs with Tokio
9//!
10//! ## Quick Start
11//!
12//! Define a job by implementing [`Job`]:
13//!
14//! ```ignore
15//! use backyard_core::{Job, JobContext, Result};
16//! use async_trait::async_trait;
17//! use serde::{Serialize, Deserialize};
18//!
19//! #[derive(Serialize, Deserialize)]
20//! struct SendEmail {
21//! to: String,
22//! }
23//!
24//! #[async_trait]
25//! impl Job for SendEmail {
26//! const NAME: &'static str = "SendEmail";
27//!
28//! async fn execute(self, ctx: &JobContext) -> Result<()> {
29//! println!("Sending email to {}", self.to);
30//! Ok(())
31//! }
32//! }
33//! ```
34//!
35//! Then use with a backend like `backyard-sqlite`:
36//!
37//! ```ignore
38//! use backyard_sqlite::SqliteQueue;
39//! use backyard_core::WorkerBuilder;
40//!
41//! #[tokio::main]
42//! async fn main() -> Result<()> {
43//! let queue = SqliteQueue::new(Default::default()).await?;
44//!
45//! // Enqueue a job
46//! let job = SendEmail { to: "user@example.com".into() };
47//! let payload = serde_json::to_vec(&job)?;
48//! let req = backyard_core::queue::EnqueueRequest {
49//! job_type: SendEmail::NAME.to_string(),
50//! queue: "default".to_string(),
51//! payload,
52//! max_retries: 3,
53//! priority: 0,
54//! scheduled_at: chrono::Utc::now(),
55//! };
56//! queue.push(req).await?;
57//!
58//! // Run the worker
59//! WorkerBuilder::new(queue)
60//! .concurrency(10)
61//! .run()
62//! .await?;
63//!
64//! Ok(())
65//! }
66//! ```
67
68pub mod error;
69pub mod job;
70pub mod options;
71pub mod queue;
72pub mod registry;
73pub mod retry;
74pub mod worker;
75
76pub use error::{BackyardError, Result};
77pub use job::{Job, JobContext, JobId, RawJob};
78pub use options::JobOptions;
79pub use queue::Queue;
80pub use registry::build_dispatch_table;
81pub use worker::{WorkerConfig, WorkerPool};
82
83pub use inventory;