pub struct TaskManager { /* private fields */ }Expand description
Background task manager.
Manages the lifecycle of background tasks including submission, status tracking, and cancellation.
Implementations§
Source§impl TaskManager
impl TaskManager
Sourcepub fn new() -> TaskManager
pub fn new() -> TaskManager
Creates a new task manager.
Sourcepub fn with_list_changed_notifications() -> TaskManager
pub fn with_list_changed_notifications() -> TaskManager
Creates a new task manager with list change notifications enabled.
Sourcepub fn new_for_testing() -> TaskManager
pub fn new_for_testing() -> TaskManager
Creates a task manager configured for deterministic tests.
Tasks are not executed automatically; tests can drive state manually.
Converts this manager into a shared handle.
Sourcepub fn has_list_changed_notifications(&self) -> bool
pub fn has_list_changed_notifications(&self) -> bool
Returns whether list change notifications are enabled.
Sourcepub fn set_notification_sender(
&self,
sender: Arc<dyn Fn(JsonRpcRequest) + Send + Sync>,
)
pub fn set_notification_sender( &self, sender: Arc<dyn Fn(JsonRpcRequest) + Send + Sync>, )
Sets the notification sender for task status updates.
Sourcepub fn register_handler<F, Fut>(&self, task_type: impl Into<String>, handler: F)
pub fn register_handler<F, Fut>(&self, task_type: impl Into<String>, handler: F)
Registers a task handler for a specific task type.
The handler will be invoked when a task of this type is submitted.
Sourcepub fn submit(
&self,
_cx: &Cx,
task_type: impl Into<String>,
params: Option<Value>,
) -> Result<TaskId, McpError>
pub fn submit( &self, _cx: &Cx, task_type: impl Into<String>, params: Option<Value>, ) -> Result<TaskId, McpError>
Submits a new background task.
Returns the task ID for tracking. The task runs asynchronously in the background region.
Sourcepub fn start_task(&self, task_id: &TaskId) -> Result<(), McpError>
pub fn start_task(&self, task_id: &TaskId) -> Result<(), McpError>
Starts execution of a pending task.
This is called internally to transition a task from Pending to Running.
Sourcepub fn update_progress(
&self,
task_id: &TaskId,
progress: f64,
message: Option<String>,
)
pub fn update_progress( &self, task_id: &TaskId, progress: f64, message: Option<String>, )
Updates progress for a running task.
Sourcepub fn complete_task(&self, task_id: &TaskId, data: Value)
pub fn complete_task(&self, task_id: &TaskId, data: Value)
Completes a task with a successful result.
Sourcepub fn fail_task(&self, task_id: &TaskId, error: impl Into<String>)
pub fn fail_task(&self, task_id: &TaskId, error: impl Into<String>)
Fails a task with an error.
Sourcepub fn get_result(&self, task_id: &TaskId) -> Option<TaskResult>
pub fn get_result(&self, task_id: &TaskId) -> Option<TaskResult>
Gets the result of a completed task.
Sourcepub fn list_tasks(&self, status_filter: Option<TaskStatus>) -> Vec<TaskInfo>
pub fn list_tasks(&self, status_filter: Option<TaskStatus>) -> Vec<TaskInfo>
Lists all tasks, optionally filtered by status.
Sourcepub fn cancel(
&self,
task_id: &TaskId,
reason: Option<String>,
) -> Result<TaskInfo, McpError>
pub fn cancel( &self, task_id: &TaskId, reason: Option<String>, ) -> Result<TaskInfo, McpError>
Requests cancellation of a task.
Returns true if the task exists and cancellation was requested. The task may still be running until it checks for cancellation.
Sourcepub fn is_cancel_requested(&self, task_id: &TaskId) -> bool
pub fn is_cancel_requested(&self, task_id: &TaskId) -> bool
Checks if cancellation has been requested for a task.
Sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Returns the number of active (non-terminal) tasks.
Sourcepub fn total_count(&self) -> usize
pub fn total_count(&self) -> usize
Returns the total number of tasks.
Sourcepub fn cleanup_completed(&self, max_age: Duration)
pub fn cleanup_completed(&self, max_age: Duration)
Removes completed tasks older than the specified duration.
This is useful for preventing unbounded memory growth from completed tasks.
Trait Implementations§
Source§impl Debug for TaskManager
impl Debug for TaskManager
Source§impl Default for TaskManager
impl Default for TaskManager
Source§fn default() -> TaskManager
fn default() -> TaskManager
Auto Trait Implementations§
impl !Freeze for TaskManager
impl !RefUnwindSafe for TaskManager
impl !UnwindSafe for TaskManager
impl Send for TaskManager
impl Sync for TaskManager
impl Unpin for TaskManager
impl UnsafeUnpin for TaskManager
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
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>
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>
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