pub struct TaskMonitor { /* private fields */ }Expand description
A monitor for spawning and monitoring background tasks.
The TaskMonitor allows you to spawn background tasks that are
automatically monitored for panics, errors, and unexpected termination.
In such cases, a BackgroundTaskFailure is sent through a broadcast
channel that subscribers can listen to.
§Example
use matrix_sdk_common::task_monitor::TaskMonitor;
let monitor = TaskMonitor::new();
// Subscribe to failures
let mut failures = monitor.subscribe();
// Spawn a task that runs indefinitely
let _handle = monitor.spawn_background_task("worker", async {
loop {
// Do work...
matrix_sdk_common::sleep::sleep(std::time::Duration::from_secs(1))
.await;
}
});Implementations§
Source§impl TaskMonitor
impl TaskMonitor
Sourcepub fn new() -> TaskMonitor
pub fn new() -> TaskMonitor
Create a new task monitor.
Sourcepub fn subscribe(&self) -> Receiver<BackgroundTaskFailure>
pub fn subscribe(&self) -> Receiver<BackgroundTaskFailure>
Subscribe to failure notifications.
Returns a broadcast receiver that will receive BackgroundTaskFailure
messages whenever a monitored task fails.
Note: If the receiver falls behind, older messages may be dropped.
Sourcepub fn spawn_background_task<F>(
&self,
name: impl Into<String>,
future: F,
) -> BackgroundTaskHandle
pub fn spawn_background_task<F>( &self, name: impl Into<String>, future: F, ) -> BackgroundTaskHandle
Spawn a background task that is expected to run indefinitely.
If the task completes (whether successfully or by panicking), it will be
reported as a BackgroundTaskFailure report through the broadcast
channel.
Use this for long-running tasks like event loops, sync tasks, or background workers that should never complete under normal operation.
§Arguments
name- A human-readable name for the task (for debugging purposes).future- The async task to run.
§Returns
A BackgroundTaskHandle that can be used to abort the task or check
if it has finished. This is the equivalent of tokio’s JoinHandle.
Sourcepub fn spawn_fallible_task<F, E>(
&self,
name: impl Into<String>,
future: F,
) -> BackgroundTaskHandlewhere
F: Future<Output = Result<(), E>> + SendOutsideWasm + 'static,
E: Error + SendOutsideWasm + 'static,
pub fn spawn_fallible_task<F, E>(
&self,
name: impl Into<String>,
future: F,
) -> BackgroundTaskHandlewhere
F: Future<Output = Result<(), E>> + SendOutsideWasm + 'static,
E: Error + SendOutsideWasm + 'static,
Spawn a background task that returns a Result.
The task is monitored for panics and errors; see also
BackgroundTaskFailure.
If the task returns Ok(()), it is considered successful and no failure
is reported.
§Arguments
name- A human-readable name for the task (for debugging purposes).future- The async task to run.
§Returns
A BackgroundTaskHandle that can be used to abort the task or check
if it has finished. This is the equivalent of tokio’s JoinHandle.
Trait Implementations§
Source§impl Debug for TaskMonitor
impl Debug for TaskMonitor
Source§impl Default for TaskMonitor
impl Default for TaskMonitor
Source§fn default() -> TaskMonitor
fn default() -> TaskMonitor
Auto Trait Implementations§
impl Freeze for TaskMonitor
impl RefUnwindSafe for TaskMonitor
impl Send for TaskMonitor
impl Sync for TaskMonitor
impl Unpin for TaskMonitor
impl UnwindSafe for TaskMonitor
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
§impl<T> CompatExt for T
impl<T> CompatExt for T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T, UT> HandleAlloc<UT> for T
impl<T, UT> HandleAlloc<UT> for T
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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