Skip to main content

JobQueue

Struct JobQueue 

Source
pub struct JobQueue { /* private fields */ }
Expand description

可靠任务队列

基于 sz-orm Pool 实现,不绑定具体数据库后端(MySQL/PostgreSQL 均可, 领取用单条 UPDATE 原子抢占,不依赖 FOR UPDATE SKIP LOCKED 方言)。

Implementations§

Source§

impl JobQueue

Source

pub fn new(pool: Arc<Pool>) -> Self

创建任务队列

Source

pub fn pool(&self) -> &Arc<Pool>

底层连接池引用(观测/测试用)

Source

pub async fn init_schema(&self) -> Result<(), JobQueueError>

幂等建表(可安全重复调用)

Source

pub async fn enqueue( &self, kind: &str, payload: Value, dedupe_key: Option<&str>, ) -> Result<u64, JobQueueError>

入队任务(立即执行)。dedupe_key 同 kind 下重复时返回已存在任务 ID,不重复入队。

Source

pub async fn enqueue_delayed( &self, kind: &str, payload: Value, dedupe_key: Option<&str>, delay: Duration, ) -> Result<u64, JobQueueError>

入队延迟任务(delay 后执行)——退避/定时不靠 worker sleep,靠 run_after

Source

pub async fn retry_dead(&self, job_id: u64) -> Result<(), JobQueueError>

死信重放:将 dead 任务重新置为 pending(保留 attempts 与错误历史)

Source

pub async fn queue_snapshot(&self) -> Result<QueueSnapshot, JobQueueError>

队列健康快照(pending/running/dead/最老等待/累计完成)

Source

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§

Source§

impl Clone for JobQueue

Source§

fn clone(&self) -> JobQueue

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more