pub struct SpawnTask { /* private fields */ }Expand description
Spawn 任务
用于包装已经构建好的 Future(例如 gRPC server) 用户可以在 service 层构建好 Future,然后通过 runtime 管理
§特性
- 支持依赖声明
- 支持 shutdown 信号
- 支持优先级设置
- 支持关键任务标记
§示例
§不需要 shutdown 的任务
use flare_core_runtime::task::SpawnTask;
let task = SpawnTask::new("my-task", async {
// 任务逻辑
Ok(())
});§需要 shutdown 的任务
use flare_core_runtime::task::SpawnTask;
let task = SpawnTask::with_shutdown("my-grpc", |shutdown_rx| {
async move {
// 使用 shutdown_rx 实现优雅停机
let _ = shutdown_rx.await;
Ok(())
}
});§带依赖的任务
use flare_core_runtime::task::SpawnTask;
let task = SpawnTask::new("task-b", async { Ok(()) })
.with_dependencies(vec!["task-a".to_string()])
.with_priority(10)
.with_critical(true);Implementations§
Source§impl SpawnTask
impl SpawnTask
Sourcepub fn with_shutdown<F, Fut>(name: impl Into<String>, future_fn: F) -> Self
pub fn with_shutdown<F, Fut>(name: impl Into<String>, future_fn: F) -> Self
Sourcepub fn with_dependencies(self, deps: Vec<String>) -> Self
pub fn with_dependencies(self, deps: Vec<String>) -> Self
Sourcepub fn with_priority(self, priority: i32) -> Self
pub fn with_priority(self, priority: i32) -> Self
Sourcepub fn with_critical(self, critical: bool) -> Self
pub fn with_critical(self, critical: bool) -> Self
Trait Implementations§
Source§impl Task for SpawnTask
impl Task for SpawnTask
Source§fn run(
self: Box<Self>,
shutdown_rx: Receiver<()>,
) -> Pin<Box<dyn Future<Output = TaskResult> + Send>>
fn run( self: Box<Self>, shutdown_rx: Receiver<()>, ) -> Pin<Box<dyn Future<Output = TaskResult> + Send>>
运行任务 Read more
Source§fn is_critical(&self) -> bool
fn is_critical(&self) -> bool
是否关键任务 (失败时触发运行时停机) Read more
Source§fn ready_check(&self) -> Pin<Box<dyn Future<Output = TaskResult> + Send + '_>>
fn ready_check(&self) -> Pin<Box<dyn Future<Output = TaskResult> + Send + '_>>
就绪检查 (可选) Read more
Source§fn initial_state(&self) -> TaskState
fn initial_state(&self) -> TaskState
获取初始状态 Read more
Auto Trait Implementations§
impl !RefUnwindSafe for SpawnTask
impl !Sync for SpawnTask
impl !UnwindSafe for SpawnTask
impl Freeze for SpawnTask
impl Send for SpawnTask
impl Unpin for SpawnTask
impl UnsafeUnpin for SpawnTask
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