pub struct JobsRepo { /* private fields */ }Expand description
Repository providing atomic database operations for job queue management.
Handles enqueueing, transactional batch leasing (SKIP LOCKED), execution completion,
re-scheduling retries, moving jobs to DLQ, and replay.
Implementations§
Source§impl JobsRepo
impl JobsRepo
Sourcepub fn new(pool: Pool<Postgres>) -> JobsRepo
pub fn new(pool: Pool<Postgres>) -> JobsRepo
Creates a new JobsRepo wrapping a SQLx PostgreSQL connection pool.
Sourcepub fn new_with_url(
pool: Pool<Postgres>,
database_url: impl Into<String>,
) -> JobsRepo
pub fn new_with_url( pool: Pool<Postgres>, database_url: impl Into<String>, ) -> JobsRepo
Creates a new JobsRepo with a dedicated database_url for unpooled LISTEN connections.
Sourcepub async fn enqueue(&self, job: NewJob) -> Result<Uuid, Error>
pub async fn enqueue(&self, job: NewJob) -> Result<Uuid, Error>
Inserts a new job into the queue database.
Automatically routes the job to the appropriate dataset partition based on run_at.
§Examples
use azums::{JobsRepo, NewJob, make_pool};
use chrono::Utc;
let pool = make_pool("postgres://localhost/flow").await?;
let repo = JobsRepo::new(pool);
let job_id = repo.enqueue(NewJob {
queue: "default".to_string(),
job_type: "email_send".to_string(),
payload_json: serde_json::json!({"to": "user@example.com"}),
run_at: Utc::now(),
priority: 0,
max_attempts: 5,
}).await?;Sourcepub async fn enqueue_now(
&self,
queue: &str,
job_type: &str,
payload_json: Value,
) -> Result<Uuid, Error>
pub async fn enqueue_now( &self, queue: &str, job_type: &str, payload_json: Value, ) -> Result<Uuid, Error>
Enqueues a job for immediate execution (run_at = Utc::now()).
§Examples
use azums::{JobsRepo, make_pool};
let pool = make_pool("postgres://localhost/flow").await?;
let repo = JobsRepo::new(pool);
let id = repo.enqueue_now("default", "send_welcome", serde_json::json!({"id": 123})).await?;Sourcepub async fn enqueue_in(
&self,
queue: &str,
job_type: &str,
payload_json: Value,
delay_secs: i64,
) -> Result<Uuid, Error>
pub async fn enqueue_in( &self, queue: &str, job_type: &str, payload_json: Value, delay_secs: i64, ) -> Result<Uuid, Error>
Schedules a job for future execution delayed by delay_secs seconds.
§Examples
use azums::{JobsRepo, make_pool};
let pool = make_pool("postgres://localhost/flow").await?;
let repo = JobsRepo::new(pool);
let id = repo.enqueue_in("default", "send_reminder", serde_json::json!({}), 300).await?;Sourcepub async fn enqueue_at(
&self,
queue: &str,
job_type: &str,
payload_json: Value,
run_at: DateTime<Utc>,
) -> Result<Uuid, Error>
pub async fn enqueue_at( &self, queue: &str, job_type: &str, payload_json: Value, run_at: DateTime<Utc>, ) -> Result<Uuid, Error>
Schedules a job to run at a specific UTC timestamp (run_at).
§Examples
use azums::{JobsRepo, make_pool};
use chrono::Utc;
let pool = make_pool("postgres://localhost/flow").await?;
let repo = JobsRepo::new(pool);
let run_at = Utc::now() + chrono::Duration::hours(1);
let id = repo.enqueue_at("default", "scheduled_report", serde_json::json!({}), run_at).await?;Sourcepub async fn get_job(&self, job_id: Uuid) -> Result<Option<Job>, Error>
pub async fn get_job(&self, job_id: Uuid) -> Result<Option<Job>, Error>
Fetches a single Job record by primary key job_id.
Sourcepub async fn extend_lease(
&self,
job_id: Uuid,
worker_id: &str,
lease_seconds: i64,
) -> Result<bool, Error>
pub async fn extend_lease( &self, job_id: Uuid, worker_id: &str, lease_seconds: i64, ) -> Result<bool, Error>
Extends the lock expiration timestamp for a running job.
Sourcepub async fn list_jobs(
&self,
queue: Option<&str>,
status: Option<&str>,
limit: i64,
cursor_created_at: Option<DateTime<Utc>>,
cursor_id: Option<Uuid>,
) -> Result<Vec<JobListItem>, Error>
pub async fn list_jobs( &self, queue: Option<&str>, status: Option<&str>, limit: i64, cursor_created_at: Option<DateTime<Utc>>, cursor_id: Option<Uuid>, ) -> Result<Vec<JobListItem>, Error>
Cursor-paginated list of jobs. Cursor is (created_at, id) ordered DESC.
- queue/status are optional filters
- limit is clamped to [1, 500]
Sourcepub async fn metrics_snapshot(&self) -> Result<(i64, i64, i64, i64), Error>
pub async fn metrics_snapshot(&self) -> Result<(i64, i64, i64, i64), Error>
Returns: (queued, running, succeeded_last_60s, failed_or_dlq_last_60s)
Sourcepub async fn lease_jobs_batch(
&self,
queue: &str,
worker_id: &str,
lease_seconds: i64,
batch_size: i64,
) -> Result<Vec<Job>, Error>
pub async fn lease_jobs_batch( &self, queue: &str, worker_id: &str, lease_seconds: i64, batch_size: i64, ) -> Result<Vec<Job>, Error>
Lease up to batch_size runnable jobs for this worker.
Correctness: SELECT … FOR UPDATE SKIP LOCKED
Storm-control gates (per queue):
- max_in_flight (jobs.status=‘running’)
- max_attempts_per_minute (attempts started in last 60s)
If exceeded:
- write a row into policy_decisions
- reschedule one candidate slightly (throttle_delay_ms)
- return an empty batch
Sourcepub async fn lease_jobs_batch_with_ordering(
&self,
queue: &str,
worker_id: &str,
lease_seconds: i64,
batch_size: i64,
ordering: QueueOrdering,
) -> Result<Vec<Job>, Error>
pub async fn lease_jobs_batch_with_ordering( &self, queue: &str, worker_id: &str, lease_seconds: i64, batch_size: i64, ordering: QueueOrdering, ) -> Result<Vec<Job>, Error>
Lease up to batch_size runnable jobs for this worker using specified azums_core::QueueOrdering.
Sourcepub async fn lease_one_job(
&self,
queue: &str,
worker_id: &str,
lease_seconds: i64,
) -> Result<Option<Job>, Error>
pub async fn lease_one_job( &self, queue: &str, worker_id: &str, lease_seconds: i64, ) -> Result<Option<Job>, Error>
Compatibility helper for call sites/tests that still lease one-by-one.
pub async fn reap_expired_locks(&self) -> Result<u64, Error>
Sourcepub async fn mark_succeeded_batch(
&self,
job_ids: &[Uuid],
worker_id: &str,
) -> Result<u64, Error>
pub async fn mark_succeeded_batch( &self, job_ids: &[Uuid], worker_id: &str, ) -> Result<u64, Error>
Fast-path for successful batch execution: transitions many jobs in one statement.
Sourcepub async fn mark_succeeded_batch_for_dataset(
&self,
dataset_id: &str,
job_ids: &[Uuid],
worker_id: &str,
) -> Result<u64, Error>
pub async fn mark_succeeded_batch_for_dataset( &self, dataset_id: &str, job_ids: &[Uuid], worker_id: &str, ) -> Result<u64, Error>
Dataset-aware fast-path for partition-pruned successful batch updates.
pub async fn mark_succeeded( &self, job_id: Uuid, worker_id: &str, ) -> Result<(), Error>
pub async fn reschedule_for_retry( &self, job_id: Uuid, next_run_at: DateTime<Utc>, last_error_code: Option<&str>, last_error_message: Option<&str>, ) -> Result<(), Error>
pub async fn mark_failed( &self, job_id: Uuid, worker_id: &str, last_error_code: Option<&str>, last_error_message: Option<&str>, ) -> Result<(), Error>
pub async fn mark_dlq( &self, job_id: Uuid, worker_id: &str, reason_code: &str, last_error_code: Option<&str>, last_error_message: Option<&str>, ) -> Result<(), Error>
pub async fn replay_job( &self, job_id: Uuid, override_queue: Option<&str>, override_run_at: Option<DateTime<Utc>>, ) -> Result<Uuid, Error>
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for JobsRepo
impl !UnwindSafe for JobsRepo
impl Freeze for JobsRepo
impl Send for JobsRepo
impl Sync for JobsRepo
impl Unpin for JobsRepo
impl UnsafeUnpin for JobsRepo
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> ⓘ
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> ⓘ
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