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 with_list_changed_notifications() -> Self
pub fn with_list_changed_notifications() -> Self
Creates a new task manager with list change notifications enabled.
Sourcepub fn new_for_testing() -> Self
pub fn new_for_testing() -> Self
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>,
) -> McpResult<TaskId>
pub fn submit( &self, _cx: &Cx, task_type: impl Into<String>, params: Option<Value>, ) -> McpResult<TaskId>
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) -> McpResult<()>
pub fn start_task(&self, task_id: &TaskId) -> McpResult<()>
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>,
) -> McpResult<TaskInfo>
pub fn cancel( &self, task_id: &TaskId, reason: Option<String>, ) -> McpResult<TaskInfo>
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.