pub struct TaskContext {
pub task_id: String,
pub cancel_token: CancellationToken,
/* private fields */
}Expand description
Context provided to every background task closure.
Bundles the cancellation token with an optional inbox sender so tasks can both respond to cancellation and push events to the owner agent.
Fields§
§task_id: StringUnique identifier of this task.
cancel_token: CancellationTokenToken that signals when the task should stop.
Implementations§
Source§impl TaskContext
impl TaskContext
Sourcepub fn emit(&self, event_type: &str, payload: Value) -> bool
pub fn emit(&self, event_type: &str, payload: Value) -> bool
Emit a custom event to the owner agent.
The event is delivered to the agent’s inbox and drained at step
boundaries or when the agent is waiting for background tasks.
Returns false if no inbox is bound or the agent has ended.
Sourcepub async fn cancelled(&self)
pub async fn cancelled(&self)
Wait until cancellation is requested.
Use this for tasks that do their work, emit a result, then park themselves until killed:
ⓘ
|ctx| async move {
let result = do_work().await;
ctx.emit("ready", serde_json::json!(result));
ctx.cancelled().await; // park until kill
TaskResult::Cancelled
}Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns true if cancellation has been requested.
Trait Implementations§
Source§impl Clone for TaskContext
impl Clone for TaskContext
Source§fn clone(&self) -> TaskContext
fn clone(&self) -> TaskContext
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for TaskContext
impl !RefUnwindSafe for TaskContext
impl Send for TaskContext
impl Sync for TaskContext
impl Unpin for TaskContext
impl UnsafeUnpin for TaskContext
impl !UnwindSafe for TaskContext
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