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
impl SchedulerRuntime
Sourcepub fn new(config: SchedulerRuntimeConfig) -> Self
pub fn new(config: SchedulerRuntimeConfig) -> Self
创建调度器运行时
Sourcepub fn start(&self, token: CancellationToken) -> JoinHandle<()>
pub fn start(&self, token: CancellationToken) -> JoinHandle<()>
启动调度循环(返回 JoinHandle,调用方持有)
§行为
- 每
tick_ms毫秒调用scheduler.try_fire_due(now)触发到期任务 - 监听
token.cancelled(),收到信号后停止循环 - 不调用
CronScheduler::stop()(避免JoinHandle::join()阻塞)
Sourcepub fn schedule(&self, task: ScheduledTask) -> Result<(), SchedulerError>
pub fn schedule(&self, task: ScheduledTask) -> Result<(), SchedulerError>
注册调度任务(委托给 CronScheduler::schedule)
Sourcepub fn cancel(&self, task_id: &str) -> Result<(), SchedulerError>
pub fn cancel(&self, task_id: &str) -> Result<(), SchedulerError>
取消任务(委托给 CronScheduler::cancel)
Sourcepub fn pause(&self, task_id: &str) -> Result<(), SchedulerError>
pub fn pause(&self, task_id: &str) -> Result<(), SchedulerError>
暂停任务(委托给 CronScheduler::pause)
Sourcepub fn resume(&self, task_id: &str) -> Result<(), SchedulerError>
pub fn resume(&self, task_id: &str) -> Result<(), SchedulerError>
恢复任务(委托给 CronScheduler::resume)
Sourcepub fn list_tasks(&self) -> Vec<ScheduledTask>
pub fn list_tasks(&self) -> Vec<ScheduledTask>
列出所有任务(委托给 CronScheduler::list_tasks)
Sourcepub fn register_handler(
&self,
task_id: impl Into<String>,
handler: Arc<dyn JobHandler>,
)
pub fn register_handler( &self, task_id: impl Into<String>, handler: Arc<dyn JobHandler>, )
注册任务处理器(委托给 CronScheduler::register_handler)
Sourcepub fn try_fire_due(&self) -> usize
pub fn try_fire_due(&self) -> usize
手动触发一次到期任务检查(对齐 scheduler:run 命令)
Sourcepub fn task_count(&self) -> usize
pub fn task_count(&self) -> usize
获取任务数量
Sourcepub fn config(&self) -> &SchedulerRuntimeConfig
pub fn config(&self) -> &SchedulerRuntimeConfig
获取配置
Sourcepub fn scheduler(&self) -> &CronScheduler
pub fn scheduler(&self) -> &CronScheduler
获取内部 CronScheduler 引用(用于直接调用未暴露的方法)
Auto Trait Implementations§
impl Freeze for SchedulerRuntime
impl RefUnwindSafe for SchedulerRuntime
impl Send for SchedulerRuntime
impl Sync for SchedulerRuntime
impl Unpin for SchedulerRuntime
impl UnsafeUnpin for SchedulerRuntime
impl UnwindSafe for SchedulerRuntime
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> 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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.