pub struct JobQueue<DB: Database> {
pub pool: Pool<DB>,
/* private fields */
}
Expand description
A generic job queue implementation that works with multiple database backends.
This struct provides a database-agnostic interface to the job queue functionality.
The actual database operations are delegated to database-specific implementations
based on the type parameter DB
.
§Examples
use hammerwork::{JobQueue, Job, queue::DatabaseQueue};
use serde_json::json;
// Create a PostgreSQL-backed queue
let pool = sqlx::PgPool::connect("postgresql://localhost/hammerwork").await?;
let queue = JobQueue::new(pool);
// Create and enqueue a job
let job = Job::new("email_queue".to_string(), json!({"to": "user@example.com"}));
let job_id = queue.enqueue(job).await?;
Fields§
§pool: Pool<DB>
Implementations§
Source§impl<DB: Database> JobQueue<DB>
impl<DB: Database> JobQueue<DB>
Sourcepub async fn set_throttle(
&self,
queue_name: &str,
config: ThrottleConfig,
) -> Result<()>
pub async fn set_throttle( &self, queue_name: &str, config: ThrottleConfig, ) -> Result<()>
Set throttling configuration for a specific queue.
This is a convenience method that stores the throttling configuration in memory for use by workers.
§Arguments
queue_name
- The name of the queue to configureconfig
- The throttling configuration
§Examples
use hammerwork::{JobQueue, rate_limit::ThrottleConfig};
use std::time::Duration;
let pool = sqlx::PgPool::connect("postgresql://localhost/hammerwork").await?;
let queue = JobQueue::new(pool);
let config = ThrottleConfig::new()
.max_concurrent(5)
.rate_per_minute(100);
queue.set_throttle("email_queue", config).await?;
Sourcepub async fn get_throttle(&self, queue_name: &str) -> Option<ThrottleConfig>
pub async fn get_throttle(&self, queue_name: &str) -> Option<ThrottleConfig>
Sourcepub async fn remove_throttle(&self, queue_name: &str) -> Result<()>
pub async fn remove_throttle(&self, queue_name: &str) -> Result<()>
Sourcepub async fn get_all_throttles(&self) -> HashMap<String, ThrottleConfig>
pub async fn get_all_throttles(&self) -> HashMap<String, ThrottleConfig>
Get all throttling configurations.
§Returns
A map of queue names to their throttling configurations.
Sourcepub fn get_pool(&self) -> &Pool<DB>
pub fn get_pool(&self) -> &Pool<DB>
Get a reference to the underlying database connection pool.
This method provides access to the database pool for advanced use cases that require direct database operations or integration with other components.
§Examples
use hammerwork::JobQueue;
let queue = JobQueue::new(pool);
// Access the pool for advanced operations
let pool_ref = &queue.pool;
let pool_clone = queue.pool.clone();
// Use the pool for custom queries or integration with other components
let row_count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM hammerwork_jobs")
.fetch_one(&queue.pool)
.await?;
println!("Total jobs in database: {}", row_count.0);
Auto Trait Implementations§
impl<DB> Freeze for JobQueue<DB>
impl<DB> !RefUnwindSafe for JobQueue<DB>
impl<DB> Send for JobQueue<DB>
impl<DB> Sync for JobQueue<DB>where
DB: Sync,
impl<DB> Unpin for JobQueue<DB>where
DB: Unpin,
impl<DB> !UnwindSafe for JobQueue<DB>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more