pub struct JobQueue { /* private fields */ }Expand description
可靠任务队列
基于 sz-orm Pool 实现,不绑定具体数据库后端(MySQL/PostgreSQL 均可,
领取用单条 UPDATE 原子抢占,不依赖 FOR UPDATE SKIP LOCKED 方言)。
Implementations§
Source§impl JobQueue
impl JobQueue
Sourcepub async fn init_schema(&self) -> Result<(), JobQueueError>
pub async fn init_schema(&self) -> Result<(), JobQueueError>
幂等建表(可安全重复调用)
Sourcepub async fn enqueue(
&self,
kind: &str,
payload: Value,
dedupe_key: Option<&str>,
) -> Result<u64, JobQueueError>
pub async fn enqueue( &self, kind: &str, payload: Value, dedupe_key: Option<&str>, ) -> Result<u64, JobQueueError>
入队任务(立即执行)。dedupe_key 同 kind 下重复时返回已存在任务 ID,不重复入队。
Sourcepub async fn enqueue_delayed(
&self,
kind: &str,
payload: Value,
dedupe_key: Option<&str>,
delay: Duration,
) -> Result<u64, JobQueueError>
pub async fn enqueue_delayed( &self, kind: &str, payload: Value, dedupe_key: Option<&str>, delay: Duration, ) -> Result<u64, JobQueueError>
入队延迟任务(delay 后执行)——退避/定时不靠 worker sleep,靠 run_after
Sourcepub async fn retry_dead(&self, job_id: u64) -> Result<(), JobQueueError>
pub async fn retry_dead(&self, job_id: u64) -> Result<(), JobQueueError>
死信重放:将 dead 任务重新置为 pending(保留 attempts 与错误历史)
Sourcepub async fn queue_snapshot(&self) -> Result<QueueSnapshot, JobQueueError>
pub async fn queue_snapshot(&self) -> Result<QueueSnapshot, JobQueueError>
队列健康快照(pending/running/dead/最老等待/累计完成)
Sourcepub async fn run_worker(
&self,
handlers: HashMap<String, Arc<dyn TaskHandler>>,
config: JobQueueConfig,
shutdown: Receiver<bool>,
) -> Result<(), JobQueueError>
pub async fn run_worker( &self, handlers: HashMap<String, Arc<dyn TaskHandler>>, config: JobQueueConfig, shutdown: Receiver<bool>, ) -> Result<(), JobQueueError>
启动 worker:轮询领取 → 分发 handler → 成功/退避重试/死信。
shutdown 为 true 时退出(优雅关闭)。
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for JobQueue
impl !UnwindSafe for JobQueue
impl Freeze for JobQueue
impl Send for JobQueue
impl Sync for JobQueue
impl Unpin for JobQueue
impl UnsafeUnpin for JobQueue
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> ⓘ
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