Skip to main content

SchedulerRuntime

Struct SchedulerRuntime 

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

调度器运行时

封装 sz_orm_scheduler::CronScheduler,提供 tokio 兼容的调度循环。

§设计

  • 不调用 CronScheduler::start()(因为它用 std::thread::spawn,会占用阻塞线程)
  • 改用 tokio::time::interval + try_fire_due() 在 tokio 任务中循环
  • 监听 CancellationToken 优雅退出
  • 保留 CronScheduler 的所有 API(schedule/cancel/pause/resume/list_tasks/register_handler)

§用法

use sz_rust_core::runtime::scheduler::{SchedulerRuntime, SchedulerRuntimeConfig};
use sz_orm_scheduler::ScheduledTask;
use tokio_util::sync::CancellationToken;

let runtime = SchedulerRuntime::new(SchedulerRuntimeConfig::default());
let task = ScheduledTask::new("task-1", "每分钟任务", "0 * * * *")
    .with_callback("demo::minute_task");
runtime.schedule(task).unwrap();

let token = CancellationToken::new();
let handle = runtime.start(token.clone());
// ... 业务运行 ...
token.cancel();
let _ = handle.await;

Implementations§

Source§

impl SchedulerRuntime

Source

pub fn new(config: SchedulerRuntimeConfig) -> Self

创建调度器运行时

Source

pub fn start(&self, token: CancellationToken) -> JoinHandle<()>

启动调度循环(返回 JoinHandle,调用方持有)

§行为
  1. tick_ms 毫秒调用 scheduler.try_fire_due(now) 触发到期任务
  2. 监听 token.cancelled(),收到信号后停止循环
  3. 不调用 CronScheduler::stop()(避免 JoinHandle::join() 阻塞)
Source

pub fn schedule(&self, task: ScheduledTask) -> Result<(), SchedulerError>

注册调度任务(委托给 CronScheduler::schedule

Source

pub fn cancel(&self, task_id: &str) -> Result<(), SchedulerError>

取消任务(委托给 CronScheduler::cancel

Source

pub fn pause(&self, task_id: &str) -> Result<(), SchedulerError>

暂停任务(委托给 CronScheduler::pause

Source

pub fn resume(&self, task_id: &str) -> Result<(), SchedulerError>

恢复任务(委托给 CronScheduler::resume

Source

pub fn list_tasks(&self) -> Vec<ScheduledTask>

列出所有任务(委托给 CronScheduler::list_tasks

Source

pub fn register_handler( &self, task_id: impl Into<String>, handler: Arc<dyn JobHandler>, )

注册任务处理器(委托给 CronScheduler::register_handler

Source

pub fn try_fire_due(&self) -> usize

手动触发一次到期任务检查(对齐 scheduler:run 命令)

Source

pub fn task_count(&self) -> usize

获取任务数量

Source

pub fn config(&self) -> &SchedulerRuntimeConfig

获取配置

Source

pub fn scheduler(&self) -> &CronScheduler

获取内部 CronScheduler 引用(用于直接调用未暴露的方法)

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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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